@@ -37,103 +37,120 @@ module.exports = function workerLoader() { };
37
37
38
38
const requests = [ ] ;
39
39
40
+ let pitchPromise = Promise . resolve ( ) ;
40
41
module . exports . pitch = function pitch ( request ) {
41
- if ( ! this . webpack ) {
42
- throw new Error ( "Only usable with webpack" ) ;
43
- }
44
-
45
42
const callback = this . async ( ) ;
46
- const options = loaderUtils . getOptions ( this ) || { } ;
47
- const compilerOptions = this . _compiler . options || { } ;
48
- const pluginOptions = compilerOptions . plugins . find ( p => p [ NATIVESCRIPT_WORKER_PLUGIN_SYMBOL ] ) . options ;
49
-
50
- // handle calls to itself to avoid an infinite loop
51
- if ( requests . indexOf ( request ) === - 1 ) {
52
- requests . push ( request ) ;
53
- } else {
54
- return callback ( null , "" ) ;
55
- }
56
43
57
- validateSchema ( optionsSchema , options , "Worker Loader" ) ;
58
- if ( ! this . _compilation . workerChunks ) {
59
- this . _compilation . workerChunks = [ ] ;
60
- }
44
+ pitchPromise = pitchPromise . then ( ( ) =>
45
+ new Promise ( ( resolve ) => {
46
+ if ( ! this . webpack ) {
47
+ const error = new Error ( "Only usable with webpack" ) ;
48
+ resolve ( ) ;
49
+ return callback ( error ) ;
50
+ }
61
51
62
- const filename = loaderUtils . interpolateName ( this , options . name || "[hash].worker.js" , {
63
- context : options . context || this . rootContext ,
64
- regExp : options . regExp ,
65
- } ) ;
66
-
67
- const outputOptions = {
68
- filename ,
69
- chunkFilename : `[id]. ${ filename } ` ,
70
- namedChunkFilename : null ,
71
- } ;
72
-
73
- const plugins = ( pluginOptions . plugins || [ ] ) . map ( plugin => {
74
- if ( typeof plugin !== "string" ) {
75
- return plugin ;
76
- }
77
- const found = compilerOptions . plugins . find ( p => p . constructor . name === plugin ) ;
78
- if ( ! found ) {
79
- console . warn ( `Warning (worker-plugin): Plugin " ${ plugin } " is not found.` ) ;
80
- }
81
- return found ;
82
- } ) ;
83
-
84
- const workerCompiler = this . _compilation . createChildCompiler ( "worker" , outputOptions , plugins ) ;
85
- new WebWorkerTemplatePlugin ( outputOptions ) . apply ( workerCompiler ) ;
86
- if ( this . target !== "webworker" && this . target !== "web" ) {
87
- new NodeTargetPlugin ( ) . apply ( workerCompiler ) ;
88
- }
52
+ const options = loaderUtils . getOptions ( this ) || { } ;
53
+ const compilerOptions = this . _compiler . options || { } ;
54
+ const pluginOptions = compilerOptions . plugins . find ( p => p [ NATIVESCRIPT_WORKER_PLUGIN_SYMBOL ] ) . options ;
55
+
56
+ // handle calls to itself to avoid an infinite loop
57
+ if ( requests . indexOf ( request ) === - 1 ) {
58
+ requests . push ( request ) ;
59
+ } else {
60
+ resolve ( ) ;
61
+ return callback ( null , "" ) ;
62
+ }
63
+
64
+ try {
65
+ validateSchema ( optionsSchema , options , "Worker Loader" ) ;
66
+ } catch ( err ) {
67
+ resolve ( ) ;
68
+ return callback ( err ) ;
69
+ }
70
+
71
+ if ( ! this . _compilation . workerChunks ) {
72
+ this . _compilation . workerChunks = [ ] ;
73
+ }
74
+
75
+ const filename = loaderUtils . interpolateName ( this , options . name || "[hash].worker.js" , {
76
+ context : options . context || this . rootContext ,
77
+ regExp : options . regExp ,
78
+ } ) ;
89
79
90
- new SingleEntryPlugin ( this . context , `!!${ request } ` , "main" ) . apply ( workerCompiler ) ;
91
- const plugin = { name : "WorkerLoader" } ;
92
-
93
- workerCompiler . hooks . thisCompilation . tap ( plugin , compilation => {
94
- /**
95
- * A dirty hack to disable HMR plugin in childCompilation:
96
- * https://github.com/webpack/webpack/blob/4056506488c1e071dfc9a0127daa61bf531170bf/lib/HotModuleReplacementPlugin.js#L154
97
- *
98
- * Once we update to webpack@4.40.3 and above this can be removed:
99
- * https://github.com/webpack/webpack/commit/1c4138d6ac04b7b47daa5ec4475c0ae1b4f596a2
100
- */
101
- compilation . hotUpdateChunkTemplate = null ;
102
- } ) ;
103
-
104
- workerCompiler . runAsChild ( ( err , entries , childCompilation ) => {
105
- if ( err ) {
106
- return callback ( err ) ;
107
- }
108
-
109
- if ( entries [ 0 ] ) {
110
- const fileDeps = Array . from ( childCompilation . fileDependencies ) ;
111
- this . clearDependencies ( ) ;
112
- fileDeps . forEach ( fileName => {
113
- this . addDependency ( fileName ) ;
80
+ const outputOptions = {
81
+ filename,
82
+ chunkFilename : `[id].${ filename } ` ,
83
+ namedChunkFilename : null ,
84
+ } ;
85
+
86
+ const plugins = ( pluginOptions . plugins || [ ] ) . map ( plugin => {
87
+ if ( typeof plugin !== "string" ) {
88
+ return plugin ;
89
+ }
90
+ const found = compilerOptions . plugins . find ( p => p . constructor . name === plugin ) ;
91
+ if ( ! found ) {
92
+ console . warn ( `Warning (worker-plugin): Plugin "${ plugin } " is not found.` ) ;
93
+ }
94
+ return found ;
114
95
} ) ;
115
- /**
116
- * Clears the hash of the child compilation as it affects the hash of the parent compilation:
117
- * https://github.com/webpack/webpack/blob/4056506488c1e071dfc9a0127daa61bf531170bf/lib/Compilation.js#L2281
118
- *
119
- * If we don't clear the hash an emit of runtime.js and an empty [somehash].hot-update.json will happen on save without changes.
120
- * This will restart the NS application.
121
- */
122
- childCompilation . hash = "" ;
123
- const workerFile = entries [ 0 ] . files [ 0 ] ;
124
- this . _compilation . workerChunks . push ( workerFile ) ;
125
- const workerFactory = getWorker ( workerFile ) ;
126
-
127
- // invalidate cache
128
- const processedIndex = requests . indexOf ( request ) ;
129
- if ( processedIndex > - 1 ) {
130
- requests . splice ( processedIndex , 1 ) ;
96
+
97
+ const workerCompiler = this . _compilation . createChildCompiler ( "worker" , outputOptions , plugins ) ;
98
+ new WebWorkerTemplatePlugin ( outputOptions ) . apply ( workerCompiler ) ;
99
+ if ( this . target !== "webworker" && this . target !== "web" ) {
100
+ new NodeTargetPlugin ( ) . apply ( workerCompiler ) ;
131
101
}
132
102
133
- return callback ( null , `module.exports = function() {\n\treturn ${ workerFactory } ;\n};` ) ;
134
- }
103
+ new SingleEntryPlugin ( this . context , `!!${ request } ` , "main" ) . apply ( workerCompiler ) ;
104
+ const plugin = { name : "WorkerLoader" } ;
105
+
106
+ workerCompiler . hooks . thisCompilation . tap ( plugin , compilation => {
107
+ /**
108
+ * A dirty hack to disable HMR plugin in childCompilation:
109
+ * https://github.com/webpack/webpack/blob/4056506488c1e071dfc9a0127daa61bf531170bf/lib/HotModuleReplacementPlugin.js#L154
110
+ *
111
+ * Once we update to webpack@4.40.3 and above this can be removed:
112
+ * https://github.com/webpack/webpack/commit/1c4138d6ac04b7b47daa5ec4475c0ae1b4f596a2
113
+ */
114
+ compilation . hotUpdateChunkTemplate = null ;
115
+ } ) ;
135
116
136
- return callback ( null , "" ) ;
137
- } ) ;
117
+ workerCompiler . runAsChild ( ( err , entries , childCompilation ) => {
118
+ if ( err ) {
119
+ resolve ( ) ;
120
+ return callback ( err ) ;
121
+ }
122
+
123
+ if ( entries [ 0 ] ) {
124
+ const fileDeps = Array . from ( childCompilation . fileDependencies ) ;
125
+ this . clearDependencies ( ) ;
126
+ fileDeps . forEach ( fileName => {
127
+ this . addDependency ( fileName ) ;
128
+ } ) ;
129
+ /**
130
+ * Clears the hash of the child compilation as it affects the hash of the parent compilation:
131
+ * https://github.com/webpack/webpack/blob/4056506488c1e071dfc9a0127daa61bf531170bf/lib/Compilation.js#L2281
132
+ *
133
+ * If we don't clear the hash an emit of runtime.js and
134
+ * an empty [somehash].hot-update.json will happen on save without changes.
135
+ * This will restart the NS application.
136
+ */
137
+ childCompilation . hash = "" ;
138
+ const workerFile = entries [ 0 ] . files [ 0 ] ;
139
+ this . _compilation . workerChunks . push ( workerFile ) ;
140
+ const workerFactory = getWorker ( workerFile ) ;
141
+
142
+ // invalidate cache
143
+ const processedIndex = requests . indexOf ( request ) ;
144
+ if ( processedIndex > - 1 ) {
145
+ requests . splice ( processedIndex , 1 ) ;
146
+ }
147
+
148
+ resolve ( ) ;
149
+ return callback ( null , `module.exports = function() {\n\treturn ${ workerFactory } ;\n};` ) ;
150
+ }
151
+
152
+ resolve ( ) ;
153
+ return callback ( null , "" ) ;
154
+ } ) ;
155
+ } ) ) ;
138
156
} ;
139
-
0 commit comments