Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: toggle features #217

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion src/Serverless.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ declare namespace Serverless {
functions: {
[key: string]: Serverless.Function
}
custom: {
[key: string]: any
}
package: Serverless.Package
getAllFunctions(): string[]
}
Expand Down Expand Up @@ -43,4 +46,4 @@ declare namespace Serverless {
interface PluginManager {
spawn(command: string): Promise<void>
}
}
}
68 changes: 42 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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...')
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down