diff --git a/index.js b/index.js index 34ab7b1..0402719 100644 --- a/index.js +++ b/index.js @@ -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'); @@ -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)); diff --git a/test/fixtures/nodejs-core-modules/entry.js b/test/fixtures/nodejs-core-modules/entry.js new file mode 100644 index 0000000..51806c2 --- /dev/null +++ b/test/fixtures/nodejs-core-modules/entry.js @@ -0,0 +1 @@ +const Worker = require('../../../index.js!./worker.js'); diff --git a/test/fixtures/nodejs-core-modules/worker.js b/test/fixtures/nodejs-core-modules/worker.js new file mode 100644 index 0000000..1dfd32e --- /dev/null +++ b/test/fixtures/nodejs-core-modules/worker.js @@ -0,0 +1 @@ +const fs = require('fs'); diff --git a/test/index.js b/test/index.js index eeeaac7..bfe8aec 100644 --- a/test/index.js +++ b/test/index.js @@ -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); + }) + ); + }); });