Skip to content

Commit dd134be

Browse files
committed
Move ts-node as optional dependency
That dependency must be added by the user only if they use TypeScript. An error will be throw if the dependency is not added and the serverless-webpack configuration is written in TypeScript.
1 parent ebe825e commit dd134be

File tree

6 files changed

+41
-14
lines changed

6 files changed

+41
-14
lines changed

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ class ServerlessWebpack {
3232
(_.has(this.serverless, 'service.custom.webpack.webpackConfig') &&
3333
_.endsWith(this.serverless.service.custom.webpack.webpackConfig, '.ts'))
3434
) {
35-
require('ts-node/register');
35+
try {
36+
require('ts-node/register');
37+
} catch (e) {
38+
throw new Error('If you want to use TypeScript with serverless-webpack, please add "ts-node" as dependency.');
39+
}
3640
}
3741

3842
_.assign(

index.test.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ describe('ServerlessWebpack', () => {
2020
let sandbox;
2121
let serverless;
2222
let ServerlessWebpack;
23+
let moduleStub;
2324

2425
before(function() {
2526
// Mockery might take some time to clear the cache. So add 3 seconds to the default timeout.
@@ -32,7 +33,7 @@ describe('ServerlessWebpack', () => {
3233
mockery.registerMock('webpack', {});
3334

3435
ServerlessWebpack = require('./index');
35-
sandbox.spy(Module, '_load');
36+
moduleStub = sandbox.stub(Module, '_load');
3637
});
3738

3839
beforeEach(() => {
@@ -83,6 +84,25 @@ describe('ServerlessWebpack', () => {
8384
expect(Module._load).to.have.been.calledOnce;
8485
expect(Module._load).to.have.been.calledWith('ts-node/register');
8586
});
87+
88+
it('should throw an error if config use TS but ts-node was not added as dependency', () => {
89+
moduleStub.throws();
90+
91+
_.set(serverless, 'service.custom.webpack.webpackConfig', 'webpack.config.ts');
92+
93+
const badDeps = function() {
94+
new ServerlessWebpack(serverless, {});
95+
};
96+
97+
expect(badDeps).to.throw(
98+
'If you want to use TypeScript with serverless-webpack, please add "ts-node" as dependency.'
99+
);
100+
101+
expect(Module._load).to.have.been.calledOnce;
102+
expect(Module._load).to.have.been.calledWith('ts-node/register');
103+
104+
moduleStub.reset();
105+
});
86106
});
87107

88108
describe('with a JS webpack configuration', () => {

lib/packExternalModules.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,4 +442,3 @@ module.exports = {
442442
});
443443
}
444444
};
445-

package-lock.json

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

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@
5555
"glob": "^7.1.4",
5656
"is-builtin-module": "^3.0.0",
5757
"lodash": "^4.17.19",
58-
"semver": "^6.2.0",
59-
"ts-node": "^8.3.0"
58+
"semver": "^6.2.0"
6059
},
6160
"devDependencies": {
6261
"babel-eslint": "^10.0.2",
@@ -80,5 +79,8 @@
8079
},
8180
"peerDependencies": {
8281
"webpack": ">= 3.0.0 < 6"
82+
},
83+
"optionalDependencies": {
84+
"ts-node": ">= 8.3.0"
8385
}
8486
}

tests/validate.test.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,12 @@ describe('validate', () => {
216216
it('should turn NodeStuffPlugin and NodeSourcePlugin plugins off by default', () => {
217217
const testEntry = 'testentry';
218218
const testConfig = {
219-
entry: testEntry,
219+
entry: testEntry
220220
};
221221
const testServicePath = 'testpath';
222222
module.serverless.config.servicePath = testServicePath;
223223
_.set(module.serverless.service, 'custom.webpack.config', testConfig);
224-
return module
225-
.validate()
226-
.then(() => expect(module.webpackConfig.node).to.eql(false));
224+
return module.validate().then(() => expect(module.webpackConfig.node).to.eql(false));
227225
});
228226
});
229227

@@ -913,4 +911,3 @@ describe('validate', () => {
913911
});
914912
});
915913
});
916-

0 commit comments

Comments
 (0)