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

Commit 43c18b0

Browse files
committed
fix(webpack): avoid being polluted by user configuration files
1 parent 04dd8f3 commit 43c18b0

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ export function handler(event) {
6464
- babel & webpack caching
6565
- 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
6666

67+
- ⚠️ 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.
68+
6769
## Why?
6870

6971
There's already a dedicated [aws-lambda-nodejs module for CDK](https://docs.aws.amazon.com/cdk/api/latest/docs/aws-lambda-nodejs-readme.html) but I had two major issues with it:

src/NodejsFunction.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,15 @@ export class NodejsFunction extends lambda.Function {
160160
loader: "${escapePathForNodeJs(pluginsPaths["babel-loader"])}",
161161
options: {
162162
cwd: "${escapePathForNodeJs(process.cwd())}",
163-
cacheDirectory: true,
163+
cacheDirectory: "${escapePathForNodeJs(
164+
path.join(
165+
process.cwd(),
166+
"node_modules",
167+
".cache",
168+
"aws-lambda-nodejs-webpack",
169+
"babel",
170+
),
171+
)}",
164172
presets: [
165173
[
166174
"${escapePathForNodeJs(pluginsPaths["@babel/preset-env"])}",
@@ -210,7 +218,16 @@ export class NodejsFunction extends lambda.Function {
210218
// force the config file to be this current file, since it won't change over builds
211219
// while the temporary webpack config file used would change, thus disabling webpack cache
212220
config: ["${escapePathForNodeJs(__filename)}"]
213-
}
221+
},
222+
cacheDirectory: "${escapePathForNodeJs(
223+
path.join(
224+
process.cwd(),
225+
"node_modules",
226+
".cache",
227+
"aws-lambda-nodejs-webpack",
228+
"webpack",
229+
),
230+
)}"
214231
},
215232
optimization: {
216233
splitChunks: {
@@ -253,9 +270,9 @@ export class NodejsFunction extends lambda.Function {
253270
webpackBinPath,
254271
["--config", webpackConfigPath],
255272
{
256-
// we force the CWD to aws-lambda-nodejs-webpack root, otherwise webpack-cli might resolve to the
257-
// user's project version (https://github.com/webpack/webpack-cli/blob/master/packages/webpack-cli/bin/cli.js)
258-
cwd: path.join(__dirname, ".."),
273+
// we force CWD to the output dir to avoid being "polluted" by any babelrc or other configuration files
274+
// that could mess up with our own webpack configuration. If you need to reuse your babelrc then please open an issue
275+
cwd: outputDir,
259276
},
260277
);
261278
// console.timeEnd("webpack");
@@ -298,7 +315,7 @@ function nodeMajorVersion(): number {
298315
// otherwise they would be resolved to the user versions / undefined versions
299316
function findModulePath(moduleName: string, pluginsPath: string) {
300317
try {
301-
return require.resolve(moduleName);
318+
return require.resolve(moduleName, { paths: [__dirname] });
302319
} catch (error) {
303320
const modulePath = findUp.sync(moduleName, {
304321
type: "directory",

0 commit comments

Comments
 (0)