Skip to content
This repository has been archived by the owner on Nov 13, 2021. It is now read-only.

Commit

Permalink
fix(targets): ensure we only transpile what's necessary
Browse files Browse the repository at this point in the history
before this commit we were still reading from:
- user babelrc
- user TypeScript compilerOptions (while we need different ones)
- browserlists

before this commit we were trying to minize code down to ES5 which would code
weird issues with modules like Knex:

`Class constructor  cannot be invoked without 'new'`

Which resulted in heavily transpiled code while Node.js do not need so much
changes.

It's now a lot better. Enjoy.
  • Loading branch information
vvo committed Feb 11, 2021
1 parent 43c18b0 commit 7fd2212
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function handler(event) {
- TypeScript support
- Babel support (preset-env)
- babel & webpack caching
- node_modules are split into a single `vendor.js` file, minified. Allowing you to debug your own code in AWS console most of the time
- node_modules are split into a single `vendor.js` file. Allowing you to debug your own code in AWS console most of the time

- ⚠️ the only configuration file from your project that we will read is `tsconfig.json`. Other files won't be used. If you need to reuse part of your babel configuration, please open an issue with details on your usecase.

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"find-up": "5.0.0",
"noop2": "2.0.0",
"source-map-support": "0.5.19",
"ts-loader": "8.0.16",
"ts-loader": "8.0.17",
"webpack": "5.21.2",
"webpack-cli": "4.5.0"
},
Expand All @@ -69,7 +69,7 @@
"semantic-release-cli": "5.4.3",
"tsdx": "0.14.1",
"tslib": "2.1.0",
"typescript": "4.1.4"
"typescript": "4.1.5"
},
"peerDependencies": {
"@aws-cdk/aws-lambda": "^1.54.0",
Expand Down
34 changes: 23 additions & 11 deletions src/NodejsFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class NodejsFunction extends lambda.Function {
module.exports = {
name: "aws-lambda-nodejs-webpack",
mode: "production",
mode: "none",
entry: "${escapePathForNodeJs(entryFullPath)}",
target: "node",
resolve: {
Expand All @@ -159,6 +159,7 @@ export class NodejsFunction extends lambda.Function {
use: {
loader: "${escapePathForNodeJs(pluginsPaths["babel-loader"])}",
options: {
babelrc: false, // do not use babelrc when present (could pollute lambda configuration)
cwd: "${escapePathForNodeJs(process.cwd())}",
cacheDirectory: "${escapePathForNodeJs(
path.join(
Expand All @@ -180,6 +181,7 @@ export class NodejsFunction extends lambda.Function {
},
loose: true,
bugfixes: true,
ignoreBrowserslistConfig: true // do not use browser list configuration, we build for node X that's it
},
]
],
Expand All @@ -205,7 +207,18 @@ export class NodejsFunction extends lambda.Function {
configFile: "${escapePathForNodeJs(
path.join(process.cwd(), "tsconfig.json"),
)}",
transpileOnly: true
transpileOnly: true,
// from: https://www.npmjs.com/package/@tsconfig/node12
compilerOptions: {
lib: ["es2019", "es2020.promise", "es2020.bigint", "es2020.string"],
module: "commonjs",
target: "es2019",
baseUrl: ".",
strict: true,
esModuleInterop: true,
skipLibCheck: true,
forceConsistentCasingInFileNames: true
}
}
},
exclude: /node_modules/,
Expand All @@ -215,9 +228,9 @@ export class NodejsFunction extends lambda.Function {
cache: {
type: "filesystem",
buildDependencies: {
// force the config file to be this current file, since it won't change over builds
// while the temporary webpack config file used would change, thus disabling webpack cache
config: ["${escapePathForNodeJs(__filename)}"]
config: [__filename, "${escapePathForNodeJs(
path.join(process.cwd(), "tsconfig.json"),
)}"]
},
cacheDirectory: "${escapePathForNodeJs(
path.join(
Expand All @@ -229,6 +242,9 @@ export class NodejsFunction extends lambda.Function {
),
)}"
},
// note: we specifically do not minify our code for Node.js
// I have had horrible experience with code being minified for ES5 that would break on Node 12
// If you have a good minifier that can minify to target Node 12 then open a PR
optimization: {
splitChunks: {
cacheGroups: {
Expand All @@ -240,10 +256,6 @@ export class NodejsFunction extends lambda.Function {
},
},
},
minimize: true,
minimizer: [new TerserPlugin({
include: "vendor.js" // only minify vendor.js
})],
},
externals: [...builtinModules, "aws-sdk"],
output: {
Expand All @@ -265,7 +277,7 @@ export class NodejsFunction extends lambda.Function {

fs.writeFileSync(webpackConfigPath, webpackConfiguration);

// console.time("webpack");
console.time("aws-lambda-nodejs-webpack");
const webpack = spawn.sync(
webpackBinPath,
["--config", webpackConfigPath],
Expand All @@ -275,7 +287,7 @@ export class NodejsFunction extends lambda.Function {
cwd: outputDir,
},
);
// console.timeEnd("webpack");
console.timeEnd("aws-lambda-nodejs-webpack");

if (webpack.status !== 0) {
console.error(
Expand Down
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10776,10 +10776,10 @@ ts-jest@^25.3.1:
semver "6.x"
yargs-parser "18.x"

[email protected].16:
version "8.0.16"
resolved "https://registry.yarnpkg.com/ts-loader/-/ts-loader-8.0.16.tgz#a60311f01f015518e1cfbb5698e6ca8830cd2391"
integrity sha512-Cr9ywsgg1n8cjGjIogHLPlqe3WJUHzuJaqwNo5I596KpIqekKzxvSENbrXeOypHcXSPPsr8hV6mglngyXvcKrg==
[email protected].17:
version "8.0.17"
resolved "https://registry.yarnpkg.com/ts-loader/-/ts-loader-8.0.17.tgz#98f2ccff9130074f4079fd89b946b4c637b1f2fc"
integrity sha512-OeVfSshx6ot/TCxRwpBHQ/4lRzfgyTkvi7ghDVrLXOHzTbSK413ROgu/xNqM72i3AFeAIJgQy78FwSMKmOW68w==
dependencies:
chalk "^4.1.0"
enhanced-resolve "^4.0.0"
Expand Down Expand Up @@ -10960,10 +10960,10 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

[email protected].4:
version "4.1.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.4.tgz#f058636e2f4f83f94ddaae07b20fd5e14598432f"
integrity sha512-+Uru0t8qIRgjuCpiSPpfGuhHecMllk5Zsazj5LZvVsEStEjmIRRBZe+jHjGQvsgS7M1wONy2PQXd67EMyV6acg==
[email protected].5:
version "4.1.5"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.5.tgz#123a3b214aaff3be32926f0d8f1f6e704eb89a72"
integrity sha512-6OSu9PTIzmn9TCDiovULTnET6BgXtDYL4Gg4szY+cGsc3JP1dQL8qvE8kShTRx1NIw4Q9IBHlwODjkjWEtMUyA==

typescript@^3.7.3:
version "3.9.7"
Expand Down

0 comments on commit 7fd2212

Please sign in to comment.