Skip to content

Commit

Permalink
refactor: binary-data-url plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jul 13, 2024
1 parent cfae434 commit 6bb42f1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 24 deletions.
1 change: 1 addition & 0 deletions packages/react-server/examples/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@types/react": "latest",
"@types/react-dom": "latest",
"@vercel/og": "^0.6.2",
"magic-string": "^0.30.8",
"vite": "latest"
}
}
10 changes: 8 additions & 2 deletions packages/react-server/examples/next/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "ES2017",
"target": "ESNext",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
Expand All @@ -22,6 +22,12 @@
"@/*": ["./*"]
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts",
"*.mts"
],
"exclude": ["node_modules"]
}
47 changes: 29 additions & 18 deletions packages/react-server/examples/next/vite.config.mts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readFile } from "node:fs/promises";
import path from "node:path";
import MagicString from "magic-string";
import next from "next/vite";
import { defineConfig } from "vite";

Expand Down Expand Up @@ -46,25 +47,35 @@ export default defineConfig({
},
},
{
name: "import-meta-url-asset-binary",
enforce: "pre",
name: "binary-data-url",
async transform(code, id, _options) {
if (
code.includes(
`new URL("./noto-sans-v27-latin-regular.ttf", import.meta.url)`,
)
) {
const file = path.resolve(
id,
"..",
"noto-sans-v27-latin-regular.ttf",
);
const data = await readFile(file, "base64");
code = code.replace(
`new URL("./noto-sans-v27-latin-regular.ttf", import.meta.url)`,
() => `${JSON.stringify("data:text/plain;base64," + data)}`,
);
return code;
// input:
// new URL("./some-font.ttf", import.meta.url)
// output:
// new URL("data:application/octet-stream;base64,...")

// https://github.com/vitejs/vite/blob/ec16a5efc04d8ab50301d184c20e7bd0c8d8f6a2/packages/vite/src/node/plugins/assetImportMetaUrl.ts
const assetImportMetaUrlRE =
/\bnew\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\)/dg;
let match: RegExpExecArray | null;
let s: MagicString | undefined;
while ((match = assetImportMetaUrlRE.exec(code))) {
s ??= new MagicString(code);
const [[startIndex, endIndex], [urlStart, urlEnd]] =
match.indices!;
const url = code.slice(urlStart, urlEnd).slice(1, -1);
if (url[0] === ".") {
const file = path.resolve(path.dirname(id), url);
const data = await readFile(file, "base64");
s.update(
startIndex,
endIndex,
`new URL(${JSON.stringify("data:application/octet-stream;base64," + data)})`,
);
}
}
if (s?.hasChanged()) {
return { code: s.toString(), map: s.generateMap() };
}
},
},
Expand Down
11 changes: 7 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6bb42f1

Please sign in to comment.