|
| 1 | +import { fileURLToPath } from "node:url"; |
| 2 | +import { readFile } from "node:fs/promises"; |
| 3 | +import { marked } from "marked"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Exclude files that do not need to be processed |
| 7 | + * @param {string} id |
| 8 | + * @returns {boolean} |
| 9 | + */ |
| 10 | +function isExclude(id) { |
| 11 | + const res = [/\/src\/app\/_locales\/[A-Za-z0-9_]+\/messages\.js/]; |
| 12 | + for (const re of res) { |
| 13 | + if (re.test(id)) return false; |
| 14 | + } |
| 15 | + return true; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Preprocess `marked` calls and `.md?raw` transformations at build time |
| 20 | + * to avoid including `marked` modules and runtime transformations |
| 21 | + * @returns {import('vite').Plugin} |
| 22 | + */ |
| 23 | +function preprocessMarked() { |
| 24 | + return { |
| 25 | + name: "preprocess-marked", |
| 26 | + resolveId: { |
| 27 | + order: "pre", |
| 28 | + handler(source, importer) { |
| 29 | + if (isExclude(importer)) return; |
| 30 | + if (source.endsWith(".md?raw")) { |
| 31 | + const importerUrl = new URL(importer, import.meta.url); |
| 32 | + const sourceUrl = new URL(source, importerUrl); |
| 33 | + return fileURLToPath(sourceUrl) + "?marked"; |
| 34 | + } |
| 35 | + }, |
| 36 | + }, |
| 37 | + load: { |
| 38 | + order: "pre", |
| 39 | + async handler(id) { |
| 40 | + if (!id.endsWith(".md?marked")) return; |
| 41 | + const url = new URL(id, import.meta.url); |
| 42 | + const text = await readFile(fileURLToPath(url), "utf8"); |
| 43 | + const html = await marked.parse(text); |
| 44 | + return `export default ${JSON.stringify(html)}`; |
| 45 | + }, |
| 46 | + }, |
| 47 | + transform(code, id) { |
| 48 | + if (isExclude(id)) return; |
| 49 | + if (!code.includes("marked.parse")) return; |
| 50 | + const node = this.parse(code); |
| 51 | + if (node.sourceType !== "module") return; |
| 52 | + for (const module of node.body) { |
| 53 | + if (module.type !== "ImportDeclaration") continue; |
| 54 | + if (!module.source.value.toString().endsWith(".md?raw")) continue; |
| 55 | + const specifier = module.specifiers.find( |
| 56 | + (specifier) => specifier.type === "ImportDefaultSpecifier", |
| 57 | + ); |
| 58 | + const varName = specifier.local.name; |
| 59 | + code = code.replace(`await marked.parse(${varName})`, varName); |
| 60 | + // console.debug(`optimizing 'await marked.parse(${varName})'`, id); // DEBUG |
| 61 | + } |
| 62 | + return { code }; |
| 63 | + }, |
| 64 | + }; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Turn off `Marked` module side effects for better tree-shaking |
| 69 | + * @returns {import('vite').Plugin} |
| 70 | + */ |
| 71 | +function disableMarkedSideEffects() { |
| 72 | + return { |
| 73 | + name: "disable-marked-side-effects", |
| 74 | + transform(code, id) { |
| 75 | + if (id.includes("/node_modules/marked")) { |
| 76 | + return { code, moduleSideEffects: false }; |
| 77 | + } |
| 78 | + }, |
| 79 | + }; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * @returns {import('vite').Plugin[]} |
| 84 | + */ |
| 85 | +function markedDivest() { |
| 86 | + return [disableMarkedSideEffects(), preprocessMarked()]; |
| 87 | +} |
| 88 | + |
| 89 | +export default markedDivest; |
0 commit comments