diff --git a/README.md b/README.md index b7aa34a..9623131 100644 --- a/README.md +++ b/README.md @@ -54,12 +54,17 @@ export function handler(event) { ## Features +- webpack 5 - fast, [no-docker](https://github.com/aws/aws-cdk/issues/9120) CDK construct - lambda output only contains the necessary files, no README, tests, ... - bundling happens in temporary directories, it never writes in your project directory - source map support -- TypeScript support (⚠️ Beta, do provide feedback in issues!) +- 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 + +- ⚠️ 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? diff --git a/src/NodejsFunction.ts b/src/NodejsFunction.ts index 0973c01..7a3ea03 100644 --- a/src/NodejsFunction.ts +++ b/src/NodejsFunction.ts @@ -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"])}", @@ -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: { @@ -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"); @@ -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",