Skip to content

Commit

Permalink
refactor: add additionalModuleDirs for the 'all' externals option
Browse files Browse the repository at this point in the history
  • Loading branch information
it-propulse committed Feb 11, 2022
1 parent 4be0e8f commit 01ee8d4
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 9 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 0 additions & 2 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
6 changes: 2 additions & 4 deletions src/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit 01ee8d4

Please sign in to comment.