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

Commit 7fd2212

Browse files
committed
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.
1 parent 43c18b0 commit 7fd2212

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export function handler(event) {
6262
- TypeScript support
6363
- Babel support (preset-env)
6464
- babel & webpack caching
65-
- 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
65+
- node_modules are split into a single `vendor.js` file. Allowing you to debug your own code in AWS console most of the time
6666

6767
- ⚠️ 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.
6868

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"find-up": "5.0.0",
5353
"noop2": "2.0.0",
5454
"source-map-support": "0.5.19",
55-
"ts-loader": "8.0.16",
55+
"ts-loader": "8.0.17",
5656
"webpack": "5.21.2",
5757
"webpack-cli": "4.5.0"
5858
},
@@ -69,7 +69,7 @@
6969
"semantic-release-cli": "5.4.3",
7070
"tsdx": "0.14.1",
7171
"tslib": "2.1.0",
72-
"typescript": "4.1.4"
72+
"typescript": "4.1.5"
7373
},
7474
"peerDependencies": {
7575
"@aws-cdk/aws-lambda": "^1.54.0",

src/NodejsFunction.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export class NodejsFunction extends lambda.Function {
141141
142142
module.exports = {
143143
name: "aws-lambda-nodejs-webpack",
144-
mode: "production",
144+
mode: "none",
145145
entry: "${escapePathForNodeJs(entryFullPath)}",
146146
target: "node",
147147
resolve: {
@@ -159,6 +159,7 @@ export class NodejsFunction extends lambda.Function {
159159
use: {
160160
loader: "${escapePathForNodeJs(pluginsPaths["babel-loader"])}",
161161
options: {
162+
babelrc: false, // do not use babelrc when present (could pollute lambda configuration)
162163
cwd: "${escapePathForNodeJs(process.cwd())}",
163164
cacheDirectory: "${escapePathForNodeJs(
164165
path.join(
@@ -180,6 +181,7 @@ export class NodejsFunction extends lambda.Function {
180181
},
181182
loose: true,
182183
bugfixes: true,
184+
ignoreBrowserslistConfig: true // do not use browser list configuration, we build for node X that's it
183185
},
184186
]
185187
],
@@ -205,7 +207,18 @@ export class NodejsFunction extends lambda.Function {
205207
configFile: "${escapePathForNodeJs(
206208
path.join(process.cwd(), "tsconfig.json"),
207209
)}",
208-
transpileOnly: true
210+
transpileOnly: true,
211+
// from: https://www.npmjs.com/package/@tsconfig/node12
212+
compilerOptions: {
213+
lib: ["es2019", "es2020.promise", "es2020.bigint", "es2020.string"],
214+
module: "commonjs",
215+
target: "es2019",
216+
baseUrl: ".",
217+
strict: true,
218+
esModuleInterop: true,
219+
skipLibCheck: true,
220+
forceConsistentCasingInFileNames: true
221+
}
209222
}
210223
},
211224
exclude: /node_modules/,
@@ -215,9 +228,9 @@ export class NodejsFunction extends lambda.Function {
215228
cache: {
216229
type: "filesystem",
217230
buildDependencies: {
218-
// force the config file to be this current file, since it won't change over builds
219-
// while the temporary webpack config file used would change, thus disabling webpack cache
220-
config: ["${escapePathForNodeJs(__filename)}"]
231+
config: [__filename, "${escapePathForNodeJs(
232+
path.join(process.cwd(), "tsconfig.json"),
233+
)}"]
221234
},
222235
cacheDirectory: "${escapePathForNodeJs(
223236
path.join(
@@ -229,6 +242,9 @@ export class NodejsFunction extends lambda.Function {
229242
),
230243
)}"
231244
},
245+
// note: we specifically do not minify our code for Node.js
246+
// I have had horrible experience with code being minified for ES5 that would break on Node 12
247+
// If you have a good minifier that can minify to target Node 12 then open a PR
232248
optimization: {
233249
splitChunks: {
234250
cacheGroups: {
@@ -240,10 +256,6 @@ export class NodejsFunction extends lambda.Function {
240256
},
241257
},
242258
},
243-
minimize: true,
244-
minimizer: [new TerserPlugin({
245-
include: "vendor.js" // only minify vendor.js
246-
})],
247259
},
248260
externals: [...builtinModules, "aws-sdk"],
249261
output: {
@@ -265,7 +277,7 @@ export class NodejsFunction extends lambda.Function {
265277

266278
fs.writeFileSync(webpackConfigPath, webpackConfiguration);
267279

268-
// console.time("webpack");
280+
console.time("aws-lambda-nodejs-webpack");
269281
const webpack = spawn.sync(
270282
webpackBinPath,
271283
["--config", webpackConfigPath],
@@ -275,7 +287,7 @@ export class NodejsFunction extends lambda.Function {
275287
cwd: outputDir,
276288
},
277289
);
278-
// console.timeEnd("webpack");
290+
console.timeEnd("aws-lambda-nodejs-webpack");
279291

280292
if (webpack.status !== 0) {
281293
console.error(

yarn.lock

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10776,10 +10776,10 @@ ts-jest@^25.3.1:
1077610776
semver "6.x"
1077710777
yargs-parser "18.x"
1077810778

10779-
ts-loader@8.0.16:
10780-
version "8.0.16"
10781-
resolved "https://registry.yarnpkg.com/ts-loader/-/ts-loader-8.0.16.tgz#a60311f01f015518e1cfbb5698e6ca8830cd2391"
10782-
integrity sha512-Cr9ywsgg1n8cjGjIogHLPlqe3WJUHzuJaqwNo5I596KpIqekKzxvSENbrXeOypHcXSPPsr8hV6mglngyXvcKrg==
10779+
ts-loader@8.0.17:
10780+
version "8.0.17"
10781+
resolved "https://registry.yarnpkg.com/ts-loader/-/ts-loader-8.0.17.tgz#98f2ccff9130074f4079fd89b946b4c637b1f2fc"
10782+
integrity sha512-OeVfSshx6ot/TCxRwpBHQ/4lRzfgyTkvi7ghDVrLXOHzTbSK413ROgu/xNqM72i3AFeAIJgQy78FwSMKmOW68w==
1078310783
dependencies:
1078410784
chalk "^4.1.0"
1078510785
enhanced-resolve "^4.0.0"
@@ -10960,10 +10960,10 @@ typedarray@^0.0.6:
1096010960
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
1096110961
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=
1096210962

10963-
typescript@4.1.4:
10964-
version "4.1.4"
10965-
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.4.tgz#f058636e2f4f83f94ddaae07b20fd5e14598432f"
10966-
integrity sha512-+Uru0t8qIRgjuCpiSPpfGuhHecMllk5Zsazj5LZvVsEStEjmIRRBZe+jHjGQvsgS7M1wONy2PQXd67EMyV6acg==
10963+
typescript@4.1.5:
10964+
version "4.1.5"
10965+
resolved "https://registry.yarnpkg.com/typescript/-/typescript-4.1.5.tgz#123a3b214aaff3be32926f0d8f1f6e704eb89a72"
10966+
integrity sha512-6OSu9PTIzmn9TCDiovULTnET6BgXtDYL4Gg4szY+cGsc3JP1dQL8qvE8kShTRx1NIw4Q9IBHlwODjkjWEtMUyA==
1096710967

1096810968
typescript@^3.7.3:
1096910969
version "3.9.7"

0 commit comments

Comments
 (0)