From 01ee8d406b0d1bae427b5f9e272934779355cfc8 Mon Sep 17 00:00:00 2001 From: Maxime Date: Fri, 11 Feb 2022 10:51:31 +0100 Subject: [PATCH] refactor: add additionalModuleDirs for the 'all' externals option --- README.md | 4 +--- src/config.js | 2 -- src/webpack.config.js | 6 ++---- 3 files changed, 3 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index cccddc14..59cd3842 100644 --- a/README.md +++ b/README.md @@ -412,14 +412,12 @@ custom: ### Externals -The `externals` option takes a list of packages, `all`, or `all-monorepo`. By default this is set to `["knex", "sharp"]`. +The `externals` option takes a list of packages or `all`. By default this is set to `["knex", "sharp"]`. Packages listed in `externals` are ignored by Webpack. They are instead added in the `node_modules/` directory of the Lambda .zip file. These usually include npm packages that are not supported by Webpack. The `all` option allows you to list all the packages in YOUR `node_modules/` directory as externals. This might be useful in cases where they are just too many to list. Or you are using something like [EJS](https://ejs.co) that implicitly requires a long list of packages that are not supported by Webpack. -The `all-monorepo` is useful when using Yarn workspaces (monorepo). It does the same as the `all` option, but also lists all the packages in the root node_modules directory (`../../node_modules/`) as externals. - Note that, adding a package to the `externals` list might make your Lambda .zip file larger. This is because the entire package directory is zipped. Instead of using Webpack to just include the code that is necessary. So it's advisable to avoid using the `all` option. If you think we should add to the default list of externals, open a PR. diff --git a/src/config.js b/src/config.js index dd99a70b..0fb44dc7 100644 --- a/src/config.js +++ b/src/config.js @@ -26,8 +26,6 @@ module.exports = { // Set non Webpack compatible packages as externals // Or if we want to exclude all packages in the node_modules: // externals: "all" - // Or if we want to exclude all packages in the root node_modules and the cwd node_modules (monorepo): - // externals: "all-monorepo" externals: ["knex", "sharp"], // Set default file extensions to use the raw-loader with rawFileExtensions: ["pem", "txt"], diff --git a/src/webpack.config.js b/src/webpack.config.js index 2cb427f0..0db76929 100644 --- a/src/webpack.config.js +++ b/src/webpack.config.js @@ -54,10 +54,8 @@ const ENABLE_CACHING = isLocal ? config.options.caching : false; // And add the forceExclude packages to it because they shouldn't be Webpacked const computedExternals = ( externals === "all" - ? [nodeExternals()] - : externals === "all-monorepo" - ? [nodeExternals({ additionalModuleDirs: [path.resolve(process.cwd(), "../../node_modules")] })] - : externals + ? [nodeExternals({ additionalModuleDirs: [path.resolve(process.cwd(), "../../node_modules")] })] + : externals ).concat(forceExclude); const extensions = [