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

Commit

Permalink
feat: support loading node core modules (#76)
Browse files Browse the repository at this point in the history
* Allow worker-loader to load node core modules
* added tests for all targets
* change this.options.target to this.target
  • Loading branch information
FrogTheFrog authored and joshwiens committed Sep 25, 2017
1 parent 13f586b commit edcda35
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const path = require('path');
const WebWorkerTemplatePlugin = require('webpack/lib/webworker/WebWorkerTemplatePlugin');
const NodeTargetPlugin = require('webpack/lib/node/NodeTargetPlugin');
const SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
const loaderUtils = require('loader-utils');

Expand Down Expand Up @@ -38,6 +39,9 @@ module.exports.pitch = function pitch(request) {
}
const workerCompiler = this._compilation.createChildCompiler('worker', outputOptions);
workerCompiler.apply(new WebWorkerTemplatePlugin(outputOptions));
if (this.target !== 'webworker' && this.target !== 'web') {
workerCompiler.apply(new NodeTargetPlugin());
}
workerCompiler.apply(new SingleEntryPlugin(this.context, `!!${request}`, 'main'));
if (this.options && this.options.worker && this.options.worker.plugins) {
this.options.worker.plugins.forEach(plugin => workerCompiler.apply(plugin));
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/nodejs-core-modules/entry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const Worker = require('../../../index.js!./worker.js');
1 change: 1 addition & 0 deletions test/fixtures/nodejs-core-modules/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const fs = require('fs');
44 changes: 44 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,48 @@ describe('worker-loader', () => {
assert.notEqual(readFile(bundleFile).indexOf('// w2 inlined without fallback'), -1);
})
);

['web', 'webworker'].forEach((target) => {
it(`should have missing dependencies (${target})`, () =>
makeBundle('nodejs-core-modules', {
target,
module: {
rules: [
{
test: /worker\.js$/,
loader: '../index.js',
options: {
inline: true,
fallback: false,
},
},
],
},
}).then((stats) => {
assert.notEqual(stats.compilation.missingDependencies.length, 0);
})
);
});

['node', 'async-node', 'node-webkit', 'atom', 'electron', 'electron-main', 'electron-renderer'].forEach((target) => {
it(`should not have missing dependencies (${target})`, () =>
makeBundle('nodejs-core-modules', {
target,
module: {
rules: [
{
test: /worker\.js$/,
loader: '../index.js',
options: {
inline: true,
fallback: false,
},
},
],
},
}).then((stats) => {
assert.equal(stats.compilation.missingDependencies.length, 0);
})
);
});
});

0 comments on commit edcda35

Please sign in to comment.