Skip to content

Commit b7d4797

Browse files
Merge pull request #54 from NativeScript/vladimirov/sync-worker-pitch
fix: worker compilation should not be async
2 parents d8df7ac + 4b07012 commit b7d4797

File tree

4 files changed

+118
-94
lines changed

4 files changed

+118
-94
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## [0.11.0](https://github.com/NativeScript/worker-loader/compare/0.10.0...0.11.0) (2020-02-20)
2+
3+
### Bug Fixes
4+
5+
* worker compilation should not be async ([#54](https://github.com/NativeScript/worker-loader/pull/54))
6+
7+
18
## [0.9.5](https://github.com/NativeScript/worker-loader/compare/0.9.4...0.9.5) (2019-02-06)
29

310

src/index.js

Lines changed: 107 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -37,103 +37,120 @@ module.exports = function workerLoader() { };
3737

3838
const requests = [];
3939

40+
let pitchPromise = Promise.resolve();
4041
module.exports.pitch = function pitch(request) {
41-
if (!this.webpack) {
42-
throw new Error("Only usable with webpack");
43-
}
44-
4542
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-
}
5643

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+
}
6151

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+
});
8979

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;
11495
});
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);
131101
}
132102

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+
});
135116

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+
}));
138156
};
139-

src/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "nativescript-worker-loader",
3-
"version": "0.10.0",
3+
"version": "0.11.0",
44
"author": "NativeScript team",
55
"description": "nativescript worker loader module for webpack",
66
"scripts": {
77
"pretest": "babel ./test/test.es6.js --out-file ./test/test.es5.js",
8-
"test": "mocha test/test.es5.js",
8+
"test": "mocha test/test.es5.js --timeout 5000",
99
"posttest": "eslint .",
1010
"prepare": "npm run test"
1111
},

src/test/test.es6.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,10 @@ describe("worker-loader", () => {
171171
module: {
172172
rules: [
173173
{
174-
test: /worker\.js$/,
174+
test: /(w1|w2)\.js$/,
175175
loader: "../index.js",
176176
options: {
177-
inline: true,
177+
name: "[name].js",
178178
fallback: false,
179179
},
180180
},

0 commit comments

Comments
 (0)