From 28198d95c08d36f41eb7fe33c5c6758190e1384e Mon Sep 17 00:00:00 2001 From: David Anson Date: Mon, 13 Jan 2025 21:43:16 -0800 Subject: [PATCH] Stop using module.createRequire (problematic under webpack), replace resolveModule helper with corresponding (new) export from markdownlint. --- markdownlint-cli2.mjs | 7 +-- package.json | 3 +- resolve-module.mjs | 16 ------ test/resolve-module-test.mjs | 101 ----------------------------------- webworker/module-stub.cjs | 8 --- webworker/webpack.config.cjs | 1 - 6 files changed, 3 insertions(+), 133 deletions(-) delete mode 100644 resolve-module.mjs delete mode 100644 test/resolve-module-test.mjs delete mode 100644 webworker/module-stub.cjs diff --git a/markdownlint-cli2.mjs b/markdownlint-cli2.mjs index 6e287be..15c7518 100755 --- a/markdownlint-cli2.mjs +++ b/markdownlint-cli2.mjs @@ -2,20 +2,17 @@ // Imports import fsNode from "node:fs"; -import { createRequire } from "node:module"; -const dynamicRequire = createRequire(import.meta.url); import os from "node:os"; import pathDefault from "node:path"; const pathPosix = pathDefault.posix; import { pathToFileURL } from "node:url"; import { globby } from "globby"; import micromatch from "micromatch"; -import { applyFixes, getVersion } from "markdownlint"; +import { applyFixes, getVersion, resolveModule } from "markdownlint"; import { lint, extendConfig, readConfig } from "markdownlint/promise"; import { expandTildePath } from "markdownlint/helpers"; import appendToArray from "./append-to-array.mjs"; import mergeOptions from "./merge-options.mjs"; -import resolveModule from "./resolve-module.mjs"; import parsers from "./parsers/parsers.mjs"; import jsoncParse from "./parsers/jsonc-parse.mjs"; import yamlParse from "./parsers/yaml-parse.mjs"; @@ -78,7 +75,7 @@ const importModule = async (dirOrDirs, id, noImport) => { let moduleName = null; try { try { - moduleName = pathToFileURL(resolveModule(dynamicRequire, expandId, dirs)); + moduleName = pathToFileURL(resolveModule(expandId, dirs)); } catch (error) { errors.push(error); moduleName = diff --git a/package.json b/package.json index 6d2e2ee..00534d2 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "playwright-test": "playwright test --config ./webworker/playwright.config.mjs", "playwright-test-docker": "docker run --rm --volume $PWD:/home/workdir --workdir /home/workdir --ipc=host mcr.microsoft.com/playwright:v1.49.1 npm run playwright-test", "schema": "cpy ./node_modules/markdownlint/schema/markdownlint-config-schema.json ./schema --flat", - "test": "ava --timeout=1m test/append-to-array-test.mjs test/fs-mock-test.mjs test/fs-virtual-test.mjs test/markdownlint-cli2-test.mjs test/markdownlint-cli2-test-exec.mjs test/markdownlint-cli2-test-exports.mjs test/markdownlint-cli2-test-fs.mjs test/markdownlint-cli2-test-main.mjs test/merge-options-test.mjs test/resolve-module-test.mjs", + "test": "ava --timeout=1m test/append-to-array-test.mjs test/fs-mock-test.mjs test/fs-virtual-test.mjs test/markdownlint-cli2-test.mjs test/markdownlint-cli2-test-exec.mjs test/markdownlint-cli2-test-exports.mjs test/markdownlint-cli2-test-fs.mjs test/markdownlint-cli2-test-main.mjs test/merge-options-test.mjs", "test-cover": "c8 --100 npm test", "test-docker-hub-image": "VERSION=$(node -e \"process.stdout.write(require('./package.json').version)\") && docker image rm davidanson/markdownlint-cli2:v$VERSION davidanson/markdownlint-cli2:latest || true && docker run --rm -v $PWD:/workdir davidanson/markdownlint-cli2:v$VERSION \"*.md\" && docker run --rm -v $PWD:/workdir davidanson/markdownlint-cli2:latest \"*.md\"", "test-docker-hub-image-rules": "VERSION=$(node -e \"process.stdout.write(require('./package.json').version)\") && docker image rm davidanson/markdownlint-cli2-rules:v$VERSION davidanson/markdownlint-cli2-rules:latest || true && docker run --rm -v $PWD:/workdir davidanson/markdownlint-cli2-rules:v$VERSION \"*.md\" && docker run --rm -v $PWD:/workdir davidanson/markdownlint-cli2-rules:latest \"*.md\"", @@ -68,7 +68,6 @@ "parsers/jsonc-parse.mjs", "parsers/yaml-parse.mjs", "README.md", - "resolve-module.mjs", "schema/markdownlint-cli2-config-schema.json", "schema/markdownlint-config-schema.json", "schema/ValidatingConfiguration.md" diff --git a/resolve-module.mjs b/resolve-module.mjs deleted file mode 100644 index 13c87ac..0000000 --- a/resolve-module.mjs +++ /dev/null @@ -1,16 +0,0 @@ -// @ts-check - -/** - * Wrapper for calling Node's require.resolve with additional paths. - * @param {object} require Node's require implementation (or equivalent). - * @param {string} request Module path to require. - * @param {string[]} paths Paths to resolve module location from. - * @returns {string} Resolved file name. - */ -const resolveModule = (require, request, paths) => { - const resolvePaths = require.resolve.paths ? require.resolve.paths("") : []; - const allPaths = [ ...paths, ...resolvePaths ]; - return require.resolve(request, { "paths": allPaths }); -}; - -export default resolveModule; diff --git a/test/resolve-module-test.mjs b/test/resolve-module-test.mjs deleted file mode 100644 index 2db0f54..0000000 --- a/test/resolve-module-test.mjs +++ /dev/null @@ -1,101 +0,0 @@ -// @ts-check - -import test from "ava"; -import path from "node:path"; -import { __dirname } from "./esm-helpers.mjs"; -import resolve from "../resolve-module.mjs"; - -import { createRequire } from "node:module"; -const require = createRequire(import.meta.url); - -test("built-in module", (t) => { - t.plan(1); - t.deepEqual( - require.resolve("node:fs"), - resolve(require, "node:fs", [ __dirname(import.meta) ]) - ); -}); - -test("locally-installed module", (t) => { - t.plan(1); - t.deepEqual( - require.resolve("micromatch"), - resolve(require, "micromatch", [ __dirname(import.meta) ]) - ); -}); - -test("relative (to __dirname(import.meta)) path to module", (t) => { - t.plan(1); - t.deepEqual( - require.resolve("./customRules/node_modules/markdownlint-rule-sample-commonjs"), - resolve( - require, - "./customRules/node_modules/markdownlint-rule-sample-commonjs", - [ __dirname(import.meta) ] - ) - ); -}); - -test("module in alternate node_modules", (t) => { - t.plan(2); - t.throws( - // @ts-ignore - () => require.resolve("markdownlint-rule-sample-commonjs"), - { "code": "MODULE_NOT_FOUND" } - ); - t.deepEqual( - require.resolve("./customRules/node_modules/markdownlint-rule-sample-commonjs"), - resolve( - require, - "markdownlint-rule-sample-commonjs", - [ path.join(__dirname(import.meta), "customRules") ] - ) - ); -}); - -test("module in alternate node_modules and no require.resolve.paths", (t) => { - t.plan(2); - // @ts-ignore - delete require.resolve.paths; - t.throws( - // @ts-ignore - () => require.resolve("markdownlint-rule-sample-commonjs"), - { "code": "MODULE_NOT_FOUND" } - ); - t.deepEqual( - require.resolve("./customRules/node_modules/markdownlint-rule-sample-commonjs"), - resolve( - require, - "markdownlint-rule-sample-commonjs", - [ path.join(__dirname(import.meta), "customRules") ] - ) - ); -}); - -test("module local, relative, and in alternate node_modules", (t) => { - t.plan(3); - const dirs = [ - __dirname(import.meta), - path.join(__dirname(import.meta), "customRules") - ]; - t.deepEqual( - require.resolve("micromatch"), - resolve(require, "micromatch", dirs) - ); - t.deepEqual( - require.resolve("./customRules/node_modules/markdownlint-rule-sample-commonjs"), - resolve( - require, - "./customRules/node_modules/markdownlint-rule-sample-commonjs", - dirs - ) - ); - t.deepEqual( - require.resolve("./customRules/node_modules/markdownlint-rule-sample-commonjs"), - resolve( - require, - "markdownlint-rule-sample-commonjs", - dirs - ) - ); -}); diff --git a/webworker/module-stub.cjs b/webworker/module-stub.cjs deleted file mode 100644 index fec9487..0000000 --- a/webworker/module-stub.cjs +++ /dev/null @@ -1,8 +0,0 @@ -// @ts-check - -"use strict"; - -module.exports = { - // @ts-ignore - "createRequire": () => require -}; diff --git a/webworker/webpack.config.cjs b/webworker/webpack.config.cjs index 4b2c073..0b0a9d7 100644 --- a/webworker/webpack.config.cjs +++ b/webworker/webpack.config.cjs @@ -52,7 +52,6 @@ module.exports = { "fallback": { "buffer": false, "fs": false, - "module": require.resolve("./module-stub.cjs"), "os": require.resolve("./os-stub.cjs"), "path": require.resolve("path-browserify"), "process": require.resolve("./process-stub.cjs"),