Skip to content

Commit eb090c7

Browse files
committed
add vite support
1 parent dd7a7d2 commit eb090c7

File tree

30 files changed

+232
-3
lines changed

30 files changed

+232
-3
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
"esbuild": {
1515
"require": "./dist/esbuild.js",
1616
"import": "./dist/esbuild.mjs"
17+
},
18+
"vite": {
19+
"require": "./dist/vite.js",
20+
"import": "./dist/vite.mjs"
1721
}
1822
},
1923
"scripts": {
@@ -26,6 +30,7 @@
2630
"pkgroll": "^2.5.0",
2731
"rollup": "^4.22.5",
2832
"typescript": "^5.6.2",
33+
"vite": "^5.4.8",
2934
"vitest": "^2.1.1",
3035
"webpack": "^5.95.0",
3136
"webpack-cli": "^5.1.4"

src/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function stringToUUID(str: string): string {
2929

3030
export function addDebugIdToSource(input: string, debugId: string): string {
3131
return input.replace(
32-
/\s*(?:\/\/# debugId=.+)?\s*(\/\/# .+)?\s*$/,
32+
/\s*(?:\/\/# debugId=.+)?\s*(\/\/# sourceMappingURL=.+)?\s*$/,
3333
`\n//# debugId=${debugId}\n$1`
3434
);
3535
}

src/rollup.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default function debugIds(): Plugin {
1010
return {
1111
name: "rollup-plugin-debug-ids",
1212
generateBundle: function (
13-
_: unknown,
13+
_,
1414
bundle: { [fileName: string]: OutputAsset | OutputChunk }
1515
) {
1616
for (const [key, value] of Object.entries(bundle)) {
@@ -36,6 +36,22 @@ export default function debugIds(): Plugin {
3636
sourceMapFile.source.toString(),
3737
debugId
3838
);
39+
40+
// vite has plugins that run after us which can modify the sourcemap so we
41+
// proxy the sourceMapFile to re-add the debugId if the source gets set again
42+
bundle[value.sourcemapFileName] = new Proxy(
43+
bundle[value.sourcemapFileName],
44+
{
45+
set: function (target, prop, value) {
46+
if (prop === "source") {
47+
target[prop] = addDebugIdToSourcemap(value, debugId);
48+
} else {
49+
target[prop] = value;
50+
}
51+
return true;
52+
},
53+
}
54+
);
3955
}
4056
},
4157
};

src/vite.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import rollupPlugin from "./rollup";
2+
import { Plugin } from "vite";
3+
4+
export default function debugIds(): Plugin {
5+
return {
6+
...rollupPlugin(),
7+
name: "vite-plugin-debug-ids",
8+
apply: "build",
9+
enforce: "post",
10+
};
11+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("anther");

test/vite/no-sourcemaps/snap/assets/index-CGQYQSPO.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("anther");
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite App</title>
8+
</head>
9+
<body>
10+
<div id="app"></div>
11+
<script type="module" src="/main.js"></script>
12+
</body>
13+
</html>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
console.log("do nothing");
2+
import("./another.js");
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { join } from "path";
2+
import debugIds from "../../../dist/vite.mjs";
3+
import { defineConfig } from "vite";
4+
5+
const __dirname = new URL(".", import.meta.url).pathname;
6+
7+
export default defineConfig({
8+
root: join(__dirname, "src"),
9+
mode: "production",
10+
plugins: [debugIds()],
11+
build: {
12+
outDir: join(__dirname, "dist"),
13+
emptyOutDir: true,
14+
},
15+
});

0 commit comments

Comments
 (0)