Repository: netlify/zip-it-and-ship-it
Pull request #299 (2021-01-11 19:56:32)

fix: match Next.js exact versions when ignoring critters
Follow up on https://github.com/netlify/zip-it-and-ship-it/pull/295

Current logic doesn't work for exact versions.

Not sure if there's a better way to do this.


Patch:
diff --git a/src/node_dependencies/nested.js b/src/node_dependencies/nested.js
index c1e11d7..b5e936c 100644
--- a/src/node_dependencies/nested.js
+++ b/src/node_dependencies/nested.js
@@ -1,4 +1,4 @@
-const { satisfies, validRange } = require('semver')
+const { valid: validVersion, validRange, satisfies, gte: greaterThanEqual } = require('semver')
 
 // Apply the Node.js module logic recursively on its own dependencies, using
 // the `package.json` `dependencies`, `peerDependencies` and
@@ -65,6 +65,8 @@ const isOptionalModule = function (
   )
 }
 
+const MIN_NEXT_VERSION = '10.0.4'
+
 // '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 = {} }) {
@@ -72,11 +74,15 @@ const isExternalCrittersModule = function (moduleName, { dependencies = {}, devD
     return false
   }
   const nextVersion = dependencies.next || devDependencies.next
-  if (!validRange(nextVersion)) {
-    return false
+
+  // can the declared Next.js version resolve to >=10.0.4 ?
+  // test exact versions
+  if (validVersion(nextVersion)) {
+    return greaterThanEqual(nextVersion, MIN_NEXT_VERSION)
   }
-  // can the declared Next.js version resolve to 10.0.4 ?
-  return satisfies('10.0.4', nextVersion)
+
+  // test ranges
+  return validRange(nextVersion) && satisfies(MIN_NEXT_VERSION, nextVersion)
 }
 
 module.exports = { getNestedDependencies, handleModuleNotFound }


Test patch:
diff --git a/tests/fixtures/node-module-next10-critters-exact/function/function.js b/tests/fixtures/node-module-next10-critters-exact/function/function.js
new file mode 100644
index 0000000..41d8840
--- /dev/null
+++ b/tests/fixtures/node-module-next10-critters-exact/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-exact/function/renderNextPage.js b/tests/fixtures/node-module-next10-critters-exact/function/renderNextPage.js
new file mode 100644
index 0000000..7fd8d6e
--- /dev/null
+++ b/tests/fixtures/node-module-next10-critters-exact/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-exact/node_modules/next/dist/next-server/server/render.js b/tests/fixtures/node-module-next10-critters-exact/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-exact/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-exact/node_modules/next/index.js b/tests/fixtures/node-module-next10-critters-exact/node_modules/next/index.js
new file mode 100644
index 0000000..33c1891
--- /dev/null
+++ b/tests/fixtures/node-module-next10-critters-exact/node_modules/next/index.js
@@ -0,0 +1,1 @@
+module.exports = true
diff --git a/tests/fixtures/node-module-next10-critters-exact/node_modules/next/package.json b/tests/fixtures/node-module-next10-critters-exact/node_modules/next/package.json
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/tests/fixtures/node-module-next10-critters-exact/node_modules/next/package.json
@@ -0,0 +1,1 @@
+{}
diff --git a/tests/fixtures/node-module-next10-critters-exact/package.json b/tests/fixtures/node-module-next10-critters-exact/package.json
new file mode 100644
index 0000000..5636547
--- /dev/null
+++ b/tests/fixtures/node-module-next10-critters-exact/package.json
@@ -0,0 +1,5 @@
+{
+  "dependencies": {
+    "next": "10.0.5"
+  }
+}
diff --git a/tests/main.js b/tests/main.js
index a219fb4..d87526c 100644
--- a/tests/main.js
+++ b/tests/main.js
@@ -147,6 +147,10 @@ test('Ignore missing critters dependency for Next.js 10', async (t) => {
   await zipNode(t, 'node-module-next10-critters')
 })
 
+test('Ignore missing critters dependency for Next.js exact version 10.0.5', async (t) => {
+  await zipNode(t, 'node-module-next10-critters-exact')
+})
+
 // We persist `package.json` as `package.json.txt` in git. Otherwise ESLint
 // tries to load when linting sibling JavaScript files. In this test, we
 // temporarily rename it to an actual `package.json`.