Skip to content

Commit

Permalink
Add subpath imports "markdownlint-imports-browser" and "markdownlint-…
Browse files Browse the repository at this point in the history
…imports-node" for overriding "browser"/"default" behavior in bundlers like webpack.
  • Loading branch information
DavidAnson committed Dec 18, 2024
1 parent 8ad4698 commit baf4944
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
},
"imports": {
"#node-imports": {
"markdownlint-imports-browser": "./lib/node-imports-browser.mjs",
"markdownlint-imports-node": "./lib/node-imports-node.mjs",
"browser": "./lib/node-imports-browser.mjs",
"default": "./lib/node-imports-node.mjs"
}
Expand Down Expand Up @@ -99,6 +101,7 @@
"markdown-it-sub": "2.0.0",
"markdown-it-sup": "2.0.0",
"markdownlint-rule-extended-ascii": "0.1.0",
"nano-spawn": "0.2.0",
"npm-run-all": "4.1.5",
"terser-webpack-plugin": "5.3.11",
"toml": "3.0.0",
Expand Down
27 changes: 27 additions & 0 deletions test/markdownlint-test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import pluginInline from "markdown-it-for-inline";
import pluginSub from "markdown-it-sub";
import pluginSup from "markdown-it-sup";
import test from "ava";
import spawn from "nano-spawn";
import { getVersion } from "markdownlint";
import { lint as lintAsync } from "markdownlint/async";
import { lint as lintPromise } from "markdownlint/promise";
Expand Down Expand Up @@ -1398,3 +1399,29 @@ for (const [ exportName, exportPath ] of exportMappings) {
t.is(importExportName, importExportPath);
});
}

test("subpathImports", async(t) => {
t.plan(8);
const scenarios = [
{ "conditions": "browser", "throws": true },
{ "conditions": "default", "throws": false },
{ "conditions": "markdownlint-imports-browser", "throws": true },
{ "conditions": "markdownlint-imports-node", "throws": false }
];
for (const scenario of scenarios) {
const { conditions, throws } = scenario;
try {
// eslint-disable-next-line no-await-in-loop
await spawn("node", [ `--conditions=${conditions}`, "./standalone.mjs" ], { "cwd": "./example" });
t.true(!throws, conditions);
} catch {
t.true(throws, conditions);
}
}
// Fake "100%" coverage for node-imports-browser.mjs
const { "fs": browserFs } = await import("../lib/node-imports-browser.mjs");
t.throws(() => browserFs.access());
t.throws(() => browserFs.accessSync());
t.throws(() => browserFs.readFile());
t.throws(() => browserFs.readFileSync());
});

0 comments on commit baf4944

Please sign in to comment.