Skip to content

Commit 5065082

Browse files
committed
fix: improve sourcemap support
1 parent f5da245 commit 5065082

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
},
3939
"dependencies": {
4040
"@babel/runtime": "^7.23.7",
41+
"@jridgewell/sourcemap-codec": "^1.5.0",
4142
"@rollup/pluginutils": "^5.1.0",
4243
"estree-walker": "^3.0.3",
43-
"is-reference": "^3.0.2",
44-
"magic-string": "^0.30.7"
44+
"is-reference": "^3.0.2"
4545
},
4646
"devDependencies": {
4747
"@gera2ld/plaid": "~2.7.0",

pnpm-lock.yaml

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { encode } from '@jridgewell/sourcemap-codec';
12
import { readFile } from 'fs/promises';
2-
import MagicString from 'magic-string';
33
import type { Plugin } from 'rollup';
44
import { collectGmApi, getMetadata } from './util';
55

@@ -32,7 +32,7 @@ export default (transform?: (metadata: string) => string): Plugin => {
3232
* Use `renderChunk` instead of `banner` to preserve the metadata after minimization.
3333
* Note that this plugin must be put after `@rollup/plugin-terser`.
3434
*/
35-
async renderChunk(code, chunk) {
35+
async renderChunk(code, chunk, options) {
3636
const metadataFile =
3737
chunk.isEntry &&
3838
[chunk.facadeModuleId, ...Object.keys(chunk.modules)]
@@ -51,11 +51,27 @@ export default (transform?: (metadata: string) => string): Plugin => {
5151
}
5252
metadata = getMetadata(metadata, grantSet);
5353
if (transform) metadata = transform(metadata);
54-
const s = new MagicString(code);
55-
s.prepend(`${metadata}\n\n`);
54+
let map = null;
55+
56+
if (options.sourcemap) {
57+
const mappings = [
58+
...Array.from(metadata.split('\n'), () => []),
59+
[],
60+
...Array.from(code.split('\n'), () => [[0, 0, 0, 0]]),
61+
];
62+
map = {
63+
version: 3,
64+
file: chunk.fileName,
65+
sources: [chunk.fileName],
66+
sourcesContent: [code],
67+
names: [],
68+
mappings: encode(mappings),
69+
};
70+
}
71+
5672
return {
57-
code: s.toString(),
58-
map: s.generateMap({ hires: 'boundary' }).toString(),
73+
code: `${metadata}\n\n${code}`,
74+
map,
5975
};
6076
},
6177
};

0 commit comments

Comments
 (0)