Skip to content

Commit 6a1a069

Browse files
authored
Merge pull request #1 from hadl/feature/update-vuecli-V5
feat: migrate vue-cli v4 -> v5 and using VueJS V2
2 parents f0fd1a5 + 0ddf090 commit 6a1a069

21 files changed

+23084
-21909
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = {
88
'@vue/airbnb',
99
],
1010
parserOptions: {
11-
parser: 'babel-eslint',
11+
parser: '@babel/eslint-parser',
1212
},
1313
rules: {
1414
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',

.stylelintrc.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"extends": "stylelint-config-standard",
3-
"plugins": [
4-
"stylelint-scss"
2+
"extends": [
3+
"stylelint-config-standard-scss",
4+
"stylelint-config-recommended-vue/scss"
55
],
66
"rules": {
77
"at-rule-no-unknown": null,

README.md

Lines changed: 1 addition & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# patternlab-node-vuecli
2-
My first try to make patternlab node V3 with Twig, Vue-Cli-Service and Webpack work together.
2+
My first try to make patternlab node with Twig-PHP, Vue-Cli-Service and Webpack work together.
33

44
Thank you, https://github.com/Comcast/patternlab-edition-node-webpack for your inspiration!!!
55

@@ -13,43 +13,8 @@ The complete process is coupled to the Vue-Cli-Service - this is not a Webpack o
1313

1414
---
1515

16-
For me, the important things (which work) are:
17-
<small>Currently i did not do a lot of tests due to heavy debugging :)</small>
18-
19-
* Start Dev with `vue-cli-service serve`
20-
* Webpack Dev Server runs Patternlab with Twig Templates and not only the Vue public HTML document
21-
* HMR for JS and CSS
22-
* Code Splitting / Vue Async Components loading seams to work
23-
* Import Twig files in Twig files
24-
25-
Currently not working, but nice to have:
26-
* Delete, move or rename of patterns will not correctly recomplile / remove them from the "frontend"
27-
* They will be gone after restart of `serve` task
28-
29-
---
30-
3116
## Patternlab Node
3217
https://github.com/pattern-lab/patternlab-node
3318

3419
## Vue CLI
3520
https://github.com/vuejs/vue-cli
36-
37-
Used Vue settings to create project:
38-
```
39-
{
40-
"useConfigFiles": true,
41-
"plugins": {
42-
"@vue/cli-plugin-babel": {},
43-
"@vue/cli-plugin-router": {
44-
"historyMode": false
45-
},
46-
"@vue/cli-plugin-eslint": {
47-
"config": "airbnb",
48-
"lintOn": [
49-
"save"
50-
]
51-
}
52-
},
53-
"cssPreprocessor": "node-sass"
54-
}
55-
```

buildScripts/PatternlabPlugin.js

Lines changed: 18 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1,44 @@
1-
const globby = require('globby');
2-
const minimatch = require('minimatch');
3-
const { resolve, join } = require('path');
1+
const { resolve } = require('path');
42
const fs = require('fs-extra');
53
const { warn, done } = require('@vue/cli-shared-utils');
64
const patternlabCore = require('@pattern-lab/core');
7-
8-
const VueService = process.VUE_CLI_SERVICE;
9-
105
const plConfig = require('../patternlab-config.json');
11-
const contentBaseDir = join(VueService.context, plConfig.paths.public.root);
6+
7+
const contentBaseDir = resolve('./', plConfig.paths.public.root);
8+
const pluginName = 'PatternlabPlugin';
129

1310
class PatternlabPlugin {
1411
constructor() {
15-
this.pluginName = 'PatternlabPlugin';
16-
this.startTime = Date.now();
17-
this.prevTimestamps = new Map();
1812
this.patternlab = patternlabCore(plConfig);
19-
this.PlPatternPath = resolve(VueService.context, plConfig.paths.source.patterns);
13+
this.firstRun = true;
14+
}
2015

21-
this.buildPatternlab();
16+
apply(compiler) {
2217
// fix html webpack plugin error child compilation failed
2318
// due to missing index.html inside the content base dir
2419
// because patternlab build not finished before html plugin init :(
2520
// create index.html
26-
fs.ensureFileSync(join(contentBaseDir, 'index.html'));
27-
}
28-
29-
apply(compilation) {
30-
this.initHooks(compilation);
31-
}
32-
33-
initHooks(compilation) {
34-
if (compilation.hooks) {
35-
compilation.hooks.afterCompile.tap(this.pluginName, this.addPatternLabFiles.bind(this));
36-
compilation.hooks.watchRun.tap(this.pluginName, this.watchRun.bind(this));
37-
}
38-
}
21+
fs.ensureFileSync(resolve(contentBaseDir, 'index.html'));
3922

40-
addPatternLabFiles(compilation) {
41-
const patternFiles = this.getPatternlabFiles();
42-
patternFiles.forEach(item => {
43-
compilation.fileDependencies.add(item);
44-
});
45-
46-
if (compilation.contextDependencies && !compilation.contextDependencies.has(this.PlPatternPath)) {
47-
compilation.contextDependencies.add(this.PlPatternPath);
48-
}
49-
}
50-
51-
getPatternlabFiles() {
52-
const allWatchFiles = this.getWatchFileGlobs();
53-
54-
const files = [];
55-
allWatchFiles.forEach(globPath => {
56-
const patternFiles = globby
57-
.sync(globPath);
58-
patternFiles.forEach(item => {
59-
files.push(item);
60-
});
61-
});
62-
63-
return files;
64-
}
65-
66-
getWatchFileGlobs() {
67-
const supportedTemplateExtensions = this.patternlab.getSupportedTemplateExtensions();
68-
const templateFilePaths = supportedTemplateExtensions.map(
69-
function (extension) {
70-
return `${plConfig.paths.source.patterns}**/*${extension}`;
71-
}
72-
);
73-
74-
// pattern lab watch file globs
75-
const watchFiles = [
76-
...templateFilePaths,
77-
`${plConfig.paths.source.patterns}**/*+(json|md|yaml|yml)`,
78-
`${plConfig.paths.source.data}**/*+(json|md|yaml|yml)`,
79-
`${plConfig.paths.source.meta}**/*`,
80-
`${plConfig.paths.source.annotations}**/*`
81-
]
82-
.map(el => VueService.context + (el.startsWith('.') ? el.substr(1) : el));
83-
84-
return watchFiles;
85-
}
86-
87-
matchesWatchFileGlob(paths = []) {
88-
const globs = this.getWatchFileGlobs();
89-
let matches = [];
90-
for (let globIndex = 0; globIndex < globs.length; globIndex++) {
91-
matches = minimatch.match(paths, globs[globIndex]);
92-
if (!!matches.length) {
93-
break;
94-
}
95-
}
96-
97-
return !!matches.length;
98-
}
99-
100-
watchRun(compilation, callback) {
101-
const changedFiles = Array.from(compilation.fileTimestamps.keys()).filter(
102-
watchfile => {
103-
return (
104-
(this.prevTimestamps.get(watchfile) || this.startTime)
105-
< (compilation.fileTimestamps.get(watchfile) || Infinity)
106-
);
23+
compiler.hooks.done.tap(pluginName, () => {
24+
if (this.firstRun) {
25+
this.firstRun = false;
26+
this.buildPatternlab();
10727
}
108-
);
109-
110-
const hasChangedPLSourceFiles = !!changedFiles.length && this.matchesWatchFileGlob(changedFiles);
111-
const removedFiles = Array.from(compilation.removedFiles);
112-
const hasDeletedPLSourceFiles = !!removedFiles.length && this.matchesWatchFileGlob(removedFiles);
113-
114-
if (hasChangedPLSourceFiles || hasDeletedPLSourceFiles) {
115-
this.buildPatternlab();
116-
}
117-
118-
this.prevTimestamps = compilation.fileTimestamps;
119-
120-
if (callback) { callback(); }
28+
});
12129
}
12230

12331
buildPatternlab() {
12432
const { cleanPublic } = plConfig;
125-
const options = { 'cleanPublic': cleanPublic };
33+
const options = {
34+
cleanPublic,
35+
watch: true,
36+
};
12637

12738
if (!this.patternlab.isBusy()) {
12839
try {
12940
this.patternlab.build(options)
130-
.then(r => {
41+
.then(() => {
13142
done('Patternlab build complete');
13243
})
13344
.catch((error) => {
@@ -138,7 +49,6 @@ class PatternlabPlugin {
13849
}
13950
}
14051
}
141-
14252
}
14353

14454
module.exports = PatternlabPlugin;
Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,44 @@
1+
/* eslint-disable no-param-reassign */
2+
const { join, resolve } = require('path');
13
const PatternLabPlugin = require('./PatternlabPlugin');
24

3-
const { getIfUtils } = require('webpack-config-utils');
4-
const { join } = require('path');
5-
const VueService = process.VUE_CLI_SERVICE;
6-
75
const plConfig = require('../patternlab-config.json');
8-
const { ifDevelopment } = getIfUtils(process.env.NODE_ENV);
9-
const contentBaseDir = join(VueService.context, plConfig.paths.public.root);
106

7+
const ifDevelopment = process.env.NODE_ENV !== 'production';
8+
const contentBaseDir = resolve('./', plConfig.paths.public.root);
9+
10+
/**
11+
* update and add plugins
12+
* @param config
13+
*/
1114
const patternlabVuePluginConfig = (config) => {
1215
config
1316
.plugin('html')
14-
.tap(args => {
17+
.tap((args) => {
1518
args[0].template = join(contentBaseDir, 'index.html');
1619
return args;
1720
});
1821

19-
if (ifDevelopment()) {
20-
config.plugin('patternlabplugin').use(PatternLabPlugin)
22+
if (ifDevelopment) {
23+
config.plugin('patternlabplugin')
24+
.use(PatternLabPlugin)
25+
.before('html');
2126
}
27+
};
2228

23-
config.devServer.contentBase(contentBaseDir);
29+
/**
30+
* Change dev server directory in configureWebpack
31+
* @param config
32+
*/
33+
const patternlabVueWebpackConfig = (config) => {
34+
if (!config.devServer) {
35+
config.devServer = {};
36+
}
37+
if (!config.devServer.static) {
38+
config.devServer.static = {};
39+
}
40+
config.devServer.static.directory = contentBaseDir;
2441
};
2542

2643
exports.patternlabVuePluginConfig = patternlabVuePluginConfig;
44+
exports.patternlabVueWebpackConfig = patternlabVueWebpackConfig;

0 commit comments

Comments
 (0)