diff --git a/README.md b/README.md index dbcea723..a72332e2 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,18 @@ The default `tsconfig.json` file used by the plugin looks like this: > Note 2: Don't confuse the [`tsconfig.json`](tsconfig.json) in this repository with the one mentioned above. +You can also enable or disable functionalities in `serverless.yml`: + +```yml +custom: + typescript: + run: true + offline: true # compile when running serverless offline + package: true # compile when doing serverless package + deploy: true # compile when doing serverless deploy + invoke: true # compile when doing serverless invoke +``` + ### Including extra files All files from `package/include` will be included in the final build file. See [Exclude/Include](https://serverless.com/framework/docs/providers/aws/guide/packaging#exclude--include) diff --git a/package.json b/package.json index 0599b0ea..7d72c8d4 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "rimraf": "2.6.3", "ts-jest": "24.0.2", "tslint": "5.14.0", - "typescript": "^3.4.1" + "typescript": "^3.7.0" }, "dependencies": { "fs-extra": "^7.0.1", diff --git a/src/Serverless.d.ts b/src/Serverless.d.ts index ec7d7049..4022d002 100644 --- a/src/Serverless.d.ts +++ b/src/Serverless.d.ts @@ -15,6 +15,9 @@ declare namespace Serverless { functions: { [key: string]: Serverless.Function } + custom: { + [key: string]: any + } package: Serverless.Package getAllFunctions(): string[] } @@ -43,4 +46,4 @@ declare namespace Serverless { interface PluginManager { spawn(command: string): Promise } -} \ No newline at end of file +} diff --git a/src/index.ts b/src/index.ts index a249ce58..cd683f5e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -21,52 +21,68 @@ export class TypeScriptPlugin { this.serverless = serverless this.options = options - this.hooks = { - 'before:run:run': async () => { + this.hooks = {} + + if (serverless.service?.custom?.typescript?.run ?? true) { + this.hooks['before:run:run'] = async () => { await this.compileTs() await this.copyExtras() await this.copyDependencies() - }, - 'before:offline:start': async () => { + } + } + + if (serverless.service?.custom?.typescript?.offline ?? true) { + this.hooks['before:offline:start'] = async () => { await this.compileTs() await this.copyExtras() await this.copyDependencies() this.watchAll() - }, - 'before:offline:start:init': async () => { + } + this.hooks['before:offline:start:init'] = async () => { await this.compileTs() await this.copyExtras() await this.copyDependencies() this.watchAll() - }, - 'before:package:createDeploymentArtifacts': async () => { + } + } + + if (serverless.service?.custom?.typescript?.package ?? true) { + this.hooks['before:package:createDeploymentArtifacts'] = async () => { await this.compileTs() await this.copyExtras() await this.copyDependencies(true) - }, - 'after:package:createDeploymentArtifacts': async () => { + } + this.hooks['after:package:createDeploymentArtifacts'] = async () => { await this.cleanup() - }, - 'before:deploy:function:packageFunction': async () => { + } + } + + if (serverless.service?.custom?.typescript?.deploy ?? true) { + this.hooks['before:deploy:function:packageFunction'] = async () => { await this.compileTs() await this.copyExtras() await this.copyDependencies(true) - }, - 'after:deploy:function:packageFunction': async () => { + } + this.hooks['after:deploy:function:packageFunction'] = async () => { await this.cleanup() - }, - 'before:invoke:local:invoke': async () => { - const emitedFiles = await this.compileTs() - await this.copyExtras() - await this.copyDependencies() - if (this.isWatching) { - emitedFiles.forEach(filename => { - const module = require.resolve(path.resolve(this.originalServicePath, filename)) - delete require.cache[module] - }) + } + } + + if (serverless.service?.custom?.typescript?.invoke ?? true) { + this.hooks['before:invoke:local:invoke'] = async () => { + if (serverless.service?.custom?.typescript?.invoke ?? true) { + const emitedFiles = await this.compileTs() + await this.copyExtras() + await this.copyDependencies() + if (this.isWatching) { + emitedFiles.forEach(filename => { + const module = require.resolve(path.resolve(this.originalServicePath, filename)) + delete require.cache[module] + }) + } } - }, - 'after:invoke:local:invoke': () => { + } + this.hooks['after:invoke:local:invoke'] = () => { if (this.options.watch) { this.watchFunction() this.serverless.cli.log('Waiting for changes...') diff --git a/yarn.lock b/yarn.lock index 735f988f..c8653353 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3455,10 +3455,10 @@ type-check@~0.3.2: dependencies: prelude-ls "~1.1.2" -typescript@^3.4.1: - version "3.4.1" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.4.1.tgz#b6691be11a881ffa9a05765a205cb7383f3b63c6" - integrity sha512-3NSMb2VzDQm8oBTLH6Nj55VVtUEpe/rgkIzMir0qVoLyjDZlnMBva0U6vDiV3IH+sl/Yu6oP5QwsAQtHPmDd2Q== +typescript@^3.7.0: + version "3.9.7" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.7.tgz#98d600a5ebdc38f40cb277522f12dc800e9e25fa" + integrity sha512-BLbiRkiBzAwsjut4x/dsibSTB6yWpwT5qWmC2OfuCg3GgVQCSgMs4vEctYPhsaGtd0AeuuHMkjZ2h2WG8MSzRw== uglify-js@^3.1.4: version "3.5.2"