From dd3af266a4e028827f318ef641e1e4e970e94a32 Mon Sep 17 00:00:00 2001 From: Maxime Date: Fri, 7 Jan 2022 15:14:32 +0100 Subject: [PATCH 1/8] feat: handle externals all-monorepo option --- src/webpack.config.js | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/webpack.config.js b/src/webpack.config.js index c08f42f7..98470fcf 100644 --- a/src/webpack.config.js +++ b/src/webpack.config.js @@ -50,11 +50,22 @@ const ENABLE_TSCHECKER = !config.options.disableForkTsChecker; const GENERATE_STATS_FILES = config.options.generateStatsFiles; const ENABLE_CACHING = isLocal ? config.options.caching : false; -// Handle the "all" option in externals +// Handle "all" and "all-monorepo" option in externals +function computedExternals() { + switch(externals) { + case "all": + return [nodeExternals()]; + + case "all-monorepo": + return [nodeExternals({ additionalModuleDirs: [path.resolve(process.cwd(), "../../node_modules")] })]; + + default: + return externals; + } +} + // And add the forceExclude packages to it because they shouldn't be Webpacked -const computedExternals = ( - externals === "all" ? [nodeExternals()] : externals -).concat(forceExclude); +const computedExternals = computedExternals().concat(forceExclude); const extensions = [ ".wasm", From 5636f2b0adae208092b0bb0b2a5bbdc52b4d2fff Mon Sep 17 00:00:00 2001 From: Maxime Date: Fri, 7 Jan 2022 15:15:05 +0100 Subject: [PATCH 2/8] style: add comments in config.js --- src/config.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/config.js b/src/config.js index 0fb44dc7..dd99a70b 100644 --- a/src/config.js +++ b/src/config.js @@ -26,6 +26,8 @@ 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"], From a2e386bd2fe9ba7a5eb7eed45508a5e17f95c4a4 Mon Sep 17 00:00:00 2001 From: Maxime Date: Fri, 7 Jan 2022 15:16:43 +0100 Subject: [PATCH 3/8] docs: update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 59cd3842..cccddc14 100644 --- a/README.md +++ b/README.md @@ -412,12 +412,14 @@ custom: ### Externals -The `externals` option takes a list of packages or `all`. By default this is set to `["knex", "sharp"]`. +The `externals` option takes a list of packages, `all`, or `all-monorepo`. 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. From 7e4454727f914fe57ba297a2b2751ffaf2a084ab Mon Sep 17 00:00:00 2001 From: Maxime Date: Fri, 7 Jan 2022 15:24:12 +0100 Subject: [PATCH 4/8] fix: all-monorepo implementation --- src/webpack.config.js | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/webpack.config.js b/src/webpack.config.js index 98470fcf..368b19b0 100644 --- a/src/webpack.config.js +++ b/src/webpack.config.js @@ -51,21 +51,14 @@ const GENERATE_STATS_FILES = config.options.generateStatsFiles; const ENABLE_CACHING = isLocal ? config.options.caching : false; // Handle "all" and "all-monorepo" option in externals -function computedExternals() { - switch(externals) { - case "all": - return [nodeExternals()]; - - case "all-monorepo": - return [nodeExternals({ additionalModuleDirs: [path.resolve(process.cwd(), "../../node_modules")] })]; - - default: - return externals; - } -} - // And add the forceExclude packages to it because they shouldn't be Webpacked -const computedExternals = computedExternals().concat(forceExclude); +const computedExternals = ( + externals === "all" + ? [nodeExternals()] + : externals === "all-monorepo" + ? [nodeExternals({ additionalModuleDirs: [path.resolve(process.cwd(), "../../node_modules")] })] + : externals +).concat(forceExclude); const extensions = [ ".wasm", From 557555b1fedfd57ba76fe1f2e9ab4e72fd6dc31d Mon Sep 17 00:00:00 2001 From: Maxime Date: Fri, 11 Feb 2022 10:51:31 +0100 Subject: [PATCH 5/8] 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 368b19b0..003b040a 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 = [ From b002a4ee973840fb90ceff3138b44c362efd299c Mon Sep 17 00:00:00 2001 From: Maxime Date: Fri, 11 Feb 2022 10:52:53 +0100 Subject: [PATCH 6/8] style: comment --- src/webpack.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/webpack.config.js b/src/webpack.config.js index 003b040a..1b208675 100644 --- a/src/webpack.config.js +++ b/src/webpack.config.js @@ -50,7 +50,7 @@ const ENABLE_TSCHECKER = !config.options.disableForkTsChecker; const GENERATE_STATS_FILES = config.options.generateStatsFiles; const ENABLE_CACHING = isLocal ? config.options.caching : false; -// Handle "all" and "all-monorepo" option in externals +// Handle the "all" option in externals // And add the forceExclude packages to it because they shouldn't be Webpacked const computedExternals = ( externals === "all" From 4008e58b87cc6da77c80c82406b2de12e36d44c0 Mon Sep 17 00:00:00 2001 From: Maxime Date: Tue, 15 Feb 2022 11:37:46 +0100 Subject: [PATCH 7/8] fix: force exclude source-map-support --- src/config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config.js b/src/config.js index 0fb44dc7..ec14b773 100644 --- a/src/config.js +++ b/src/config.js @@ -21,7 +21,7 @@ module.exports = { generateStatsFiles: false, tsConfig: "tsconfig.json", // Exclude aws-sdk since it's available in the Lambda runtime - forceExclude: ["aws-sdk"], + forceExclude: ["aws-sdk", "source-map-support"], disableForkTsChecker: false, // Set non Webpack compatible packages as externals // Or if we want to exclude all packages in the node_modules: From c71572381a51b2d46807de3b246473b035c1316c Mon Sep 17 00:00:00 2001 From: Maxime Date: Tue, 15 Feb 2022 11:43:07 +0100 Subject: [PATCH 8/8] test: fix externals-all test --- src/config.js | 2 +- tests/externals-all/externals-all.test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/config.js b/src/config.js index ec14b773..0fb44dc7 100644 --- a/src/config.js +++ b/src/config.js @@ -21,7 +21,7 @@ module.exports = { generateStatsFiles: false, tsConfig: "tsconfig.json", // Exclude aws-sdk since it's available in the Lambda runtime - forceExclude: ["aws-sdk", "source-map-support"], + forceExclude: ["aws-sdk"], disableForkTsChecker: false, // Set non Webpack compatible packages as externals // Or if we want to exclude all packages in the node_modules: diff --git a/tests/externals-all/externals-all.test.js b/tests/externals-all/externals-all.test.js index 9d9c2d54..d1129e1e 100644 --- a/tests/externals-all/externals-all.test.js +++ b/tests/externals-all/externals-all.test.js @@ -16,5 +16,5 @@ test("externals all option", async () => { /* Ensure that array-first is packaged as a part of the "all" externals option */ - expect(result).toMatch(/Packing external modules: array-first@\^[\d\\.]+/); + expect(result).toMatch(/Packing external modules: source-map-support, array-first@\^[\d\\.]+/); });