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

Commit 1e761ed

Browse files
authoredJul 28, 2020
fix: respect externals (#264)
1 parent c117a7c commit 1e761ed

File tree

7 files changed

+124
-4
lines changed

7 files changed

+124
-4
lines changed
 

‎src/index.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import validateOptions from 'schema-utils';
44
import NodeTargetPlugin from 'webpack/lib/node/NodeTargetPlugin';
55
import SingleEntryPlugin from 'webpack/lib/SingleEntryPlugin';
66
import WebWorkerTemplatePlugin from 'webpack/lib/webworker/WebWorkerTemplatePlugin';
7+
import ExternalsPlugin from 'webpack/lib/ExternalsPlugin';
78

89
import schema from './options.json';
910
import supportWebpack5 from './supportWebpack5';
1011
import supportWebpack4 from './supportWebpack4';
11-
import { getDefaultFilename, getDefaultChunkFilename } from './utils';
12+
import {
13+
getDefaultFilename,
14+
getDefaultChunkFilename,
15+
getExternalsType,
16+
} from './utils';
1217

1318
let FetchCompileWasmPlugin;
1419
let FetchCompileAsyncWasmPlugin;
@@ -56,7 +61,11 @@ export function pitch(request) {
5661
? options.chunkFilename
5762
: getDefaultChunkFilename(compilerOptions.output.chunkFilename);
5863

59-
worker.options = { filename, chunkFilename, globalObject: 'self' };
64+
worker.options = {
65+
filename,
66+
chunkFilename,
67+
globalObject: 'self',
68+
};
6069

6170
worker.compiler = this._compilation.createChildCompiler(
6271
'worker',
@@ -79,6 +88,13 @@ export function pitch(request) {
7988
new FetchCompileAsyncWasmPlugin().apply(worker.compiler);
8089
}
8190

91+
if (compilerOptions.externals) {
92+
new ExternalsPlugin(
93+
getExternalsType(compilerOptions),
94+
compilerOptions.externals
95+
).apply(worker.compiler);
96+
}
97+
8298
new SingleEntryPlugin(this.context, `!!${request}`, 'main').apply(
8399
worker.compiler
84100
);

‎src/utils.js

+28-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@ function getDefaultChunkFilename(chunkFilename) {
1010
return chunkFilename.replace(/\.([a-z]+)(\?.+)?$/i, '.worker.$1$2');
1111
}
1212

13+
function getExternalsType(compilerOptions) {
14+
// For webpack@4
15+
if (compilerOptions.output.libraryTarget) {
16+
return compilerOptions.output.libraryTarget;
17+
}
18+
19+
// For webpack@5
20+
if (compilerOptions.externalsType) {
21+
return compilerOptions.externalsType;
22+
}
23+
24+
if (compilerOptions.output.library) {
25+
return compilerOptions.output.library.type;
26+
}
27+
28+
if (compilerOptions.output.module) {
29+
return 'module';
30+
}
31+
32+
return 'var';
33+
}
34+
1335
function getWorker(file, content, options) {
1436
const publicPath =
1537
typeof options.publicPath === 'undefined'
@@ -48,5 +70,9 @@ function getWorker(file, content, options) {
4870
})`;
4971
}
5072

51-
// eslint-disable-next-line import/prefer-default-export
52-
export { getDefaultFilename, getDefaultChunkFilename, getWorker };
73+
export {
74+
getDefaultFilename,
75+
getDefaultChunkFilename,
76+
getExternalsType,
77+
getWorker,
78+
};

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

+13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`worker-loader should work with "externals": errors 1`] = `Array []`;
4+
5+
exports[`worker-loader should work with "externals": module 1`] = `
6+
"export default function() {
7+
return new Worker(__webpack_public_path__ + \\"test.worker.js\\");
8+
};
9+
"
10+
`;
11+
12+
exports[`worker-loader should work with "externals": result 1`] = `"{\\"postMessage\\":true,\\"onmessage\\":true}"`;
13+
14+
exports[`worker-loader should work with "externals": warnings 1`] = `Array []`;
15+
316
exports[`worker-loader should work with WASM: errors 1`] = `Array []`;
417

518
exports[`worker-loader should work with WASM: module 1`] = `

‎test/fixtures/external/entry.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Worker from './worker.js';
2+
3+
const worker = new Worker();
4+
5+
let result;
6+
7+
worker.onmessage = function (event) {
8+
if (!result) {
9+
result = document.createElement("div");
10+
result.setAttribute('id', 'result');
11+
12+
document.body.append(result);
13+
}
14+
15+
result.innerText = JSON.stringify(event.data)
16+
};
17+
18+
const button = document.getElementById('button');
19+
20+
button.addEventListener('click', () => {
21+
worker.postMessage({ postMessage: true })
22+
});

‎test/fixtures/external/index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Webpack App</title>
6+
<meta name="viewport" content="width=device-width,initial-scale=1">
7+
</head>
8+
<body>
9+
10+
<button id="button">Run Action</button>
11+
12+
</body>
13+
</html>

‎test/fixtures/external/worker.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import foo from 'my-custom-module';
2+
3+
onmessage = function(event) {
4+
const workerResult = event.data;
5+
6+
workerResult.onmessage = true;
7+
8+
postMessage(workerResult);
9+
};

‎test/loader.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,25 @@ describe('worker-loader', () => {
5959
expect(getWarnings(stats)).toMatchSnapshot('warnings');
6060
expect(getErrors(stats)).toMatchSnapshot('errors');
6161
});
62+
63+
it('should work with "externals"', async () => {
64+
const compiler = getCompiler(
65+
'./external/entry.js',
66+
{},
67+
{
68+
externals: {
69+
'my-custom-module': 'navigator',
70+
},
71+
}
72+
);
73+
const stats = await compile(compiler);
74+
const result = await getResultFromBrowser(stats);
75+
76+
expect(getModuleSource('./external/worker.js', stats)).toMatchSnapshot(
77+
'module'
78+
);
79+
expect(result).toMatchSnapshot('result');
80+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
81+
expect(getErrors(stats)).toMatchSnapshot('errors');
82+
});
6283
});

0 commit comments

Comments
 (0)
This repository has been archived.