Skip to content

Commit bdb73b4

Browse files
committed
support webpack 5
1 parent 1b210fe commit bdb73b4

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,16 @@ module.exports = function nodeExternals(options) {
5353
});
5454

5555
// return an externals function
56-
return function (context, request, callback) {
56+
return function (arg1, arg2, arg3) {
57+
var context = arg1;
58+
var request = arg2;
59+
var callback = arg3;
60+
// in case of webpack 5
61+
if (arg1 && arg1.context && arg1.request) {
62+
context = arg1.context;
63+
request = arg1.request;
64+
callback = arg2;
65+
}
5766
var moduleName = getModuleName(request, includeAbsolutePaths);
5867
if (
5968
utils.contains(nodeModules, moduleName) &&

test/library.spec.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var mockNodeModules = testUtils.mockNodeModules;
55
var restoreMock = testUtils.restoreMock;
66
var context={};
77
var assertResult = testUtils.buildAssertion.bind(null, context);
8+
var assertResultWebpack5 = testUtils.buildAssertionWebpack5.bind(null, context);
89
var chai = require('chai');
910
var expect = chai.expect;
1011

@@ -262,6 +263,40 @@ describe('when modules dir does not exist', function() {
262263
});
263264
})
264265

266+
// Test basic functionality
267+
describe('invocation with no settings - webpack 5', function() {
268+
269+
before(function(){
270+
mockNodeModules();
271+
context.instance = nodeExternals();
272+
});
273+
274+
describe('should invoke a commonjs callback', function(){
275+
it('when given an existing module', assertResultWebpack5('moduleA', 'commonjs moduleA'));
276+
it('when given another existing module', assertResultWebpack5('moduleB', 'commonjs moduleB'));
277+
it('when given another existing module for scoped package', assertResultWebpack5('@organisation/moduleA', 'commonjs @organisation/moduleA'));
278+
it('when given an existing sub-module', assertResultWebpack5('moduleA/sub-module', 'commonjs moduleA/sub-module'));
279+
it('when given an existing file in a sub-module', assertResultWebpack5('moduleA/another-sub/index.js', 'commonjs moduleA/another-sub/index.js'));
280+
it('when given an existing file in a scoped package', assertResultWebpack5('@organisation/moduleA/index.js', 'commonjs @organisation/moduleA/index.js'))
281+
it('when given an another existing file in a scoped package', assertResultWebpack5('@organisation/base-node/vs/base/common/paths', 'commonjs @organisation/base-node/vs/base/common/paths'))
282+
283+
});
284+
285+
describe('should invoke an empty callback', function(){
286+
it('when given a non-node module', assertResultWebpack5('non-node-module', undefined));
287+
it('when given a module in the file but not in folder', assertResultWebpack5('moduleE', undefined));
288+
it('when given a relative path', assertResultWebpack5('./src/index.js', undefined));
289+
it('when given a different absolute path', assertResultWebpack5('/test/node_modules/non-node-module', undefined));
290+
it('when given a complex different absolute path', assertResultWebpack5('/test/node_modules/non-node-module/node_modules/moduleA', undefined));
291+
it('when given an absolute path', assertResultWebpack5('/test/node_modules/moduleA', undefined));
292+
it('when given an existing sub-module inside node_modules', assertResultWebpack5('/moduleA/node_modules/moduleB', undefined));
293+
});
294+
295+
after(function(){
296+
restoreMock()
297+
});
298+
});
299+
265300
describe('validate options', function () {
266301
it('should identify misspelled terms', function () {
267302
var results = utils.validateOptions({ whitelist: [], moduledirs: [] });

test/test-utils.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,23 @@ exports.buildAssertion = function buildAssertion(context, moduleName, expectedRe
2424
};
2525
}
2626

27+
/**
28+
* Creates an assertion function that makes sure to output expectedResult when given moduleName
29+
* <<Webpack 5 version>>
30+
* @param {object} context context object that holds the instance
31+
* @param {string} moduleName given module name
32+
* @param {string} expectedResult expected external module string
33+
* @return {function} the assertion function
34+
*/
35+
exports.buildAssertionWebpack5 = function buildAssertion(context, moduleName, expectedResult){
36+
return function(done) {
37+
context.instance({ context: relative(), request: moduleName }, function(noarg, externalModule) {
38+
expect(externalModule).to.be.equal(expectedResult);
39+
done();
40+
})
41+
};
42+
}
43+
2744
/**
2845
* Mocks the fs module to output a desired structure
2946
* @param {object} structure the requested structure

0 commit comments

Comments
 (0)