Skip to content

Commit 06d9a39

Browse files
authored
feat(jsii): make source map behavior fully configurable (#3558)
Instead of enabling declarations maps everywhere as was done in #3521, allow customers to define their desired source-map related configuration in the `jsii.tsc` stanza of `package.json`. This change in stance is motivated by how introduction of declarations map causes broad asset hash changes in consumer code, which effectively breaks many snapshot-based regression tests, and this feature should hence be opt-in. --- By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license]. [Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
1 parent c40f26c commit 06d9a39

8 files changed

Lines changed: 80 additions & 27 deletions

File tree

.github/workflows/main.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ on:
99

1010
env:
1111
DOTNET_NOLOGO: true
12+
NODE_OPTIONS: --max-old-space-size=4096
1213

1314
# This workflows currently has the following jobs:
1415
# - build : Builds the source tree as-is

gh-pages/content/user-guides/lib-author/configuration/index.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,14 @@ are set in the `jsii.tsc` section of the `package.json` file, but use the same n
186186
- `rootDir` - specifies the root directory that contains all of the `.ts` source files. This is used in conjunction with
187187
`outDir`, to control the directory structure that gets generated.
188188
- `forceConsistentCasingInFileNames` - if `true`, will make the `TypeScript` compiler care about the casing of files
189-
specified in `import` statements. This is helpful if you're developing on a filesystem that is case-insensitive
189+
specified in `import` statements. This is helpful if you're developing on a filesystem that is case-insensitive
190190
(Mac/Win), but building/deploying on a filesystem that is case-sensitive (Linux).
191+
- `declarationMap`, `inlineSourceMap`, `inlineSources`, and `sourceMap` allow confifuring the source map generation.
192+
This option can be useful to finely control your local development experience (for example, by enabling
193+
`declarationMap`), or to optimize the emitted code size (by disabling source maps entirely).
194+
+ if any of these options is specified, the source map configuration will exactly match what is being provided here
195+
+ If none are specified, the default settings will be used: `#!ts { inlineSourceMap: true, inlineSources: true }`
196+
191197
Refer to the [TypeScript compiler options reference][ts-options] for more information about those options.
192198

193199
[`tsconfig.json`]: https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

packages/@jsii/check-node/src/index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ test('tested node releases are correctly registered & supported', () => {
1010
});
1111

1212
// This test is there to ensure house-keeping happens when it should. If we are
13-
// testing a given node release, it must not have been EOL for over 30 days.
14-
test('tested node release have not ben EOL for more than 30 days', () => {
13+
// testing a given node release, it must not have been EOL for over 60 days.
14+
test('tested node release have not ben EOL for more than 60 days', () => {
1515
const { nodeRelease } = NodeRelease.forThisRuntime();
1616
expect(nodeRelease?.endOfLifeDate?.getTime()).toBeGreaterThan(
17-
Date.now() - 30 * 86_400_000,
17+
Date.now() - 60 * 86_400_000,
1818
);
1919
});

packages/@scope/jsii-calc-lib/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@
6969
}
7070
},
7171
"tsc": {
72-
"outDir": "build"
72+
"outDir": "build",
73+
"sourceMap": false
7374
},
7475
"versionFormat": "short",
7576
"metadata": {

packages/jsii/lib/compiler.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ const BASE_COMPILER_OPTIONS: ts.CompilerOptions = {
1616
alwaysStrict: true,
1717
charset: 'utf8',
1818
declaration: true,
19-
declarationMap: true,
2019
experimentalDecorators: true,
2120
incremental: true,
22-
inlineSourceMap: true,
23-
inlineSources: true,
2421
lib: ['lib.es2019.d.ts'],
2522
module: ts.ModuleKind.CommonJS,
2623
newLine: ts.NewLineKind.LineFeed,

packages/jsii/lib/project-info.ts

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,21 @@ const spdx: Set<string> = require('spdx-license-list/simple');
1313

1414
const LOG = log4js.getLogger('jsii/package-info');
1515

16-
export interface TSCompilerOptions {
17-
readonly outDir?: string;
18-
readonly rootDir?: string;
19-
readonly forceConsistentCasingInFileNames?: boolean;
20-
}
16+
export type TSCompilerOptions = Partial<
17+
Pick<
18+
ts.CompilerOptions,
19+
// Directory preferences
20+
| 'outDir'
21+
| 'rootDir'
22+
// Style preferences
23+
| 'forceConsistentCasingInFileNames'
24+
// Source map preferences
25+
| 'declarationMap'
26+
| 'inlineSourceMap'
27+
| 'inlineSources'
28+
| 'sourceMap'
29+
>
30+
>;
2131

2232
export interface ProjectInfo {
2333
readonly projectRoot: string;
@@ -207,6 +217,9 @@ export function loadProjectInfo(projectRoot: string): ProjectInfoResult {
207217
tsc: {
208218
outDir: pkg.jsii?.tsc?.outDir,
209219
rootDir: pkg.jsii?.tsc?.rootDir,
220+
forceConsistentCasingInFileNames:
221+
pkg.jsii?.tsc?.forceConsistentCasingInFileNames,
222+
..._sourceMapPreferences(pkg.jsii?.tsc),
210223
},
211224
bin: pkg.bin,
212225
exports: pkg.exports,
@@ -228,6 +241,34 @@ function _guessRepositoryType(url: string): string {
228241
);
229242
}
230243

244+
function _sourceMapPreferences({
245+
declarationMap,
246+
inlineSourceMap,
247+
inlineSources,
248+
sourceMap,
249+
}: TSCompilerOptions = {}) {
250+
// If none of the options are specified, use the default configuration from jsii <= 1.58.0, which
251+
// means inline source maps with embedded source information.
252+
if (
253+
declarationMap == null &&
254+
inlineSourceMap == null &&
255+
inlineSources == null &&
256+
sourceMap == null
257+
) {
258+
declarationMap = false;
259+
inlineSourceMap = true;
260+
inlineSources = true;
261+
sourceMap = undefined;
262+
}
263+
264+
return {
265+
declarationMap,
266+
inlineSourceMap,
267+
inlineSources,
268+
sourceMap,
269+
};
270+
}
271+
231272
interface DependencyInfo {
232273
readonly assembly: spec.Assembly;
233274
readonly resolvedDependencies: Record<string, string>;

packages/jsii/test/compiler.test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ describe(Compiler, () => {
149149
}
150150
});
151151

152-
test('emits declaration map', () => {
152+
test('emits declaration map when feature is enabled', () => {
153153
const sourceDir = mkdtempSync(join(tmpdir(), 'jsii-tmpdir'));
154154

155155
try {
@@ -158,6 +158,9 @@ describe(Compiler, () => {
158158
const compiler = new Compiler({
159159
projectInfo: {
160160
..._makeProjectInfo(sourceDir, 'index.d.ts'),
161+
tsc: {
162+
declarationMap: true,
163+
},
161164
},
162165
generateTypeScriptConfig: 'tsconfig.jsii.json',
163166
});
@@ -191,6 +194,11 @@ function _makeProjectInfo(sourceDir: string, types: string): ProjectInfo {
191194
bundleDependencies: {},
192195
targets: {},
193196
excludeTypescript: [],
197+
tsc: {
198+
// NOTE: these are the default values jsii uses when none are provided in package.json.
199+
inlineSourceMap: true,
200+
inlineSources: true,
201+
},
194202
};
195203
}
196204

@@ -203,7 +211,6 @@ function expectedTypeScriptConfig() {
203211
charset: 'utf8',
204212
composite: false,
205213
declaration: true,
206-
declarationMap: true,
207214
experimentalDecorators: true,
208215
incremental: true,
209216
inlineSourceMap: true,

packages/jsii/test/deprecated-remover.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ test('produces correct output', () => {
113113
export * from './retained';
114114
export * from './enums';
115115
export { GrandChild, Retained } from './mixed';
116-
//# sourceMappingURL=index.d.ts.map//////////////////
116+
//////////////////
117117
118118
119119
///////////////////////
120120
/// deprecated.d.ts ///
121-
//# sourceMappingURL=deprecated.d.ts.map///////////////////////
121+
///////////////////////
122122
123123
124124
/////////////////////
@@ -127,15 +127,15 @@ test('produces correct output', () => {
127127
}
128128
export declare class RetainedClass {
129129
}
130-
//# sourceMappingURL=retained.d.ts.map/////////////////////
130+
/////////////////////
131131
132132
133133
//////////////////
134134
/// enums.d.ts ///
135135
export declare enum SomeEnum {
136136
VALUE_RETAINED = 0
137137
}
138-
//# sourceMappingURL=enums.d.ts.map//////////////////
138+
//////////////////
139139
140140
141141
//////////////////
@@ -148,7 +148,7 @@ test('produces correct output', () => {
148148
export declare class GrandChild extends Retained implements retained_1.IRetainedInterface {
149149
retainedMethod(): void;
150150
}
151-
//# sourceMappingURL=mixed.d.ts.map//////////////////
151+
//////////////////
152152
"
153153
`);
154154
});
@@ -177,12 +177,12 @@ test('cross-file deprecated heritage', () => {
177177
import './deprecated';
178178
export interface INotDeprecated {
179179
}
180-
//# sourceMappingURL=index.d.ts.map//////////////////
180+
//////////////////
181181
182182
183183
///////////////////////
184184
/// deprecated.d.ts ///
185-
//# sourceMappingURL=deprecated.d.ts.map///////////////////////
185+
///////////////////////
186186
"
187187
`);
188188
});
@@ -452,15 +452,15 @@ describe('stripDeprecatedAllowList', () => {
452452
export * from './retained';
453453
export * from './enums';
454454
export { Deprecated, GrandChild, Retained } from './mixed';
455-
//# sourceMappingURL=index.d.ts.map//////////////////
455+
//////////////////
456456
457457
458458
///////////////////////
459459
/// deprecated.d.ts ///
460460
/** @deprecated stripped */
461461
export declare class DeprecatedClass {
462462
}
463-
//# sourceMappingURL=deprecated.d.ts.map///////////////////////
463+
///////////////////////
464464
465465
466466
/////////////////////
@@ -469,7 +469,7 @@ describe('stripDeprecatedAllowList', () => {
469469
}
470470
export declare class RetainedClass {
471471
}
472-
//# sourceMappingURL=retained.d.ts.map/////////////////////
472+
/////////////////////
473473
474474
475475
//////////////////
@@ -482,7 +482,7 @@ describe('stripDeprecatedAllowList', () => {
482482
VALUE_ONE = 0,
483483
VALUE_TWO = 1
484484
}
485-
//# sourceMappingURL=enums.d.ts.map//////////////////
485+
//////////////////
486486
487487
488488
//////////////////
@@ -499,7 +499,7 @@ describe('stripDeprecatedAllowList', () => {
499499
export declare class GrandChild extends Deprecated {
500500
retainedMethod(): void;
501501
}
502-
//# sourceMappingURL=mixed.d.ts.map//////////////////
502+
//////////////////
503503
"
504504
`);
505505
});

0 commit comments

Comments
 (0)