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

feat: support loading node core modules #76

Merged
merged 7 commits into from
Jul 4, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.options.target !== 'webworker' && this.options.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');
39 changes: 39 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,43 @@ describe('worker-loader', () => {
assert.notEqual(readFile(bundleFile).indexOf('// w2 inlined without fallback'), -1);
})
);

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

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