Skip to content

Commit a962787

Browse files
authored
Merge pull request #953 from serverless-heaven/fix/webpack-external-module
2 parents 0123811 + 9bd1655 commit a962787

File tree

6 files changed

+66
-33
lines changed

6 files changed

+66
-33
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Change Log
22

3+
* 5.5.4
4+
* Fix the way to detect external module from Webpack ([#953][] @j0k3r)
5+
6+
[#944]: https://github.com/serverless-heaven/serverless-webpack/pull/953
7+
38
* 5.5.3
49
* Fallback on using service provider runtime when using `sls deploy function` ([#944][] @mostthingsweb)
510

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ class ServerlessWebpack {
106106
'after:package:createDeploymentArtifacts': () => BbPromise.bind(this).then(this.cleanup),
107107

108108
'before:deploy:function:packageFunction': () => {
109-
const runtime = this.serverless.service.getFunction(this.options.function).runtime || this.serverless.service.provider.runtime || 'nodejs';
109+
const runtime =
110+
this.serverless.service.getFunction(this.options.function).runtime ||
111+
this.serverless.service.provider.runtime ||
112+
'nodejs';
110113

111114
if (isNodeRuntime(runtime)) {
112115
return BbPromise.bind(this)

lib/compile.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ function getStatsLogger(statsConfig, consoleLog) {
2828
}
2929

3030
function getExternalModuleName(module) {
31-
const path = /^external "(.*)"$/.exec(module.identifier())[1];
31+
const pathArray = /^external .*"(.*?)"$/.exec(module.identifier());
32+
if (!pathArray) {
33+
throw new Error(`Unable to extract module name from Webpack identifier: ${module.identifier()}`);
34+
}
35+
36+
const path = pathArray[1];
3237
const pathComponents = path.split('/');
3338
const main = pathComponents[0];
3439

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-webpack",
3-
"version": "5.5.3",
3+
"version": "5.5.4",
44
"description": "Serverless plugin to bundle your javascript with Webpack",
55
"main": "index.js",
66
"types": "index.d.ts",

tests/compile.test.js

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -217,33 +217,21 @@ describe('compile', () => {
217217
outputPath: 'compileStats-outputPath'
218218
},
219219
modules: [
220-
{
221-
identifier: _.constant('"crypto"')
222-
},
223-
{
224-
identifier: _.constant('"uuid/v4"')
225-
},
226-
{
227-
identifier: _.constant('"mockery"')
228-
},
229-
{
230-
identifier: _.constant('"@scoped/vendor/module1"')
231-
},
232-
{
233-
identifier: _.constant('external "@scoped/vendor/module2"')
234-
},
235-
{
236-
identifier: _.constant('external "uuid/v4"')
237-
},
238-
{
239-
identifier: _.constant('external "localmodule"')
240-
},
241-
{
242-
identifier: _.constant('external "bluebird"')
243-
},
244-
{
245-
identifier: _.constant('external "aws-sdk"')
246-
}
220+
{ identifier: _.constant('"crypto"') },
221+
{ identifier: _.constant('"uuid/v4"') },
222+
{ identifier: _.constant('"mockery"') },
223+
{ identifier: _.constant('"@scoped/vendor/module1"') },
224+
{ identifier: _.constant('external "@scoped/vendor/module2"') },
225+
{ identifier: _.constant('external "uuid/v4"') },
226+
{ identifier: _.constant('external "localmodule"') },
227+
{ identifier: _.constant('external "bluebird"') },
228+
{ identifier: _.constant('external "aws-sdk"') },
229+
{ identifier: _.constant('external node-commonjs "lodash"') },
230+
{ identifier: _.constant('external commonjs-module "globby"') },
231+
{ identifier: _.constant('external this "glob"') },
232+
{ identifier: _.constant('external module "semver"') },
233+
{ identifier: _.constant('external assign "whatever"') },
234+
{ identifier: _.constant('external umd2 "hiyou"') }
247235
]
248236
},
249237
toString: sandbox.stub().returns('testStats'),
@@ -261,9 +249,41 @@ describe('compile', () => {
261249
{ external: 'uuid', origin: undefined },
262250
{ external: 'localmodule', origin: undefined },
263251
{ external: 'bluebird', origin: undefined },
264-
{ external: 'aws-sdk', origin: undefined }
252+
{ external: 'aws-sdk', origin: undefined },
253+
{ external: 'lodash', origin: undefined },
254+
{ external: 'globby', origin: undefined },
255+
{ external: 'glob', origin: undefined },
256+
{ external: 'semver', origin: undefined },
257+
{ external: 'whatever', origin: undefined },
258+
{ external: 'hiyou', origin: undefined }
265259
]);
266260
return null;
267261
});
268262
});
263+
264+
it('should fail to set stats externals', () => {
265+
const testWebpackConfig = 'testconfig';
266+
const multiStats = {
267+
stats: [
268+
{
269+
compilation: {
270+
errors: [],
271+
compiler: {
272+
outputPath: 'compileStats-outputPath'
273+
},
274+
modules: [{ identifier: _.constant('external node-commonjs "aws-sdk".') }]
275+
},
276+
toString: sandbox.stub().returns('testStats'),
277+
hasErrors: _.constant(false)
278+
}
279+
]
280+
};
281+
module.webpackConfig = testWebpackConfig;
282+
module.configuration = { concurrency: 1 };
283+
webpackMock.compilerMock.run.reset();
284+
webpackMock.compilerMock.run.yields(null, multiStats);
285+
return expect(module.compile()).to.be.rejectedWith(
286+
'Unable to extract module name from Webpack identifier: external node-commonjs "aws-sdk".'
287+
);
288+
});
269289
});

0 commit comments

Comments
 (0)