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

Commit f72b252

Browse files
authoredJul 31, 2020
refactor: use ES modules syntax for inline mode (#273)
1 parent 76c63fa commit f72b252

9 files changed

+144
-38
lines changed
 

‎src/utils.js

+17-11
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ function getExternalsType(compilerOptions) {
3535
}
3636

3737
function workerGenerator(loaderContext, workerFilename, workerSource, options) {
38-
let workerCode;
3938
let workerConstructor;
4039
let workerOptions;
4140

@@ -47,6 +46,9 @@ function workerGenerator(loaderContext, workerFilename, workerSource, options) {
4746
({ type: workerConstructor, options: workerOptions } = options.worker);
4847
}
4948

49+
const esModule =
50+
typeof options.esModule !== 'undefined' ? options.esModule : true;
51+
5052
if (options.inline) {
5153
const InlineWorkerPath = stringifyRequest(
5254
loaderContext,
@@ -61,23 +63,27 @@ function workerGenerator(loaderContext, workerFilename, workerSource, options) {
6163
)}`;
6264
}
6365

64-
workerCode = `require(${InlineWorkerPath})(${JSON.stringify(
66+
return `
67+
${
68+
esModule
69+
? `import worker from ${InlineWorkerPath};`
70+
: `var worker = require(${InlineWorkerPath});`
71+
}
72+
73+
${
74+
esModule ? 'export default' : 'module.exports ='
75+
} function() {\n return worker(${JSON.stringify(
6576
workerSource
6677
)}, ${JSON.stringify(workerConstructor)}, ${JSON.stringify(
6778
workerOptions
68-
)}, ${fallbackWorkerPath})`;
69-
} else {
70-
workerCode = `new ${workerConstructor}(__webpack_public_path__ + ${JSON.stringify(
71-
workerFilename
72-
)}${workerOptions ? `, ${JSON.stringify(workerOptions)}` : ''})`;
79+
)}, ${fallbackWorkerPath});\n}\n`;
7380
}
7481

75-
const esModule =
76-
typeof options.esModule !== 'undefined' ? options.esModule : true;
77-
7882
return `${
7983
esModule ? 'export default' : 'module.exports ='
80-
} function() {\n return ${workerCode};\n};\n`;
84+
} function() {\n return new ${workerConstructor}(__webpack_public_path__ + ${JSON.stringify(
85+
workerFilename
86+
)}${workerOptions ? `, ${JSON.stringify(workerOptions)}` : ''});\n}\n`;
8187
}
8288

8389
// Matches only the last occurrence of sourceMappingURL

‎test/__snapshots__/chunkFilename-option.test.js.snap

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports[`"name" option should work ("string"): errors 1`] = `Array []`;
55
exports[`"name" option should work ("string"): module 1`] = `
66
"export default function() {
77
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
8-
};
8+
}
99
"
1010
`;
1111

@@ -18,7 +18,7 @@ exports[`"name" option should work and respect the "output.chunkFilename" defaul
1818
exports[`"name" option should work and respect the "output.chunkFilename" default value option: module 1`] = `
1919
"export default function() {
2020
return new Worker(__webpack_public_path__ + \\"main.worker.js\\");
21-
};
21+
}
2222
"
2323
`;
2424

@@ -31,7 +31,7 @@ exports[`"name" option should work and respect the "output.chunkFilename" option
3131
exports[`"name" option should work and respect the "output.chunkFilename" option ("string"): module 1`] = `
3232
"export default function() {
3333
return new Worker(__webpack_public_path__ + \\"main.worker.js\\");
34-
};
34+
}
3535
"
3636
`;
3737

‎test/__snapshots__/esModule-option.test.js.snap

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports[`"esModule" option should work and generate ES module syntax by default:
55
exports[`"esModule" option should work and generate ES module syntax by default: module 1`] = `
66
"export default function() {
77
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
8-
};
8+
}
99
"
1010
`;
1111

@@ -16,7 +16,7 @@ exports[`"esModule" option should work with "false" value: errors 1`] = `Array [
1616
exports[`"esModule" option should work with "false" value: module 1`] = `
1717
"module.exports = function() {
1818
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
19-
};
19+
}
2020
"
2121
`;
2222

@@ -29,7 +29,7 @@ exports[`"esModule" option should work with "true" value: errors 1`] = `Array []
2929
exports[`"esModule" option should work with "true" value: module 1`] = `
3030
"export default function() {
3131
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
32-
};
32+
}
3333
"
3434
`;
3535

‎test/__snapshots__/filename-options.test.js.snap

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports[`"filename" option should work ("function"): errors 1`] = `Array []`;
55
exports[`"filename" option should work ("function"): module 1`] = `
66
"export default function() {
77
return new Worker(__webpack_public_path__ + \\"main.custom.worker.js\\");
8-
};
8+
}
99
"
1010
`;
1111

@@ -18,7 +18,7 @@ exports[`"filename" option should work ("string"): errors 1`] = `Array []`;
1818
exports[`"filename" option should work ("string"): module 1`] = `
1919
"export default function() {
2020
return new Worker(__webpack_public_path__ + \\"main.custom.worker.js\\");
21-
};
21+
}
2222
"
2323
`;
2424

@@ -31,7 +31,7 @@ exports[`"filename" option should work and respect the "output.filename" default
3131
exports[`"filename" option should work and respect the "output.filename" default value option: module 1`] = `
3232
"export default function() {
3333
return new Worker(__webpack_public_path__ + \\"main.worker.js\\");
34-
};
34+
}
3535
"
3636
`;
3737

@@ -44,7 +44,7 @@ exports[`"filename" option should work and respect the "output.filename" option
4444
exports[`"filename" option should work and respect the "output.filename" option ("function"): module 1`] = `
4545
"export default function() {
4646
return new Worker(__webpack_public_path__ + \\"main.custom.worker.js\\");
47-
};
47+
}
4848
"
4949
`;
5050

@@ -57,7 +57,7 @@ exports[`"filename" option should work and respect the "output.filename" option
5757
exports[`"filename" option should work and respect the "output.filename" option ("string"): module 1`] = `
5858
"export default function() {
5959
return new Worker(__webpack_public_path__ + \\"main.custom.worker.js\\");
60-
};
60+
}
6161
"
6262
`;
6363

‎test/__snapshots__/inline-option.test.js.snap

+25-1
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,26 @@ exports[`"inline" option should not work by default: errors 1`] = `Array []`;
55
exports[`"inline" option should not work by default: module 1`] = `
66
"export default function() {
77
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
8-
};
8+
}
99
"
1010
`;
1111

1212
exports[`"inline" option should not work by default: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
1313

1414
exports[`"inline" option should not work by default: warnings 1`] = `Array []`;
1515

16+
exports[`"inline" option should work with "fallback" value and "esModule" with "false" value: errors 1`] = `Array []`;
17+
18+
exports[`"inline" option should work with "fallback" value and "esModule" with "false" value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
19+
20+
exports[`"inline" option should work with "fallback" value and "esModule" with "false" value: warnings 1`] = `Array []`;
21+
22+
exports[`"inline" option should work with "fallback" value and "esModule" with "true" value: errors 1`] = `Array []`;
23+
24+
exports[`"inline" option should work with "fallback" value and "esModule" with "true" value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
25+
26+
exports[`"inline" option should work with "fallback" value and "esModule" with "true" value: warnings 1`] = `Array []`;
27+
1628
exports[`"inline" option should work with "fallback" value and the "devtool" option ("source-map" value): errors 1`] = `Array []`;
1729

1830
exports[`"inline" option should work with "fallback" value and the "devtool" option ("source-map" value): result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
@@ -25,6 +37,18 @@ exports[`"inline" option should work with "fallback" value: result 1`] = `"{\\"p
2537

2638
exports[`"inline" option should work with "fallback" value: warnings 1`] = `Array []`;
2739

40+
exports[`"inline" option should work with "no-fallback" value and "esModule" with "false" value: errors 1`] = `Array []`;
41+
42+
exports[`"inline" option should work with "no-fallback" value and "esModule" with "false" value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
43+
44+
exports[`"inline" option should work with "no-fallback" value and "esModule" with "false" value: warnings 1`] = `Array []`;
45+
46+
exports[`"inline" option should work with "no-fallback" value and "esModule" with "true" value: errors 1`] = `Array []`;
47+
48+
exports[`"inline" option should work with "no-fallback" value and "esModule" with "true" value: result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
49+
50+
exports[`"inline" option should work with "no-fallback" value and "esModule" with "true" value: warnings 1`] = `Array []`;
51+
2852
exports[`"inline" option should work with "no-fallback" value and the "devtool" option ("source-map" value): errors 1`] = `Array []`;
2953

3054
exports[`"inline" option should work with "no-fallback" value and the "devtool" option ("source-map" value): errors 2`] = `Array []`;

‎test/__snapshots__/loader.test.js.snap

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports[`worker-loader should work and respect the "devtool" option ("false" val
55
exports[`worker-loader should work and respect the "devtool" option ("false" value): module 1`] = `
66
"export default function() {
77
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
8-
};
8+
}
99
"
1010
`;
1111

@@ -18,7 +18,7 @@ exports[`worker-loader should work with "externals": errors 1`] = `Array []`;
1818
exports[`worker-loader should work with "externals": module 1`] = `
1919
"export default function() {
2020
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
21-
};
21+
}
2222
"
2323
`;
2424

@@ -31,7 +31,7 @@ exports[`worker-loader should work with WASM: errors 1`] = `Array []`;
3131
exports[`worker-loader should work with WASM: module 1`] = `
3232
"export default function() {
3333
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
34-
};
34+
}
3535
"
3636
`;
3737

@@ -44,7 +44,7 @@ exports[`worker-loader should work with async chunks: errors 1`] = `Array []`;
4444
exports[`worker-loader should work with async chunks: module 1`] = `
4545
"export default function() {
4646
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
47-
};
47+
}
4848
"
4949
`;
5050

@@ -57,7 +57,7 @@ exports[`worker-loader should work with inline syntax: errors 1`] = `Array []`;
5757
exports[`worker-loader should work with inline syntax: module 1`] = `
5858
"export default function() {
5959
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
60-
};
60+
}
6161
"
6262
`;
6363

@@ -70,7 +70,7 @@ exports[`worker-loader should work: errors 1`] = `Array []`;
7070
exports[`worker-loader should work: module 1`] = `
7171
"export default function() {
7272
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
73-
};
73+
}
7474
"
7575
`;
7676

‎test/__snapshots__/publicPath.test.js.snap

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports[`"publicPath" option should work and respect the "output.publicPath" opt
55
exports[`"publicPath" option should work and respect the "output.publicPath" option default value: module 1`] = `
66
"export default function() {
77
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
8-
};
8+
}
99
"
1010
`;
1111

@@ -18,7 +18,7 @@ exports[`"publicPath" option should work and respect the "output.publicPath" opt
1818
exports[`"publicPath" option should work and respect the "output.publicPath" option value ("function"): module 1`] = `
1919
"export default function() {
2020
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
21-
};
21+
}
2222
"
2323
`;
2424

@@ -31,7 +31,7 @@ exports[`"publicPath" option should work and respect the "output.publicPath" opt
3131
exports[`"publicPath" option should work and respect the "output.publicPath" option value ("string"): module 1`] = `
3232
"export default function() {
3333
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
34-
};
34+
}
3535
"
3636
`;
3737

@@ -44,7 +44,7 @@ exports[`"publicPath" option should work and respect the "publicPath" option ("f
4444
exports[`"publicPath" option should work and respect the "publicPath" option ("function"): module 1`] = `
4545
"export default function() {
4646
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
47-
};
47+
}
4848
"
4949
`;
5050

@@ -57,7 +57,7 @@ exports[`"publicPath" option should work and respect the "publicPath" option ("s
5757
exports[`"publicPath" option should work and respect the "publicPath" option ("string"): module 1`] = `
5858
"export default function() {
5959
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
60-
};
60+
}
6161
"
6262
`;
6363

@@ -70,7 +70,7 @@ exports[`"publicPath" option should work and use "__webpack_public_path__" by de
7070
exports[`"publicPath" option should work and use "__webpack_public_path__" by default: module 1`] = `
7171
"export default function() {
7272
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
73-
};
73+
}
7474
"
7575
`;
7676

‎test/__snapshots__/worker-option.test.js.snap

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ exports[`"workerType" option should support the "Worker" object value: errors 1`
1717
exports[`"workerType" option should support the "Worker" object value: module 1`] = `
1818
"export default function() {
1919
return new Worker(__webpack_public_path__ + \\"test.worker.js\\", {\\"type\\":\\"classic\\",\\"name\\":\\"worker-name\\"});
20-
};
20+
}
2121
"
2222
`;
2323

@@ -30,7 +30,7 @@ exports[`"workerType" option should support the "Worker" string value: errors 1`
3030
exports[`"workerType" option should support the "Worker" string value: module 1`] = `
3131
"export default function() {
3232
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
33-
};
33+
}
3434
"
3535
`;
3636

@@ -43,7 +43,7 @@ exports[`"workerType" option should use "Worker" by default: errors 1`] = `Array
4343
exports[`"workerType" option should use "Worker" by default: module 1`] = `
4444
"export default function() {
4545
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
46-
};
46+
}
4747
"
4848
`;
4949

‎test/inline-option.test.js

+76
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,80 @@ describe('"inline" option', () => {
141141
expect(getWarnings(stats)).toMatchSnapshot('warnings');
142142
expect(getErrors(stats)).toMatchSnapshot('errors');
143143
});
144+
145+
it('should work with "no-fallback" value and "esModule" with "false" value', async () => {
146+
const compiler = getCompiler('./basic/entry.js', {
147+
inline: 'no-fallback',
148+
esModule: false,
149+
});
150+
const stats = await compile(compiler);
151+
const result = await getResultFromBrowser(stats);
152+
const moduleSource = getModuleSource('./basic/worker.js', stats);
153+
154+
expect(moduleSource.indexOf('inline.js') > 0).toBe(true);
155+
expect(
156+
moduleSource.indexOf('__webpack_public_path__ + "test.worker.js"') === -1
157+
).toBe(true);
158+
expect(stats.compilation.assets['test.worker.js']).toBeUndefined();
159+
expect(result).toMatchSnapshot('result');
160+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
161+
expect(getErrors(stats)).toMatchSnapshot('errors');
162+
});
163+
164+
it('should work with "no-fallback" value and "esModule" with "true" value', async () => {
165+
const compiler = getCompiler('./basic/entry.js', {
166+
inline: 'no-fallback',
167+
esModule: true,
168+
});
169+
const stats = await compile(compiler);
170+
const result = await getResultFromBrowser(stats);
171+
const moduleSource = getModuleSource('./basic/worker.js', stats);
172+
173+
expect(moduleSource.indexOf('inline.js') > 0).toBe(true);
174+
expect(
175+
moduleSource.indexOf('__webpack_public_path__ + "test.worker.js"') === -1
176+
).toBe(true);
177+
expect(stats.compilation.assets['test.worker.js']).toBeUndefined();
178+
expect(result).toMatchSnapshot('result');
179+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
180+
expect(getErrors(stats)).toMatchSnapshot('errors');
181+
});
182+
183+
it('should work with "fallback" value and "esModule" with "false" value', async () => {
184+
const compiler = getCompiler('./basic/entry.js', {
185+
inline: 'fallback',
186+
esModule: false,
187+
});
188+
const stats = await compile(compiler);
189+
const result = await getResultFromBrowser(stats);
190+
const moduleSource = getModuleSource('./basic/worker.js', stats);
191+
192+
expect(moduleSource.indexOf('inline.js') > 0).toBe(true);
193+
expect(
194+
moduleSource.indexOf('__webpack_public_path__ + "test.worker.js"') > 0
195+
).toBe(true);
196+
expect(stats.compilation.assets['test.worker.js']).toBeDefined();
197+
expect(result).toMatchSnapshot('result');
198+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
199+
expect(getErrors(stats)).toMatchSnapshot('errors');
200+
});
201+
202+
it('should work with "fallback" value and "esModule" with "true" value', async () => {
203+
const compiler = getCompiler('./basic/entry.js', {
204+
inline: 'fallback',
205+
esModule: true,
206+
});
207+
const stats = await compile(compiler);
208+
const result = await getResultFromBrowser(stats);
209+
const moduleSource = getModuleSource('./basic/worker.js', stats);
210+
211+
expect(moduleSource.indexOf('inline.js') > 0).toBe(true);
212+
expect(
213+
moduleSource.indexOf('__webpack_public_path__ + "test.worker.js"') > 0
214+
).toBe(true);
215+
expect(stats.compilation.assets['test.worker.js']).toBeDefined();
216+
expect(result).toMatchSnapshot('result');
217+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
218+
expect(getErrors(stats)).toMatchSnapshot('errors');
219+
});
144220
});

0 commit comments

Comments
 (0)
This repository has been archived.