|
| 1 | +import assert from "node:assert"; |
| 2 | +import path from "node:path"; |
1 | 3 | import url from "node:url"; |
2 | 4 | import { isValidIdentifier } from "@babel/types"; |
| 5 | +import { outdent } from "outdent"; |
3 | 6 | import serialize from "serialize-javascript"; |
4 | 7 |
|
| 8 | +function serializeModule(module) { |
| 9 | + return Object.entries(module) |
| 10 | + .map(([specifier, value]) => { |
| 11 | + const code = |
| 12 | + value instanceof RegExp |
| 13 | + ? `/${value.source}/${value.flags}` |
| 14 | + : serialize(value, { space: 2, unsafe: true }); |
| 15 | + if (specifier === "default") { |
| 16 | + return `export default ${code};`; |
| 17 | + } |
| 18 | + |
| 19 | + if (!isValidIdentifier(specifier)) { |
| 20 | + throw new Error(`${specifier} is not a valid specifier`); |
| 21 | + } |
| 22 | + |
| 23 | + return `export const ${specifier} = ${code};`; |
| 24 | + }) |
| 25 | + .join("\n"); |
| 26 | +} |
| 27 | + |
| 28 | +function serializeVisitorKeys(module) { |
| 29 | + const specifiers = Object.keys(module); |
| 30 | + assert.deepEqual(specifiers, ["default"]); |
| 31 | + |
| 32 | + const references = new Map(); |
| 33 | + const properties = Object.entries(module.default); |
| 34 | + for (const [, keys] of properties) { |
| 35 | + if (!references.has(keys)) { |
| 36 | + references.set(keys, 1); |
| 37 | + } else { |
| 38 | + references.set(keys, references.get(keys) + 1); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + const variables = []; |
| 43 | + const propertiesText = []; |
| 44 | + for (const [type, keys] of properties) { |
| 45 | + const isSingleReference = references.get(keys) === 1; |
| 46 | + const key = isValidIdentifier(type) ? type : JSON.stringify(type); |
| 47 | + if (isSingleReference) { |
| 48 | + propertiesText.push(` ${key}: ${JSON.stringify(keys)},`); |
| 49 | + } else { |
| 50 | + if (!variables.includes(keys)) { |
| 51 | + variables.push(keys); |
| 52 | + } |
| 53 | + const index = variables.indexOf(keys); |
| 54 | + propertiesText.push(` ${key}: vk[${index}],`); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + const code = outdent` |
| 59 | + const vk = [ |
| 60 | + ${variables.map((keys) => ` ${JSON.stringify(keys)},`).join("\n")} |
| 61 | + ]; |
| 62 | +
|
| 63 | + export default { |
| 64 | + ${propertiesText.join("\n")} |
| 65 | + }; |
| 66 | + `; |
| 67 | + |
| 68 | + return code; |
| 69 | +} |
| 70 | + |
5 | 71 | export default function esbuildPluginEvaluate() { |
6 | 72 | return { |
7 | 73 | name: "evaluate", |
8 | 74 | setup(build) { |
9 | 75 | build.onLoad( |
10 | 76 | { filter: /\.evaluate\.[cm]?js$/, namespace: "file" }, |
11 | | - async ({ path }) => { |
12 | | - const module = await import(url.pathToFileURL(path)); |
13 | | - const text = Object.entries(module) |
14 | | - .map(([specifier, value]) => { |
15 | | - const code = |
16 | | - value instanceof RegExp |
17 | | - ? `/${value.source}/${value.flags}` |
18 | | - : serialize(value, { space: 2 }); |
19 | | - |
20 | | - if (specifier === "default") { |
21 | | - return `export default ${code};`; |
22 | | - } |
23 | | - |
24 | | - if (!isValidIdentifier(specifier)) { |
25 | | - throw new Error(`${specifier} is not a valid specifier`); |
26 | | - } |
27 | | - |
28 | | - return `export const ${specifier} = ${code};`; |
29 | | - }) |
30 | | - .join("\n"); |
| 77 | + async ({ path: filePath }) => { |
| 78 | + const module = await import(url.pathToFileURL(filePath)); |
| 79 | + const isVisitorKeys = |
| 80 | + path.basename(filePath, path.extname(filePath)) === |
| 81 | + "visitor-keys.evaluate"; |
| 82 | + |
| 83 | + const text = isVisitorKeys |
| 84 | + ? serializeVisitorKeys(module) |
| 85 | + : serializeModule(module); |
| 86 | + |
31 | 87 | return { contents: text }; |
32 | 88 | }, |
33 | 89 | ); |
|
0 commit comments