Skip to content

Commit

Permalink
Add sourcemap
Browse files Browse the repository at this point in the history
  • Loading branch information
AsPulse committed Dec 13, 2023
1 parent 045e500 commit 3ec62e1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
"@repo/eslint-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@swc/core": "^1.3.100",
"@types/multi-stage-sourcemap": "^0.3.4",
"@types/node": "^20.10.4",
"consola": "^3.2.3",
"dts-bundle-generator": "^9.0.0",
"esbuild": "^0.19.9",
"esbuild-node-externals": "^1.11.0",
"execa": "^8.0.1",
"multi-stage-sourcemap": "^0.3.1",
"prettier": "^3.1.0",
"tsx": "^4.6.2",
"turbo": "latest",
Expand Down
23 changes: 17 additions & 6 deletions packages/core/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { build } from 'esbuild';
import { execa } from 'execa';
import { nodeExternalsPlugin } from 'esbuild-node-externals';
import packageJson from './package.json' assert { type: 'json' };
import { transfer } from 'multi-stage-sourcemap';


const entry = './lib.ts';
Expand All @@ -25,6 +26,8 @@ const bundled = bundle();
await Promise.all([bundled, pipeline('esm'), pipeline('cjs'), types()]);
consola.log('');
consola.success('All tasks completed.');
consola.log('');


async function bundle() {
consola.start(`Bundling...`);
Expand Down Expand Up @@ -54,7 +57,7 @@ async function bundle() {
consola.error('Build result of esbuild did not contain source!');
process.exit(1);
}
consola.success(`Bundling by esbuild finished! (${toKiloBytes(getStringBytes(source))})`);
consola.success(`Bundled by esbuild! (${toKiloBytes(getStringBytes(source))})`);
return { sourcemap, source };
}

Expand All @@ -68,7 +71,7 @@ async function types() {
async function pipeline(format: 'esm' | 'cjs') {
const code = await bundled;

consola.start(`Building ${format}...`);
consola.start(`[${format}] Building...`);
const output = join(format === 'esm' ? packageJson.exports['.'].import : packageJson.exports['.'].require);

const transformed = await transform(code.source, {
Expand All @@ -90,7 +93,7 @@ async function pipeline(format: 'esm' | 'cjs') {

const transform_map = transformed.map;
if (transform_map === undefined) {
consola.error('Transform result of SWC did not contain source!');
consola.error(`[${format}] Transform result of SWC did not contain source!`);
process.exit(1);
}

Expand All @@ -104,7 +107,7 @@ async function pipeline(format: 'esm' | 'cjs') {

const minify_map = minified.map;
if (minify_map === undefined) {
consola.error('Minify result of SWC did not contain source!');
consola.error(`[${format}] Minify result of SWC did not contain source!`);
process.exit(1);
}

Expand All @@ -116,12 +119,20 @@ async function pipeline(format: 'esm' | 'cjs') {
consola.info(`[${format}] SWC minified. (${toKiloBytes(size.unminified)} -> ${toKiloBytes(size.minified)}, -${Math.round((1 - size.minified / size.unminified) * 100)}%)`);

await writeFile(output, minified.code);
consola.success(`Done! (${output})`);
consola.success(`[${format}] Done! (${output})`);

consola.start(`[${format}] Merging sourcemaps...`);
const swc_map = transfer({ fromSourceMap: minify_map, toSourceMap: transform_map });
const sourcemap = transfer({ fromSourceMap: swc_map, toSourceMap: (await bundled).sourcemap });
const sourcemap_output = `${output}.map`;
await writeFile(sourcemap_output, sourcemap);
consola.success(`[${format}] Done! (${sourcemap_output})`);

}

function getStringBytes(str: string) {
return Buffer.byteLength(str, 'utf-8');
}
function toKiloBytes(bytes: number) {
return `${Math.round(bytes * 100) / 100}kB`;
return `${Math.round(bytes / 10) / 100}kB`;
}
35 changes: 35 additions & 0 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 3ec62e1

Please sign in to comment.