Skip to content

Commit cb404bf

Browse files
authored
feat: improve sourcemaps generation (#280)
- Generate sourcemap for TS declaration files - Include `sourceRoot` to resolve the source file against - Don't inline the source code to the sourcemap to reduce size of the map - Add an option to disable source maps (e.g. if we're not publishing the source) Closes #279
1 parent 2f53830 commit cb404bf

File tree

5 files changed

+19
-11
lines changed

5 files changed

+19
-11
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ By default, this will compile the code for last 2 versions of modern browsers, a
147147

148148
If your source code is written in [Flow](http://www.typescriptlang.org/), You can also specify the `copyFlow` option to copy the source files as `.js.flow` to the output folder. If the `main` entry in `package.json` points to the `index` file in the output folder, the flow type checker will pick these files up to use for type definitions.
149149

150+
By default, sourcemaps are generated alongside the compiled files. You can disable them by setting the `sourceMaps` option to `false`.
151+
150152
Example:
151153

152154
```json

packages/react-native-builder-bob/src/targets/commonjs.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type Options = Input & {
88
options?: {
99
babelrc?: boolean | null;
1010
configFile?: string | false | null;
11+
sourceMaps?: boolean;
1112
copyFlow?: boolean;
1213
};
1314
};
@@ -26,13 +27,11 @@ export default async function build({
2627
await del([output]);
2728

2829
await compile({
30+
...options,
2931
root,
3032
source,
3133
output,
3234
modules: 'commonjs',
33-
babelrc: options?.babelrc,
34-
configFile: options?.configFile,
35-
copyFlow: options?.copyFlow,
3635
report,
3736
});
3837
}

packages/react-native-builder-bob/src/targets/module.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type Options = Input & {
88
options?: {
99
babelrc?: boolean | null;
1010
configFile?: string | false | null;
11+
sourceMaps?: boolean;
1112
copyFlow?: boolean;
1213
};
1314
};
@@ -26,13 +27,11 @@ export default async function build({
2627
await del([output]);
2728

2829
await compile({
30+
...options,
2931
root,
3032
source,
3133
output,
3234
modules: false,
33-
babelrc: options?.babelrc,
34-
configFile: options?.configFile,
35-
copyFlow: options?.copyFlow,
3635
report,
3736
});
3837
}

packages/react-native-builder-bob/src/targets/typescript.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ export default async function build({
132132
[
133133
'--pretty',
134134
'--declaration',
135+
'--declarationMap',
135136
'--emitDeclarationOnly',
136137
'--project',
137138
project,

packages/react-native-builder-bob/src/utils/compile.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import glob from 'glob';
77
import type { Input } from '../types';
88

99
type Options = Input & {
10-
babelrc?: boolean | null | undefined;
11-
configFile?: string | false | null | undefined;
10+
babelrc?: boolean | null;
11+
configFile?: string | false | null;
12+
sourceMaps?: boolean;
13+
copyFlow?: boolean;
1214
modules: 'commonjs' | false;
13-
copyFlow: boolean | undefined;
1415
};
1516

1617
export default async function compile({
@@ -21,6 +22,7 @@ export default async function compile({
2122
configFile = false,
2223
modules,
2324
copyFlow,
25+
sourceMaps = true,
2426
report,
2527
}: Options) {
2628
const files = glob.sync('**/*', {
@@ -54,7 +56,8 @@ export default async function compile({
5456
const result = await babel.transformAsync(content, {
5557
babelrc: babelrc,
5658
configFile: configFile,
57-
sourceMaps: true,
59+
sourceMaps,
60+
sourceRoot: path.relative(output, source),
5861
filename: filepath,
5962
...(babelrc || configFile
6063
? null
@@ -99,10 +102,14 @@ export default async function compile({
99102

100103
let code = result.code;
101104

102-
if (result.map) {
105+
if (sourceMaps && result.map) {
103106
const mapFilename = outputFilename + '.map';
107+
104108
code += '\n//# sourceMappingURL=' + path.basename(mapFilename);
105109

110+
// Don't inline the source code, it can be retrieved from the source file
111+
result.map.sourcesContent = undefined;
112+
106113
fs.writeFileSync(mapFilename, JSON.stringify(result.map));
107114
}
108115

0 commit comments

Comments
 (0)