diff --git a/demo/webpack.config.mjs b/demo/webpack.config.mjs index f8e6ecd4a..de05a6024 100644 --- a/demo/webpack.config.mjs +++ b/demo/webpack.config.mjs @@ -1,12 +1,9 @@ // @ts-check -import { createRequire } from "node:module"; -const require = createRequire(import.meta.url); import webpack from "webpack"; import TerserPlugin from "terser-webpack-plugin"; import { __dirname, importWithTypeJson } from "../test/esm-helpers.mjs"; const libraryPackageJson = await importWithTypeJson(import.meta, "../package.json"); -const nodeModulePrefixRe = /^node:/u; // eslint-disable-next-line jsdoc/require-jsdoc function config(options) { @@ -19,14 +16,6 @@ function config(options) { "markdown-it": "markdownit" }, "mode": mode, - "module": { - "rules": [ - { - "test": /\.[cm]?js$/, - "exclude": /node_modules/ - } - ] - }, "name": name, "optimization": optimization, "output": { @@ -38,25 +27,10 @@ function config(options) { "path": __dirname(import.meta) }, "plugins": [ - new webpack.NormalModuleReplacementPlugin( - nodeModulePrefixRe, - (resource) => { - const module = resource.request.replace(nodeModulePrefixRe, ""); - resource.request = module; - } - ), new webpack.BannerPlugin({ "banner": `${name} ${version} ${homepage} @license ${license}` }) ], - "resolve": { - "fallback": { - "fs": false, - "os": false, - "path": false, - "module": require.resolve("./module-stub.cjs") - } - }, "ignoreWarnings": [ { "message": /(asset|entrypoint) size limit/ diff --git a/lib/markdownlint.mjs b/lib/markdownlint.mjs index 6ff36222b..8ae980196 100644 --- a/lib/markdownlint.mjs +++ b/lib/markdownlint.mjs @@ -1,10 +1,8 @@ // @ts-check -import * as nodeFs from "node:fs"; -import { createRequire } from "node:module"; -const dynamicRequire = createRequire(import.meta.url); -import * as os from "node:os"; -import path from "node:path"; +// @ts-ignore +import { fs as nodeFs, module, os, path } from "#node-imports"; +const dynamicRequire = module.createRequire(import.meta.url); import { initialize as cacheInitialize } from "./cache.mjs"; import { version } from "./constants.mjs"; import rules from "./rules.mjs"; diff --git a/demo/module-stub.cjs b/lib/node-imports-browser-module.cjs similarity index 84% rename from demo/module-stub.cjs rename to lib/node-imports-browser-module.cjs index fec94871b..a48161571 100644 --- a/demo/module-stub.cjs +++ b/lib/node-imports-browser-module.cjs @@ -3,6 +3,5 @@ "use strict"; module.exports = { - // @ts-ignore "createRequire": () => require }; diff --git a/lib/node-imports-browser.mjs b/lib/node-imports-browser.mjs new file mode 100644 index 000000000..ecd8d01ff --- /dev/null +++ b/lib/node-imports-browser.mjs @@ -0,0 +1,22 @@ +// @ts-check + +const getError = () => new Error("Node APIs are not available in browser context."); +const throwForSync = () => { + throw getError(); +}; + +export const fs = { + "access": (path, callback) => callback(getError()), + "accessSync": throwForSync, + "readFile": (path, options, callback) => callback(getError()), + "readFileSync": throwForSync +}; + +export { default as module } from "./node-imports-browser-module.cjs"; + +export const os = {}; + +export const path = { + "dirname": throwForSync, + "resolve": throwForSync +}; diff --git a/lib/node-imports-node.mjs b/lib/node-imports-node.mjs new file mode 100644 index 000000000..694165776 --- /dev/null +++ b/lib/node-imports-node.mjs @@ -0,0 +1,14 @@ +// @ts-check + +import { access, accessSync, readFile, readFileSync } from "node:fs"; +export const fs = { access, accessSync, readFile, readFileSync }; + +import { createRequire } from "node:module"; +export const module = { createRequire }; + +import { EOL, homedir } from "node:os"; +export const os = { EOL, homedir }; + +// eslint-disable-next-line unicorn/import-style +import { dirname, resolve } from "node:path"; +export const path = { dirname, resolve }; diff --git a/package.json b/package.json index 09ffc7828..57466f9db 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,12 @@ "./style/prettier": "./style/prettier.json", "./style/relaxed": "./style/relaxed.json" }, + "imports": { + "#node-imports": { + "browser": "./lib/node-imports-browser.mjs", + "default": "./lib/node-imports-node.mjs" + } + }, "types": "./lib/types.d.mts", "author": "David Anson (https://dlaa.me/)", "license": "MIT",