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

Commit edcda35

Browse files
FrogTheFrogjoshwiens
authored andcommitted
feat: support loading node core modules (#76)
* Allow worker-loader to load node core modules * added tests for all targets * change this.options.target to this.target
1 parent 13f586b commit edcda35

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const path = require('path');
44
const WebWorkerTemplatePlugin = require('webpack/lib/webworker/WebWorkerTemplatePlugin');
5+
const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
56
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
67
const loaderUtils = require('loader-utils');
78

@@ -38,6 +39,9 @@ module.exports.pitch = function pitch(request) {
3839
}
3940
const workerCompiler = this._compilation.createChildCompiler('worker', outputOptions);
4041
workerCompiler.apply(new WebWorkerTemplatePlugin(outputOptions));
42+
if (this.target !== 'webworker' && this.target !== 'web') {
43+
workerCompiler.apply(new NodeTargetPlugin());
44+
}
4145
workerCompiler.apply(new SingleEntryPlugin(this.context, `!!${request}`, 'main'));
4246
if (this.options && this.options.worker && this.options.worker.plugins) {
4347
this.options.worker.plugins.forEach(plugin => workerCompiler.apply(plugin));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const Worker = require('../../../index.js!./worker.js');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const fs = require('fs');

test/index.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,48 @@ describe('worker-loader', () => {
180180
assert.notEqual(readFile(bundleFile).indexOf('// w2 inlined without fallback'), -1);
181181
})
182182
);
183+
184+
['web', 'webworker'].forEach((target) => {
185+
it(`should have missing dependencies (${target})`, () =>
186+
makeBundle('nodejs-core-modules', {
187+
target,
188+
module: {
189+
rules: [
190+
{
191+
test: /worker\.js$/,
192+
loader: '../index.js',
193+
options: {
194+
inline: true,
195+
fallback: false,
196+
},
197+
},
198+
],
199+
},
200+
}).then((stats) => {
201+
assert.notEqual(stats.compilation.missingDependencies.length, 0);
202+
})
203+
);
204+
});
205+
206+
['node', 'async-node', 'node-webkit', 'atom', 'electron', 'electron-main', 'electron-renderer'].forEach((target) => {
207+
it(`should not have missing dependencies (${target})`, () =>
208+
makeBundle('nodejs-core-modules', {
209+
target,
210+
module: {
211+
rules: [
212+
{
213+
test: /worker\.js$/,
214+
loader: '../index.js',
215+
options: {
216+
inline: true,
217+
fallback: false,
218+
},
219+
},
220+
],
221+
},
222+
}).then((stats) => {
223+
assert.equal(stats.compilation.missingDependencies.length, 0);
224+
})
225+
);
226+
});
183227
});

0 commit comments

Comments
 (0)