Skip to content

Commit

Permalink
Fix sourcemap paths
Browse files Browse the repository at this point in the history
  • Loading branch information
AsPulse committed Dec 17, 2023
1 parent 33f4bc8 commit 7dbeda1
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 120 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
"@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",
"source-map": "^0.7.4",
"tsx": "^4.6.2",
"turbo": "latest",
"typescript": "^5.3.3"
Expand Down
42 changes: 18 additions & 24 deletions packages/core/build.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { consola } from 'consola';
import { join } from 'path';
import { basename, join } from 'path';
import type { RawSourceMap } from 'source-map';
import { mkdir, readFile, rm, writeFile } from 'fs/promises';
import { transform, minify } from '@swc/core';
import { build } from 'esbuild';
Expand Down Expand Up @@ -69,11 +70,9 @@ async function types() {
consola.start('Building types...');

const preserved_buildinfo = await readFile(tsbuildinfo).catch(() => null);
await writeFile('./dist/index.ts', (await bundled).source);
await execa('tsc', ['--project', 'tsconfig.types.json'])
.pipeStdout?.(stdout)
.pipeStderr?.(stderr);
await rm('./dist/index.ts');
if (preserved_buildinfo !== null) {
consola.info('tsbuildinfo was changed. reverting...');
await writeFile(tsbuildinfo, preserved_buildinfo);
Expand All @@ -82,24 +81,6 @@ async function types() {
await rm(tsbuildinfo);
}

// Check both index.d.ts and index.d.ts.map exists
await Promise.all([
readFile('./dist/index.d.ts', 'utf-8'),
readFile('./dist/index.d.ts.map', 'utf-8'),
]).catch(() => {
consola.error(
'tsc did not generate declaration files! Ensure you ran `pnpm typecheck` before run build.'
);
process.exit(1);
});

consola.start('Merging Declarationmap...');
const sourcemap = transfer({
fromSourceMap: await readFile('./dist/index.d.ts.map', 'utf-8'),
toSourceMap: (await bundled).sourcemap,
});
await writeFile('./dist/index.d.ts.map', sourcemap);

consola.success(`Done! (${output})`);
}

Expand All @@ -112,6 +93,7 @@ async function pipeline(format: 'esm' | 'cjs') {
? packageJson.exports['.'].import
: packageJson.exports['.'].require
);
const sourcemap_output = `${output}.map`;

const transformed = await transform(code.source, {
sourceMaps: true,
Expand Down Expand Up @@ -164,7 +146,7 @@ async function pipeline(format: 'esm' | 'cjs') {
)}%)`
);

await writeFile(output, minified.code);
await writeFile(output, minified.code + `\n//# sourceMappingURL=${basename(sourcemap_output)}`);
consola.success(`[${format}] Done! (${output})`);

consola.start(`[${format}] Merging sourcemaps...`);
Expand All @@ -176,8 +158,20 @@ async function pipeline(format: 'esm' | 'cjs') {
fromSourceMap: swc_map,
toSourceMap: (await bundled).sourcemap,
});
const sourcemap_output = `${output}.map`;
await writeFile(sourcemap_output, sourcemap);

const relative_sourcemap = JSON.parse(sourcemap) as RawSourceMap;
relative_sourcemap.sourcesContent = undefined;
relative_sourcemap.sourceRoot = '';
relative_sourcemap.sources = relative_sourcemap.sources.map(v => {
if(!v.startsWith('src')) {
consola.error(`[${format}] Source path ${v} is including out of ./src folder!`);
process.exit(1);
}
return `../${v}`;
});


await writeFile(sourcemap_output, JSON.stringify(relative_sourcemap));
consola.success(`[${format}] Done! (${sourcemap_output})`);
}

Expand Down
13 changes: 7 additions & 6 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@
"react",
"front-end"
],
"main": "dist/index.cjs",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"main": "dist/lib.cjs",
"module": "dist/lib.js",
"types": "dist/lib.d.ts",
"sideEffects": false,
"exports": {
".": {
"types": "./dist/index.d.ts",
"require": "./dist/index.cjs",
"import": "./dist/index.js"
"types": "./dist/lib.d.ts",
"require": "./dist/lib.cjs",
"import": "./dist/lib.js"
}
},
"files": [
"src",
"dist"
],
"scripts": {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { aabbccddeeffgg } from './util';
export { bare } from './util';

export function hello() {
return 'a' == aabbccddeeffgg();
export function hello(a: string): string {
return 'Hello, world!' + a;
}
5 changes: 2 additions & 3 deletions packages/core/src/util.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export function aabbccddeeffgg() {
console.log('a');
return 'a';
export function bare() {
return <></>;
}
2 changes: 1 addition & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"compilerOptions": {
"tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
},
"include": ["../**/*.ts", "../**/*.tsx"],
"include": ["../**/*.ts", "../**/*.tsx", ".eslintrc.cjs"],
"exclude": ["node_modules", "dist"]
}
3 changes: 2 additions & 1 deletion packages/core/tsconfig.types.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"emitDeclarationOnly": true
},
"files": ["./dist/index.ts"],
"files": ["./src/lib.ts"],
"include": []
}
84 changes: 3 additions & 81 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 7dbeda1

Please sign in to comment.