Skip to content
This repository was archived by the owner on Sep 9, 2021. It is now read-only.

Commit 0952bb6

Browse files
committedFeb 22, 2017
Split worker creator
1 parent fc0a874 commit 0952bb6

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed
 

‎index.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ const WebWorkerTemplatePlugin = require('webpack/lib/webworker/WebWorkerTemplate
55
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
66
const loaderUtils = require('loader-utils');
77

8+
const getWorker = (file, content, query) => {
9+
const workerPublicPath = `__webpack_public_path__ + ${JSON.stringify(file)}`;
10+
if (query.inline) {
11+
const createInlineWorkerPath = JSON.stringify(`!!${path.resolve('createInlineWorker.js')}`);
12+
return `require(${createInlineWorkerPath})(${JSON.stringify(content)}, ${workerPublicPath})`;
13+
}
14+
return `new Worker(${workerPublicPath})`;
15+
};
16+
817
module.exports = function workerLoader() {};
918

1019
module.exports.pitch = function pitch(request) {
@@ -45,15 +54,8 @@ module.exports.pitch = function pitch(request) {
4554
if (err) return callback(err);
4655
if (entries[0]) {
4756
const workerFile = entries[0].files[0];
48-
let constructor = `new Worker(__webpack_public_path__ + ${JSON.stringify(workerFile)})`;
49-
if (query.inline) {
50-
constructor = `require(${JSON.stringify(`!!${path.join(__dirname, 'createInlineWorker.js')}`)})(${
51-
JSON.stringify(compilation.assets[workerFile].source())
52-
}, __webpack_public_path__ + ${
53-
JSON.stringify(workerFile)
54-
})`;
55-
}
56-
return callback(null, `module.exports = function() {\n\treturn ${constructor};\n};`);
57+
const workerFactory = getWorker(workerFile, compilation.assets[workerFile].source(), query);
58+
return callback(null, `module.exports = function() {\n\treturn ${workerFactory};\n};`);
5759
}
5860
return callback(null, null);
5961
});

0 commit comments

Comments
 (0)
This repository has been archived.