From 5418e3c06abe7e50ff0182ad731232a05a6ba3ce Mon Sep 17 00:00:00 2001 From: Wojciech Maj Date: Fri, 28 Jun 2024 11:06:07 +0200 Subject: [PATCH 1/3] Remove `is-core-module` dependency Node.js from v6.13.0, v8.10.0, v9.3.0 includes `module.builtinModules` which we can use to natively check if some module belongs to Node.js core or not. This drops not one, but _three_ dependencies, removing 70 KB of bloat: https://npmgraph.js.org/?q=is-core-module --- package-lock.json | 1 - package.json | 1 - src/core/elementsInfo.js | 12 ++++++++++-- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6d80794..3101efc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,6 @@ "chalk": "4.1.2", "eslint-import-resolver-node": "0.3.9", "eslint-module-utils": "2.8.1", - "is-core-module": "2.13.1", "micromatch": "4.0.5" }, "devDependencies": { diff --git a/package.json b/package.json index 12100b7..5a88ff0 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,6 @@ "chalk": "4.1.2", "eslint-import-resolver-node": "0.3.9", "eslint-module-utils": "2.8.1", - "is-core-module": "2.13.1", "micromatch": "4.0.5" }, "devDependencies": { diff --git a/src/core/elementsInfo.js b/src/core/elementsInfo.js index 03f10c1..9385c94 100644 --- a/src/core/elementsInfo.js +++ b/src/core/elementsInfo.js @@ -1,4 +1,4 @@ -const isCoreModule = require("is-core-module"); +const mod = require("module"); const micromatch = require("micromatch"); const resolve = require("eslint-module-utils/resolve").default; @@ -9,6 +9,14 @@ const { isArray } = require("../helpers/utils"); const { filesCache, importsCache, elementsCache } = require("./cache"); +function isBuiltin(moduleName) { + const moduleNameWithoutPrefix = moduleName.startsWith("node:") + ? moduleName.slice(5) + : moduleName; + + return mod.builtinModules.includes(moduleNameWithoutPrefix); +} + function baseModule(name) { if (isScoped(name)) { const [scope, packageName] = name.split("/"); @@ -38,7 +46,7 @@ function isIgnored(path, settings) { function isBuiltIn(name, path) { if (path || !name) return false; const base = baseModule(name); - return isCoreModule(base); + return isBuiltin(base); } const scopedRegExp = /^@[^/]*\/?[^/]+/; From 78e9508887e2a62dd3ae0d277326ad47fea8f3cd Mon Sep 17 00:00:00 2001 From: Wojciech Maj Date: Fri, 28 Jun 2024 12:54:36 +0200 Subject: [PATCH 2/3] Fix name clash --- src/core/elementsInfo.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/elementsInfo.js b/src/core/elementsInfo.js index 9385c94..0ef71d5 100644 --- a/src/core/elementsInfo.js +++ b/src/core/elementsInfo.js @@ -9,7 +9,7 @@ const { isArray } = require("../helpers/utils"); const { filesCache, importsCache, elementsCache } = require("./cache"); -function isBuiltin(moduleName) { +function isCoreModule(moduleName) { const moduleNameWithoutPrefix = moduleName.startsWith("node:") ? moduleName.slice(5) : moduleName; @@ -46,7 +46,7 @@ function isIgnored(path, settings) { function isBuiltIn(name, path) { if (path || !name) return false; const base = baseModule(name); - return isBuiltin(base); + return isCoreModule(base); } const scopedRegExp = /^@[^/]*\/?[^/]+/; From 9b925795fcb5967bd135183b89a6e75b41059a7f Mon Sep 17 00:00:00 2001 From: Wojciech Maj Date: Tue, 2 Jul 2024 14:48:26 +0200 Subject: [PATCH 3/3] Add unit tests --- test/rules/one-level/element-types.spec.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/rules/one-level/element-types.spec.js b/test/rules/one-level/element-types.spec.js index 828d193..e666a86 100644 --- a/test/rules/one-level/element-types.spec.js +++ b/test/rules/one-level/element-types.spec.js @@ -166,6 +166,18 @@ const test = (settings, options, errorMessages) => { }, ], }, + // Can import fs module + { + filename: absoluteFilePath("modules/module-a/ModuleA.js"), + code: "import fs from 'fs'", + options, + }, + // Can import node:fs module + { + filename: absoluteFilePath("modules/module-a/ModuleA.js"), + code: "import fs from 'node:fs'", + options, + }, ], invalid: [ // Helpers can't import another if everything is disallowed