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

Commit 23d2116

Browse files
authoredFeb 25, 2017
Merge pull request #52 from webpack-contrib/inline-fallback
Inline fallback
2 parents 614a241 + 232a08d commit 23d2116

File tree

10 files changed

+2091
-30
lines changed

10 files changed

+2091
-30
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

+14-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ 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.join(__dirname, 'createInlineWorker.js')}`);
12+
const fallbackWorkerPath = query.fallback === false ? 'null' : workerPublicPath;
13+
return `require(${createInlineWorkerPath})(${JSON.stringify(content)}, ${fallbackWorkerPath})`;
14+
}
15+
return `new Worker(${workerPublicPath})`;
16+
};
17+
818
module.exports = function workerLoader() {};
919

1020
module.exports.pitch = function pitch(request) {
@@ -45,15 +55,11 @@ module.exports.pitch = function pitch(request) {
4555
if (err) return callback(err);
4656
if (entries[0]) {
4757
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-
})`;
58+
const workerFactory = getWorker(workerFile, compilation.assets[workerFile].source(), query);
59+
if (query.fallback === false) {
60+
delete this._compilation.assets[workerFile];
5561
}
56-
return callback(null, `module.exports = function() {\n\treturn ${constructor};\n};`);
62+
return callback(null, `module.exports = function() {\n\treturn ${workerFactory};\n};`);
5763
}
5864
return callback(null, null);
5965
});

‎log.txt

+1,981
Large diffs are not rendered by default.
+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/inline-fallbacks/w1.js

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

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

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

‎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

+85-22
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const makeBundle = (name, options) => del(`expected/${name}`).then(() => {
2222
bundle.run((err, stats) => {
2323
if (err) {
2424
reject(err);
25+
} else if (stats.compilation.errors.length) {
26+
reject(Error(stats.toString('errors-only')));
2527
} else {
2628
resolve(stats);
2729
}
@@ -32,25 +34,25 @@ const makeBundle = (name, options) => del(`expected/${name}`).then(() => {
3234
describe('worker-loader', () => {
3335
it('should create chunk with worker', () =>
3436
makeBundle('worker').then((stats) => {
35-
const workerFile = stats.toJson('minimal').children
37+
const files = stats.toJson('minimal').children
3638
.map(item => item.chunks)
3739
.reduce((acc, item) => acc.concat(item), [])
3840
.map(item => item.files)
39-
.map(item => `expected/worker/${item}`)[0];
40-
assert(workerFile);
41-
assert.notEqual(readFile(workerFile).indexOf('// worker test mark'), -1);
41+
.map(item => `expected/worker/${item}`);
42+
assert.equal(files.length, 1);
43+
assert.notEqual(readFile(files[0]).indexOf('// worker test mark'), -1);
4244
})
4345
);
4446

4547
it('should create chunk with specified name in query', () =>
4648
makeBundle('name-query').then((stats) => {
47-
const file = stats.toJson('minimal').children
49+
const files = stats.toJson('minimal').children
4850
.map(item => item.chunks)
4951
.reduce((acc, item) => acc.concat(item), [])
5052
.map(item => item.files)
51-
.map(item => `expected/name-query/${item}`)[0];
52-
assert.equal(file, 'expected/name-query/namedWorker.js');
53-
assert.notEqual(readFile(file).indexOf('// named worker test mark'), -1);
53+
.map(item => `expected/name-query/${item}`);
54+
assert.equal(files[0], 'expected/name-query/namedWorker.js');
55+
assert.notEqual(readFile(files[0]).indexOf('// named worker test mark'), -1);
5456
})
5557
);
5658

@@ -72,24 +74,22 @@ describe('worker-loader', () => {
7274
.map(item => item.chunks)
7375
.reduce((acc, item) => acc.concat(item), [])
7476
.map(item => item.files)
75-
.map(item => `expected/name-options/${item}`);
76-
const w1 = files.find(file => file === 'expected/name-options/w1.js');
77-
const w2 = files.find(file => file === 'expected/name-options/w2.js');
78-
assert(w1);
79-
assert(w2);
80-
assert.notEqual(readFile(w1).indexOf('// w1 via worker options'), -1);
81-
assert.notEqual(readFile(w2).indexOf('// w2 via worker options'), -1);
77+
.map(item => `expected/name-options/${item}`)
78+
.sort();
79+
assert.equal(files.length, 2);
80+
assert.notEqual(readFile(files[0]).indexOf('// w1 via worker options'), -1);
81+
assert.notEqual(readFile(files[1]).indexOf('// w2 via worker options'), -1);
8282
})
8383
);
8484

8585
it('should inline worker with inline option in query', () =>
8686
makeBundle('inline-query').then((stats) => {
87-
const bundleFile = stats.toJson('minimal').chunks
87+
const files = stats.toJson('minimal').chunks
8888
.map(item => item.files)
8989
.reduce((acc, item) => acc.concat(item), [])
90-
.map(item => `expected/inline-query/${item}`)[0];
91-
assert(bundleFile);
92-
assert.notEqual(readFile(bundleFile).indexOf('// inlined worker test mark'), -1);
90+
.map(item => `expected/inline-query/${item}`);
91+
assert.equal(files.length, 1);
92+
assert.notEqual(readFile(files[0]).indexOf('// inlined worker test mark'), -1);
9393
})
9494
);
9595

@@ -107,13 +107,76 @@ describe('worker-loader', () => {
107107
],
108108
},
109109
}).then((stats) => {
110+
const files = stats.toJson('minimal').chunks
111+
.map(item => item.files)
112+
.reduce((acc, item) => acc.concat(item), [])
113+
.map(item => `expected/inline-options/${item}`);
114+
assert.equal(files.length, 1);
115+
assert.notEqual(readFile(files[0]).indexOf('// w1 inlined via options'), -1);
116+
assert.notEqual(readFile(files[0]).indexOf('// w2 inlined via options'), -1);
117+
})
118+
);
119+
120+
it('should add fallback chunks with inline option', () =>
121+
makeBundle('inline-fallbacks', {
122+
module: {
123+
rules: [
124+
{
125+
test: /(w1|w2)\.js$/,
126+
loader: '../index.js',
127+
options: {
128+
inline: true,
129+
},
130+
},
131+
],
132+
},
133+
}).then((stats) => {
134+
const files = stats.toJson('minimal').children
135+
.map(item => item.chunks)
136+
.reduce((acc, item) => acc.concat(item), [])
137+
.map(item => item.files)
138+
.map(item => `expected/inline-fallbacks/${item}`);
139+
assert.equal(files.length, 2);
140+
const w1 = readFile(files[0]);
141+
const w2 = readFile(files[1]);
142+
if (w1.indexOf('// w1 via worker options') !== -1) {
143+
assert.notEqual(w2.indexOf('// w2 via worker options'), -1);
144+
}
145+
if (w1.indexOf('// w2 via worker options') !== -1) {
146+
assert.notEqual(w2.indexOf('// w1 via worker options'), -1);
147+
}
148+
})
149+
);
150+
151+
it('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}`);
110171
const bundleFile = stats.toJson('minimal').chunks
111172
.map(item => item.files)
112173
.reduce((acc, item) => acc.concat(item), [])
113-
.map(item => `expected/inline-options/${item}`)[0];
174+
.map(item => `expected/no-fallbacks/${item}`)[0];
114175
assert(bundleFile);
115-
assert.notEqual(readFile(bundleFile).indexOf('// w1 inlined via options'), -1);
116-
assert.notEqual(readFile(bundleFile).indexOf('// w2 inlined via options'), -1);
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 fallback'), -1);
179+
assert.notEqual(readFile(bundleFile).indexOf('// w2 inlined without fallback'), -1);
117180
})
118181
);
119182
});

0 commit comments

Comments
 (0)
This repository has been archived.