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

Commit 8ffded7

Browse files
committedFeb 22, 2017
Add fallbacks option to prevent creating chunks with inlined workers
1 parent 9180a12 commit 8ffded7

File tree

6 files changed

+44
-1
lines changed

6 files changed

+44
-1
lines changed
 

‎createInlineWorker.js

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ module.exports = function(content, url) {
1818
return new Worker('data:application/javascript,' + encodeURIComponent(content));
1919
}
2020
} catch(e) {
21+
if (!url) {
22+
throw Error('Inline worker is not supported');
23+
}
2124
return new Worker(url);
2225
}
2326
}

‎index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ const getWorker = (file, content, query) => {
99
const workerPublicPath = `__webpack_public_path__ + ${JSON.stringify(file)}`;
1010
if (query.inline) {
1111
const createInlineWorkerPath = JSON.stringify(`!!${path.resolve('createInlineWorker.js')}`);
12-
return `require(${createInlineWorkerPath})(${JSON.stringify(content)}, ${workerPublicPath})`;
12+
const fallbackWorkerPath = query.fallback === false ? 'null' : workerPublicPath;
13+
return `require(${createInlineWorkerPath})(${JSON.stringify(content)}, ${fallbackWorkerPath})`;
1314
}
1415
return `new Worker(${workerPublicPath})`;
1516
};
@@ -55,6 +56,9 @@ module.exports.pitch = function pitch(request) {
5556
if (entries[0]) {
5657
const workerFile = entries[0].files[0];
5758
const workerFactory = getWorker(workerFile, compilation.assets[workerFile].source(), query);
59+
if (query.fallback === false) {
60+
delete this._compilation.assets[workerFile];
61+
}
5862
return callback(null, `module.exports = function() {\n\treturn ${workerFactory};\n};`);
5963
}
6064
return callback(null, null);

‎test/fixtures/no-fallbacks/entry.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const w1 = require('./w1.js');
2+
const w2 = require('./w2.js');

‎test/fixtures/no-fallbacks/w1.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// w1 inlined without fallback

‎test/fixtures/no-fallbacks/w2.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// w2 inlined without fallback

‎test/index.js

+32
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,36 @@ describe('worker-loader', () => {
147147
}
148148
})
149149
);
150+
151+
it.only('should not add fallback chunks with inline and fallback === false', () =>
152+
makeBundle('no-fallbacks', {
153+
module: {
154+
rules: [
155+
{
156+
test: /(w1|w2)\.js$/,
157+
loader: '../index.js',
158+
options: {
159+
inline: true,
160+
fallback: false
161+
},
162+
},
163+
],
164+
},
165+
}).then((stats) => {
166+
const workerFiles = stats.toJson('minimal').children
167+
.map(item => item.chunks)
168+
.reduce((acc, item) => acc.concat(item), [])
169+
.map(item => item.files)
170+
.map(item => `expected/no-fallbacks/${item}`);
171+
const bundleFile = stats.toJson('minimal').chunks
172+
.map(item => item.files)
173+
.reduce((acc, item) => acc.concat(item), [])
174+
.map(item => `expected/no-fallbacks/${item}`)[0];
175+
assert(bundleFile);
176+
assert.equal(fs.readdirSync('expected/no-fallbacks').length, 1);
177+
assert.equal(workerFiles.length, 0);
178+
assert.notEqual(readFile(bundleFile).indexOf('// w1 inlined without fallbacks'), -1);
179+
assert.notEqual(readFile(bundleFile).indexOf('// w2 inlined without fallbacks'), -1);
180+
})
181+
);
150182
});

0 commit comments

Comments
 (0)
This repository has been archived.