Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ class ServerlessWebpack {
(_.has(this.serverless, 'service.custom.webpack.webpackConfig') &&
_.endsWith(this.serverless.service.custom.webpack.webpackConfig, '.ts'))
) {
require('ts-node/register');
try {
require('ts-node/register');
} catch (e) {
throw new Error('If you want to use TypeScript with serverless-webpack, please add "ts-node" as dependency.');
}
}

_.assign(
Expand Down
22 changes: 21 additions & 1 deletion index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('ServerlessWebpack', () => {
let sandbox;
let serverless;
let ServerlessWebpack;
let moduleStub;

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

ServerlessWebpack = require('./index');
sandbox.spy(Module, '_load');
moduleStub = sandbox.stub(Module, '_load');
});

beforeEach(() => {
Expand Down Expand Up @@ -83,6 +84,25 @@ describe('ServerlessWebpack', () => {
expect(Module._load).to.have.been.calledOnce;
expect(Module._load).to.have.been.calledWith('ts-node/register');
});

it('should throw an error if config use TS but ts-node was not added as dependency', () => {
moduleStub.throws();

_.set(serverless, 'service.custom.webpack.webpackConfig', 'webpack.config.ts');

const badDeps = function() {
new ServerlessWebpack(serverless, {});
};

expect(badDeps).to.throw(
'If you want to use TypeScript with serverless-webpack, please add "ts-node" as dependency.'
);

expect(Module._load).to.have.been.calledOnce;
expect(Module._load).to.have.been.calledWith('ts-node/register');

moduleStub.reset();
});
});

describe('with a JS webpack configuration', () => {
Expand Down
1 change: 0 additions & 1 deletion lib/packExternalModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,4 +442,3 @@ module.exports = {
});
}
};

13 changes: 9 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@
"glob": "^7.1.4",
"is-builtin-module": "^3.0.0",
"lodash": "^4.17.19",
"semver": "^6.2.0",
"ts-node": "^8.3.0"
"semver": "^6.2.0"
},
"devDependencies": {
"babel-eslint": "^10.0.2",
Expand All @@ -80,5 +79,8 @@
},
"peerDependencies": {
"webpack": ">= 3.0.0 < 6"
},
"optionalDependencies": {
"ts-node": ">= 8.3.0"
}
}
7 changes: 2 additions & 5 deletions tests/validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,12 @@ describe('validate', () => {
it('should turn NodeStuffPlugin and NodeSourcePlugin plugins off by default', () => {
const testEntry = 'testentry';
const testConfig = {
entry: testEntry,
entry: testEntry
};
const testServicePath = 'testpath';
module.serverless.config.servicePath = testServicePath;
_.set(module.serverless.service, 'custom.webpack.config', testConfig);
return module
.validate()
.then(() => expect(module.webpackConfig.node).to.eql(false));
return module.validate().then(() => expect(module.webpackConfig.node).to.eql(false));
});
});

Expand Down Expand Up @@ -913,4 +911,3 @@ describe('validate', () => {
});
});
});