Skip to content

Commit 46745c1

Browse files
committed
add ability to generate chained sourcemap for precompiled sources
Signed-off-by: Karthik Ganeshram <[email protected]>
1 parent 5d210a7 commit 46745c1

File tree

7 files changed

+56
-14
lines changed

7 files changed

+56
-14
lines changed

packages/build-tools/package-lock.json

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

packages/build-tools/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@
2929
"typescript": "^5.7.3"
3030
},
3131
"dependencies": {
32+
"@ampproject/remapping": "^2.3.0",
3233
"@bytecodealliance/componentize-js": "^0.18.1",
3334
"@bytecodealliance/jco": "^1.10.2",
34-
"yargs": "^17.7.2",
3535
"acorn-walk": "^8.3.4",
3636
"acron": "^1.0.5",
3737
"magic-string": "^0.30.17",
38-
"regexpu-core": "^6.2.0"
38+
"regexpu-core": "^6.2.0",
39+
"yargs": "^17.7.2"
3940
},
4041
"files": [
4142
"lib",
@@ -53,4 +54,4 @@
5354
}
5455
]
5556
}
56-
}
57+
}

packages/build-tools/src/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { version as componentizeVersion } from '@bytecodealliance/componentize-j
55
import { getPackagesWithWasiDeps, processWasiDeps } from './wasiDepsParser.js';
66
import {
77
calculateChecksum,
8+
chainSourceMaps,
9+
fileExists,
810
saveBuildData,
911
} from './utils.js';
1012
import { getCliArgs } from './cli.js';
@@ -14,6 +16,7 @@ import { mergeWit } from '../lib/wit_tools.js';
1416
//@ts-ignore
1517
import { precompile } from "./precompile.js"
1618
import path from 'node:path'
19+
import { SourceMapInput } from '@ampproject/remapping';
1720

1821
async function main() {
1922
try {
@@ -65,13 +68,20 @@ async function main() {
6568
console.log('Componentizing...');
6669

6770
const source = await readFile(src, 'utf8');
68-
const precompiledSource = precompile(source, src, true) as string;
71+
let { content: precompiledSource, sourceMapInput: precompiledSourceMap } = precompile(source, src, true, 'precompiled-source.js') as { content: string; sourceMapInput: SourceMapInput };
72+
// Check if input file has a source map because if we does, we need to chain it with the precompiled source map
73+
if (await fileExists(src + '.map')) {
74+
const inputSourceMap = JSON.parse(await readFile(src + '.map', 'utf8')) as SourceMapInput;
75+
precompiledSourceMap = chainSourceMaps(precompiledSourceMap, { src: inputSourceMap }) as SourceMapInput;
76+
}
6977

70-
// Write precompiled source to disk for debugging purposes In the future we
71-
// will also write a source map to make debugging easier
78+
// Write precompiled source to disk for debugging purposes.
7279
let srcDir = path.dirname(src);
7380
let precompiledSourcePath = path.join(srcDir, 'precompiled-source.js');
7481
await writeFile(precompiledSourcePath, precompiledSource);
82+
if (precompiledSourceMap) {
83+
await writeFile(precompiledSourcePath + '.map', JSON.stringify(precompiledSourceMap, null, 2));
84+
}
7585

7686
const { component } = await componentize({
7787
sourcePath: precompiledSourcePath,

packages/build-tools/src/precompile.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const POSTAMBLE = '}';
1717
/// will intern regular expressions, duplicating them at the top level and testing them with both
1818
/// an ascii and utf8 string should ensure that they won't be re-compiled when run in the fetch
1919
/// handler.
20-
export function precompile(source, filename = '<input>', moduleMode = false) {
20+
export function precompile(source, filename = '<input>', moduleMode = false, precompiledFileName = 'precompiled-source.js') {
2121
const magicString = new MagicString(source, {
2222
filename,
2323
});
@@ -52,11 +52,11 @@ export function precompile(source, filename = '<input>', moduleMode = false) {
5252
magicString.prepend(`${PREAMBLE}${precompileCalls.join('\n')}${POSTAMBLE}`);
5353

5454
// When we're ready to pipe in source maps:
55-
// const map = magicString.generateMap({
56-
// source: 'source.js',
57-
// file: 'converted.js.map',
58-
// includeContent: true
59-
// });
55+
const map = magicString.generateMap({
56+
source: filename,
57+
file: `${precompiledFileName}.map`,
58+
includeContent: true
59+
});
6060

61-
return magicString.toString();
61+
return { content: magicString.toString(), sourceMap: map };
6262
}

packages/build-tools/src/utils.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
import { readFile } from 'fs/promises';
22
import { createHash } from 'node:crypto';
33
import { access, writeFile } from 'node:fs/promises';
4+
import remapping, { SourceMapInput } from '@ampproject/remapping';
45

6+
type FileName = string;
7+
type SourceMapLookup = Record<FileName, SourceMapInput>;
8+
9+
export function chainSourceMaps(finalMap: SourceMapInput, sourceMapLookup: SourceMapLookup) {
10+
return remapping(finalMap, (source) => {
11+
const sourceMap = sourceMapLookup[source];
12+
if (sourceMap) {
13+
return sourceMap;
14+
}
15+
// If not the source, we do not want to traverse it further so we return null.
16+
// This is because sometimes npm packages have their own source maps but do
17+
// not have the original source files.
18+
return null;
19+
});
20+
}
521

6-
// Function to calculate file checksum
722
export async function calculateChecksum(content: string | Buffer) {
823
try {
924
const hash = createHash('sha256');

test/test-app/package-lock.json

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

test/test-app/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const config = async () => {
77
mode: 'production',
88
stats: 'errors-only',
99
entry: './src/index.ts',
10+
devtool: 'source-map',
1011
experiments: {
1112
outputModule: true,
1213
},

0 commit comments

Comments
 (0)