diff --git a/index.js b/index.js index e370bbebf..a29526bf4 100644 --- a/index.js +++ b/index.js @@ -26,11 +26,13 @@ class ServerlessWebpack { this.options = options; if ( - (_.has(this.serverless, 'service.custom.webpack') && - _.isString(this.serverless.service.custom.webpack) && - _.endsWith(this.serverless.service.custom.webpack, '.ts')) || - (_.has(this.serverless, 'service.custom.webpack.webpackConfig') && - _.endsWith(this.serverless.service.custom.webpack.webpackConfig, '.ts')) + ( + (_.has(this.serverless, 'service.custom.webpack') && + _.isString(this.serverless.service.custom.webpack) && + _.endsWith(this.serverless.service.custom.webpack, '.ts')) || + (_.has(this.serverless, 'service.custom.webpack.webpackConfig') && + _.endsWith(this.serverless.service.custom.webpack.webpackConfig, '.ts')) + ) && !process[Symbol.for('ts-node.register.instance')] ) { try { require('ts-node/register'); diff --git a/index.test.js b/index.test.js index 585e8b70a..ef4ef3a99 100644 --- a/index.test.js +++ b/index.test.js @@ -80,6 +80,15 @@ describe('ServerlessWebpack', () => { expect(Module._load).to.have.been.calledWith('ts-node/register'); }); + it('should not register ts-node if it has already been registered', () => { + _.set(serverless, 'service.custom.webpack.webpackConfig', 'webpack.config.ts'); + process[Symbol.for('ts-node.register.instance')] = 'foo'; + new ServerlessWebpack(serverless, {}); + delete process[Symbol.for('ts-node.register.instance')]; + expect(Module._load).to.not.have.been.called; + expect(Module._load).to.not.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();