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

Commit

Permalink
fix(webpack): avoid being polluted by user configuration files
Browse files Browse the repository at this point in the history
  • Loading branch information
vvo committed Feb 10, 2021
1 parent 04dd8f3 commit 43c18b0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export function handler(event) {
- 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

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

## Why?

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:
Expand Down
29 changes: 23 additions & 6 deletions src/NodejsFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,15 @@ export class NodejsFunction extends lambda.Function {
loader: "${escapePathForNodeJs(pluginsPaths["babel-loader"])}",
options: {
cwd: "${escapePathForNodeJs(process.cwd())}",
cacheDirectory: true,
cacheDirectory: "${escapePathForNodeJs(
path.join(
process.cwd(),
"node_modules",
".cache",
"aws-lambda-nodejs-webpack",
"babel",
),
)}",
presets: [
[
"${escapePathForNodeJs(pluginsPaths["@babel/preset-env"])}",
Expand Down Expand Up @@ -210,7 +218,16 @@ export class NodejsFunction extends lambda.Function {
// 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)}"]
}
},
cacheDirectory: "${escapePathForNodeJs(
path.join(
process.cwd(),
"node_modules",
".cache",
"aws-lambda-nodejs-webpack",
"webpack",
),
)}"
},
optimization: {
splitChunks: {
Expand Down Expand Up @@ -253,9 +270,9 @@ export class NodejsFunction extends lambda.Function {
webpackBinPath,
["--config", webpackConfigPath],
{
// we force the CWD to aws-lambda-nodejs-webpack root, otherwise webpack-cli might resolve to the
// user's project version (https://github.com/webpack/webpack-cli/blob/master/packages/webpack-cli/bin/cli.js)
cwd: path.join(__dirname, ".."),
// we force CWD to the output dir to avoid being "polluted" by any babelrc or other configuration files
// that could mess up with our own webpack configuration. If you need to reuse your babelrc then please open an issue
cwd: outputDir,
},
);
// console.timeEnd("webpack");
Expand Down Expand Up @@ -298,7 +315,7 @@ function nodeMajorVersion(): number {
// otherwise they would be resolved to the user versions / undefined versions
function findModulePath(moduleName: string, pluginsPath: string) {
try {
return require.resolve(moduleName);
return require.resolve(moduleName, { paths: [__dirname] });
} catch (error) {
const modulePath = findUp.sync(moduleName, {
type: "directory",
Expand Down

0 comments on commit 43c18b0

Please sign in to comment.