1
- const globby = require ( 'globby' ) ;
2
- const minimatch = require ( 'minimatch' ) ;
3
- const { resolve, join } = require ( 'path' ) ;
1
+ const { resolve } = require ( 'path' ) ;
4
2
const fs = require ( 'fs-extra' ) ;
5
3
const { warn, done } = require ( '@vue/cli-shared-utils' ) ;
6
4
const patternlabCore = require ( '@pattern-lab/core' ) ;
7
-
8
- const VueService = process . VUE_CLI_SERVICE ;
9
-
10
5
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' ;
12
9
13
10
class PatternlabPlugin {
14
11
constructor ( ) {
15
- this . pluginName = 'PatternlabPlugin' ;
16
- this . startTime = Date . now ( ) ;
17
- this . prevTimestamps = new Map ( ) ;
18
12
this . patternlab = patternlabCore ( plConfig ) ;
19
- this . PlPatternPath = resolve ( VueService . context , plConfig . paths . source . patterns ) ;
13
+ this . firstRun = true ;
14
+ }
20
15
21
- this . buildPatternlab ( ) ;
16
+ apply ( compiler ) {
22
17
// fix html webpack plugin error child compilation failed
23
18
// due to missing index.html inside the content base dir
24
19
// because patternlab build not finished before html plugin init :(
25
20
// 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' ) ) ;
39
22
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 ( ) ;
107
27
}
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
+ } ) ;
121
29
}
122
30
123
31
buildPatternlab ( ) {
124
32
const { cleanPublic } = plConfig ;
125
- const options = { 'cleanPublic' : cleanPublic } ;
33
+ const options = {
34
+ cleanPublic,
35
+ watch : true ,
36
+ } ;
126
37
127
38
if ( ! this . patternlab . isBusy ( ) ) {
128
39
try {
129
40
this . patternlab . build ( options )
130
- . then ( r => {
41
+ . then ( ( ) => {
131
42
done ( 'Patternlab build complete' ) ;
132
43
} )
133
44
. catch ( ( error ) => {
@@ -138,7 +49,6 @@ class PatternlabPlugin {
138
49
}
139
50
}
140
51
}
141
-
142
52
}
143
53
144
54
module . exports = PatternlabPlugin ;
0 commit comments