From 7fd221237c1905864ad2abca7e208169893cbf76 Mon Sep 17 00:00:00 2001 From: Vincent Voyer Date: Thu, 11 Feb 2021 15:46:40 +0100 Subject: [PATCH] fix(targets): ensure we only transpile what's necessary 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. --- README.md | 2 +- package.json | 4 ++-- src/NodejsFunction.ts | 34 +++++++++++++++++++++++----------- yarn.lock | 16 ++++++++-------- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 9623131..1c00681 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/package.json b/package.json index d5ceb68..5f96a38 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -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", diff --git a/src/NodejsFunction.ts b/src/NodejsFunction.ts index 7a3ea03..267cb1c 100644 --- a/src/NodejsFunction.ts +++ b/src/NodejsFunction.ts @@ -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: { @@ -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( @@ -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 }, ] ], @@ -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/, @@ -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( @@ -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: { @@ -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: { @@ -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], @@ -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( diff --git a/yarn.lock b/yarn.lock index e1fec49..d79d284 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10776,10 +10776,10 @@ ts-jest@^25.3.1: semver "6.x" yargs-parser "18.x" -ts-loader@8.0.16: - version "8.0.16" - resolved "https://registry.yarnpkg.com/ts-loader/-/ts-loader-8.0.16.tgz#a60311f01f015518e1cfbb5698e6ca8830cd2391" - integrity sha512-Cr9ywsgg1n8cjGjIogHLPlqe3WJUHzuJaqwNo5I596KpIqekKzxvSENbrXeOypHcXSPPsr8hV6mglngyXvcKrg== +ts-loader@8.0.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" @@ -10960,10 +10960,10 @@ typedarray@^0.0.6: resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c= -typescript@4.1.4: - version "4.1.4" - resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.4.tgz#f058636e2f4f83f94ddaae07b20fd5e14598432f" - integrity sha512-+Uru0t8qIRgjuCpiSPpfGuhHecMllk5Zsazj5LZvVsEStEjmIRRBZe+jHjGQvsgS7M1wONy2PQXd67EMyV6acg== +typescript@4.1.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"