diff --git a/src/index.ts b/src/index.ts index 27e1769b..0c537bfe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -39,9 +39,13 @@ export class TypeScriptPlugin { await this.compileTs() this.watchAll() }, - 'before:package:createDeploymentArtifacts': this.compileTs.bind(this), + 'before:package:createDeploymentArtifacts': async () => { + await this.compileTs(true) + }, 'after:package:createDeploymentArtifacts': this.cleanup.bind(this), - 'before:deploy:function:packageFunction': this.compileTs.bind(this), + 'before:deploy:function:packageFunction': async () => { + await this.compileTs(true) + }, 'after:deploy:function:packageFunction': this.cleanup.bind(this), 'before:invoke:local:invoke': async () => { const emitedFiles = await this.compileTs() @@ -111,7 +115,7 @@ export class TypeScriptPlugin { }) } - async compileTs(): Promise { + async compileTs(packaging = false): Promise { this.prepare() this.serverless.cli.log('Compiling with Typescript...') @@ -130,19 +134,26 @@ export class TypeScriptPlugin { tsconfig.outDir = buildFolder const emitedFiles = await typescript.run(this.rootFileNames, tsconfig) - await this.copyExtras() + await this.copyExtras(packaging) this.serverless.cli.log('Typescript compiled.') return emitedFiles } - async copyExtras() { + async copyExtras(packaging = false) { const outPkgPath = path.resolve(path.join(buildFolder, 'package.json')) const outModulesPath = path.resolve(path.join(buildFolder, 'node_modules')) // Link or copy node_modules and package.json to .build so Serverless can - // exlcude devDeps during packaging - if (!fs.existsSync(outModulesPath)) { - await this.linkOrCopy(path.resolve('node_modules'), outModulesPath, 'junction') + // exclude devDeps during packaging + if (packaging) { + if (fs.existsSync(path.resolve(path.join(buildFolder, 'node_modules')))) { + fs.unlinkSync(path.resolve(path.join(buildFolder, 'node_modules'))) + } + fs.copySync(path.resolve('node_modules'), path.resolve(path.join(buildFolder, 'node_modules'))) + } else { + if (!fs.existsSync(outModulesPath)) { + await this.linkOrCopy(path.resolve('node_modules'), outModulesPath, 'junction') + } } if (!fs.existsSync(outPkgPath)) {