Repository: netlify/zip-it-and-ship-it
Pull request #307 (2021-01-26 14:09:29)

fix: add missing use case for matching critter dependecy
Fixes https://github.com/netlify/next-on-netlify/issues/157

This is another use case I missed in the original PR


Patch:
diff --git a/src/node_dependencies/nested.js b/src/node_dependencies/nested.js
index b5e936c..f0252c5 100644
--- a/src/node_dependencies/nested.js
+++ b/src/node_dependencies/nested.js
@@ -1,4 +1,4 @@
-const { valid: validVersion, validRange, satisfies, gte: greaterThanEqual } = require('semver')
+const { valid: validVersion, validRange, satisfies, gte: greaterThanEqual, ltr: lessThanRange } = require('semver')
 
 // Apply the Node.js module logic recursively on its own dependencies, using
 // the `package.json` `dependencies`, `peerDependencies` and
@@ -67,6 +67,9 @@ const isOptionalModule = function (
 
 const MIN_NEXT_VERSION = '10.0.4'
 
+const satisfiesRange = (range) =>
+  validRange(range) && (satisfies(MIN_NEXT_VERSION, range) || lessThanRange(MIN_NEXT_VERSION, range))
+
 // 'critters' is used only in Next.js >= 10.0.4 when enabling an experimental option and has to be installed manually
 // we ignore it if it's missing
 const isExternalCrittersModule = function (moduleName, { dependencies = {}, devDependencies = {} }) {
@@ -81,8 +84,7 @@ const isExternalCrittersModule = function (moduleName, { dependencies = {}, devD
     return greaterThanEqual(nextVersion, MIN_NEXT_VERSION)
   }
 
-  // test ranges
-  return validRange(nextVersion) && satisfies(MIN_NEXT_VERSION, nextVersion)
+  return satisfiesRange(nextVersion)
 }
 
 module.exports = { getNestedDependencies, handleModuleNotFound }


Test patch:
diff --git a/tests/fixtures/node-module-next10-critters-10.0.5-range/function/function.js b/tests/fixtures/node-module-next10-critters-10.0.5-range/function/function.js
new file mode 100644
index 0000000..41d8840
--- /dev/null
+++ b/tests/fixtures/node-module-next10-critters-10.0.5-range/function/function.js
@@ -0,0 +1,4 @@
+// eslint-disable-next-line import/no-unassigned-import
+require('./renderNextPage')
+
+module.exports = true
diff --git a/tests/fixtures/node-module-next10-critters-10.0.5-range/function/renderNextPage.js b/tests/fixtures/node-module-next10-critters-10.0.5-range/function/renderNextPage.js
new file mode 100644
index 0000000..7fd8d6e
--- /dev/null
+++ b/tests/fixtures/node-module-next10-critters-10.0.5-range/function/renderNextPage.js
@@ -0,0 +1,6 @@
+/* eslint-disable unicorn/filename-case */
+// eslint-disable-next-line import/no-unassigned-import
+require('next/dist/next-server/server/render.js')
+
+module.exports = true
+/* eslint-enable unicorn/filename-case */
diff --git a/tests/fixtures/node-module-next10-critters-10.0.5-range/node_modules/next/dist/next-server/server/render.js b/tests/fixtures/node-module-next10-critters-10.0.5-range/node_modules/next/dist/next-server/server/render.js
new file mode 100644
index 0000000..6480be1
--- /dev/null
+++ b/tests/fixtures/node-module-next10-critters-10.0.5-range/node_modules/next/dist/next-server/server/render.js
@@ -0,0 +1,8 @@
+const render = () => {
+  if (renderOpts.optimizeCss) {
+    // eslint-disable-next-line node/no-extraneous-require
+    require('critters')
+  }
+}
+
+module.exports = render
diff --git a/tests/fixtures/node-module-next10-critters-10.0.5-range/node_modules/next/index.js b/tests/fixtures/node-module-next10-critters-10.0.5-range/node_modules/next/index.js
new file mode 100644
index 0000000..33c1891
--- /dev/null
+++ b/tests/fixtures/node-module-next10-critters-10.0.5-range/node_modules/next/index.js
@@ -0,0 +1,1 @@
+module.exports = true
diff --git a/tests/fixtures/node-module-next10-critters-10.0.5-range/node_modules/next/package.json b/tests/fixtures/node-module-next10-critters-10.0.5-range/node_modules/next/package.json
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/tests/fixtures/node-module-next10-critters-10.0.5-range/node_modules/next/package.json
@@ -0,0 +1,1 @@
+{}
diff --git a/tests/fixtures/node-module-next10-critters-10.0.5-range/package.json b/tests/fixtures/node-module-next10-critters-10.0.5-range/package.json
new file mode 100644
index 0000000..74584d2
--- /dev/null
+++ b/tests/fixtures/node-module-next10-critters-10.0.5-range/package.json
@@ -0,0 +1,5 @@
+{
+  "dependencies": {
+    "next": "^10.0.5"
+  }
+}
diff --git a/tests/main.js b/tests/main.js
index 79a1e0a..7da52a1 100644
--- a/tests/main.js
+++ b/tests/main.js
@@ -151,6 +151,10 @@ test('Ignore missing critters dependency for Next.js exact version 10.0.5', asyn
   await zipNode(t, 'node-module-next10-critters-exact')
 })
 
+test('Ignore missing critters dependency for Next.js with range ^10.0.5', async (t) => {
+  await zipNode(t, 'node-module-next10-critters-10.0.5-range')
+})
+
 test('Resolves dependencies from .netlify/plugins/node_modules', async (t) => {
   await zipNode(t, 'node-module-next-image')
 })