Repository: netlify/zip-it-and-ship-it
Pull request #538 (2021-06-17 11:27:06)

fix: add aws-sdk to ignored_node_modules
**- Summary**

The `aws-sdk` Node module is a special case. Because the module is injected automatically in the AWS Lambda environment, we want to leave any `require`/`import` calls untouched, but also we don't want to include its supporting files in the bundle.

This behaviour is being respected when using the ZISI bundler, but not with esbuild. We missed this even though we have a test specifically targeting this case, because the assertion was simply checking that `aws-sdk` was not part of the function's `node_modules`:

```js
t.false(await pathExists(`${tmpDir}/src/node_modules/aws-sdk`))
```

This is sufficient for ZISI, but not for esbuild, since the bundled modules will be inlined into the main file anyway.

This PR adds `aws-sdk` to the list of ignored modules and verifies that the module doesn't exist in the generated bundle by trying to `require` it at runtime.

**- Test plan**

Adjusted an existing test.

**- A picture of a cute animal (not mandatory but encouraged)**

![2020-Animal-Kingdom-Baby-Zebra](https://user-images.githubusercontent.com/4162329/122387879-3b652e80-cf67-11eb-916c-c9fc6813ad9f.jpg)



github-actions[bot]: ## ⏱ Benchmark results

*Comparing with 508721c3c2e3af2d672da86172686aa2e4e0776c*

### largeDepsEsbuild: 13s
 ⬆️ **4.34% increase** vs. 508721c3c2e3af2d672da86172686aa2e4e0776c
```
^                                                                                                   13s   
│                                                                                  12.1s   12.5s    ┌──┐  
│                   12s     12s                            11.5s   11.6s            ┌──┐    ┌──┐    |▒▒|  
│                   ┌──┐    ┌──┐           11.4s   11.3s    ┌──┐    ┌──┐            |  |    |  |    |▒▒|  
│                   |  |    |  |            ┌──┐    ┌──┐    |  |    |  |            |  |    |  |    |▒▒|  
│   9.9s    10s     |  |    |  |    9.7s    |  |    |  |    |  |    |  |    9.6s    |  |    |  |    |▒▒|  
│   ┌──┐    ┌──┐    |  |    |  |    ┌──┐    |  |    |  |    |  |    |  |    ┌──┐    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
└───┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴──>
    T-12    T-11    T-10    T-9     T-8     T-7     T-6     T-5     T-4     T-3     T-2     T-1      T    
```
<details>
<summary>Legend</summary>

- T-30 (508721c3c2e3af2d672da86172686aa2e4e0776c): 12.5s
- T-29 (790010f035f121286c189ac88cf49e5a51de7a1b): 10.6s
- T-28 (0dfcc70a334d09523dcb127e1812d0dbbda7481b): 12s
- T-27 (c4c746323af0ffa3706c6136675aebbac2af1e3d): 10.3s
- T-26 (f1363d24abeeeb11abaed0c564ca5ae8874a70ae): 11.7s
- T-25 (a5cef00715188d477ba69f033c16de00cea768c0): 13s
- T-24 (febeffcae56016e74348c4380fd5572edcbe84d0): 12.5s
- T-23 (991d9c3ab2e4e4da188024e3e005601ce9f14896): 12.3s
- T-22 (dd7dd81e5bf63586018d0ef384c399b2c1ba21c4): 11.6s
- T-21 (d4bcaf0ab1ab6f984d4658da105936a21011fe66): 12s
- T-20 (92f64a67160ac9eb0301ae738e38d258117f2b06): 10.3s
- T-19 (06bc4f69b05c96899a1efbd953c560219291b568): 11.6s
- T-18 (6c0e5f1059d5e672ea5ee6509150e0896030e745): 12.1s
- T-17 (e4c22192f1f66aae395b00fe8eaf4c886781d723): 11.4s
- T-16 (3ba92859e8fbf0f20abc46912284575c24e18124): 12s
- T-15 (5cdd4d23e88300d026f0152222136a35849c3b7f): 11.5s
- T-14 (df7f3e92e9547649a4c7c0b25d6e317af292c089): 12.7s
- T-13 (08f2a5d49a78e4424bc7478aa3cc8c98d21c5ee7): 9.9s
- T-12 (1e5ba0b8519f8ab643496979d7304d68d749d31e): 9.9s
- T-11 (1507586389e536db74867e3e8d3a7145373adfd3): 10s
- T-10 (6f4ab2998291fc3dd9dfa2d48d38be4d5ab8a901): 12s
- T-9 (d28f357f9dc98d7a0b278b99fee81daaa1e2c434): 12s
- T-8 (0687ba92a59971f75e554b70b407a85540f35616): 9.7s
- T-7 (98d18d9aa251f7f5758f55b3da27ccf066f98cc0): 11.4s
- T-6 (3de38a4f1d3c5c4b64c87a47926926a7812319e4): 11.3s
- T-5 (ab9f1d536555739628655ad674d4c0452ca47218): 11.5s
- T-4 (b248dfa11282ff23bd19001aa1ff66bd38a93d0b): 11.6s
- T-3 (c296d1706906a5d7b820cd7fb625c06453503f0e): 9.6s
- T-2 (62d7be931cf51cfb5cf5dba60a463678d69af4ce): 12.1s
- T-1 (8392f530c234ed8fa2881bad5c1ad7c937d7b588): 12.5s
- **T (current commit): 13s**</details>
### largeDepsZisi: 1m 7.4s
 ⬆️ **6.35% increase** vs. 508721c3c2e3af2d672da86172686aa2e4e0776c
```
^                                                                                         1m 9.7s         
│                 1m 5.4s 1m 5.9s                                                 1m 5.5s   ┌──┐  1m 7.4s 
│                   ┌──┐    ┌──┐                                  1m 1.8s           ┌──┐    |  |    ┌──┐  
│                   |  |    |  |          1m 0.6s 1m 0.1s 1m 0.3s   ┌──┐            |  |    |  |    |▒▒|  
│          54.8s    |  |    |  |            ┌──┐    ┌──┐    ┌──┐    |  |            |  |    |  |    |▒▒|  
│  52.8s    ┌──┐    |  |    |  |   52.2s    |  |    |  |    |  |    |  |   51.8s    |  |    |  |    |▒▒|  
│   ┌──┐    |  |    |  |    |  |    ┌──┐    |  |    |  |    |  |    |  |    ┌──┐    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
│   |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |  |    |▒▒|  
└───┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴────┴──┴──>
    T-12    T-11    T-10    T-9     T-8     T-7     T-6     T-5     T-4     T-3     T-2     T-1      T    
```
<details>
<summary>Legend</summary>

- T-30 (508721c3c2e3af2d672da86172686aa2e4e0776c): 1m 3.1s
- T-29 (790010f035f121286c189ac88cf49e5a51de7a1b): 54s
- T-28 (0dfcc70a334d09523dcb127e1812d0dbbda7481b): 1m 3.9s
- T-27 (c4c746323af0ffa3706c6136675aebbac2af1e3d): 54.3s
- T-26 (f1363d24abeeeb11abaed0c564ca5ae8874a70ae): 59.5s
- T-25 (a5cef00715188d477ba69f033c16de00cea768c0): 1m 9.5s
- T-24 (febeffcae56016e74348c4380fd5572edcbe84d0): 1m 3.7s
- T-23 (991d9c3ab2e4e4da188024e3e005601ce9f14896): 1m 2.9s
- T-22 (dd7dd81e5bf63586018d0ef384c399b2c1ba21c4): 59.5s
- T-21 (d4bcaf0ab1ab6f984d4658da105936a21011fe66): 1m 7.1s
- T-20 (92f64a67160ac9eb0301ae738e38d258117f2b06): 54.7s
- T-19 (06bc4f69b05c96899a1efbd953c560219291b568): 1m 4.4s
- T-18 (6c0e5f1059d5e672ea5ee6509150e0896030e745): 1m 3.6s
- T-17 (e4c22192f1f66aae395b00fe8eaf4c886781d723): 59.3s
- T-16 (3ba92859e8fbf0f20abc46912284575c24e18124): 1m 2.7s
- T-15 (5cdd4d23e88300d026f0152222136a35849c3b7f): 1m 2.4s
- T-14 (df7f3e92e9547649a4c7c0b25d6e317af292c089): 1m 6.3s
- T-13 (08f2a5d49a78e4424bc7478aa3cc8c98d21c5ee7): 52.2s
- T-12 (1e5ba0b8519f8ab643496979d7304d68d749d31e): 52.8s
- T-11 (1507586389e536db74867e3e8d3a7145373adfd3): 54.8s
- T-10 (6f4ab2998291fc3dd9dfa2d48d38be4d5ab8a901): 1m 5.4s
- T-9 (d28f357f9dc98d7a0b278b99fee81daaa1e2c434): 1m 5.9s
- T-8 (0687ba92a59971f75e554b70b407a85540f35616): 52.2s
- T-7 (98d18d9aa251f7f5758f55b3da27ccf066f98cc0): 1m 0.6s
- T-6 (3de38a4f1d3c5c4b64c87a47926926a7812319e4): 1m 0.1s
- T-5 (ab9f1d536555739628655ad674d4c0452ca47218): 1m 0.3s
- T-4 (b248dfa11282ff23bd19001aa1ff66bd38a93d0b): 1m 1.8s
- T-3 (c296d1706906a5d7b820cd7fb625c06453503f0e): 51.8s
- T-2 (62d7be931cf51cfb5cf5dba60a463678d69af4ce): 1m 5.5s
- T-1 (8392f530c234ed8fa2881bad5c1ad7c937d7b588): 1m 9.7s
- **T (current commit): 1m 7.4s**</details>
<!--delta:benchmark@{}-->

Patch:
diff --git a/src/node_dependencies/special_cases.js b/src/node_dependencies/special_cases.js
index 5454436..f90ac30 100644
--- a/src/node_dependencies/special_cases.js
+++ b/src/node_dependencies/special_cases.js
@@ -1,7 +1,7 @@
 const { getPackageJson } = require('./package_json')
 
 const EXTERNAL_MODULES = ['@prisma/client']
-const IGNORED_MODULES = []
+const IGNORED_MODULES = ['aws-sdk']
 
 const getPackageJsonIfAvailable = async (srcDir) => {
   try {


Test patch:
diff --git a/tests/fixtures/node-module-excluded/function.js b/tests/fixtures/node-module-excluded/function.js
index e63d25c..866120a 100644
--- a/tests/fixtures/node-module-excluded/function.js
+++ b/tests/fixtures/node-module-excluded/function.js
@@ -1,6 +1,6 @@
-try {
-  // eslint-disable-next-line import/no-unassigned-import, import/no-unresolved, node/global-require
-  require('aws-sdk')
-} catch (error) {}
+module.exports = () => {
+  // eslint-disable-next-line node/global-require, node/no-extraneous-require
+  const AWS = require('aws-sdk')
 
-module.exports = true
+  return AWS
+}
diff --git a/tests/fixtures/node-module-excluded/node_modules/aws-sdk/index.js b/tests/fixtures/node-module-excluded/node_modules/aws-sdk/index.js
new file mode 100644
index 0000000..11ca588
--- /dev/null
+++ b/tests/fixtures/node-module-excluded/node_modules/aws-sdk/index.js
@@ -0,0 +1,1 @@
+module.exports = 'fake module'
diff --git a/tests/fixtures/node-module-excluded/node_modules/aws-sdk/package.json b/tests/fixtures/node-module-excluded/node_modules/aws-sdk/package.json
new file mode 100644
index 0000000..0d514dc
--- /dev/null
+++ b/tests/fixtures/node-module-excluded/node_modules/aws-sdk/package.json
@@ -0,0 +1,5 @@
+{
+  "name": "aws-sdk",
+  "version": "1.0.0",
+  "main": "index.js"
+}
diff --git a/tests/main.js b/tests/main.js
index f9aaa38..96785a7 100644
--- a/tests/main.js
+++ b/tests/main.js
@@ -150,7 +150,19 @@ testBundlers('Can require dynamically generated node modules', [ESBUILD, ESBUILD
 
 testBundlers('Ignore some excluded node modules', [ESBUILD, ESBUILD_ZISI, DEFAULT], async (bundler, t) => {
   const { tmpDir } = await zipNode(t, 'node-module-excluded', { opts: { config: { '*': { nodeBundler: bundler } } } })
+
   t.false(await pathExists(`${tmpDir}/src/node_modules/aws-sdk`))
+
+  try {
+    // eslint-disable-next-line import/no-dynamic-require, node/global-require
+    const func = require(`${tmpDir}/function.js`)
+
+    func()
+
+    t.fail('Running the function should fail due to the missing module')
+  } catch (error) {
+    t.is(error.code, 'MODULE_NOT_FOUND')
+  }
 })
 
 testBundlers('Ignore TypeScript types', [ESBUILD, ESBUILD_ZISI, DEFAULT], async (bundler, t) => {