Skip to content

Commit c8a3cd6

Browse files
milahukaisermann
andauthored
fix: translate options.sourceMap to options.compilerOptions.sourceMap for ts (#286) (#299)
* fix typescript: make sourcemaps default on (#286) * move option * getTransformerOptions: accept propPath * typescript: fix propPath to compilerOptions.sourceMap * typescript: remove old patch * lint * fix test to use propPath * chore: 🤖 lint * refactor to function setProp * setProp: clarify fn signature Co-authored-by: Christian Kaisermann <[email protected]>
1 parent c23f11a commit c8a3cd6

File tree

4 files changed

+33
-15
lines changed

4 files changed

+33
-15
lines changed

Diff for: src/autoProcess.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type {
77
Transformers,
88
Options,
99
} from './types';
10-
import { hasDepInstalled, concat } from './modules/utils';
10+
import { hasDepInstalled, concat, setProp } from './modules/utils';
1111
import { getTagInfo } from './modules/tagInfo';
1212
import {
1313
addLanguageAlias,
@@ -127,9 +127,7 @@ export function sveltePreprocess(
127127
}
128128

129129
if (sourceMap && name in SOURCE_MAP_PROP_MAP) {
130-
const [propName, value] = SOURCE_MAP_PROP_MAP[name];
131-
132-
opts[propName] = value;
130+
setProp(opts, ...SOURCE_MAP_PROP_MAP[name]);
133131
}
134132

135133
return opts;

Diff for: src/modules/language.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ export function getLanguageDefaults(lang: string): null | Record<string, any> {
3636
return defaults;
3737
}
3838

39-
export const SOURCE_MAP_PROP_MAP: Record<string, [string, any]> = {
40-
babel: ['sourceMaps', true],
41-
typescript: ['sourceMap', true],
42-
scss: ['sourceMap', true],
43-
less: ['sourceMap', {}],
44-
stylus: ['sourcemap', true],
45-
postcss: ['map', true],
46-
coffeescript: ['sourceMap', true],
47-
globalStyle: ['sourceMap', true],
39+
export const SOURCE_MAP_PROP_MAP: Record<string, [string[], any]> = {
40+
babel: [['sourceMaps'], true],
41+
typescript: [['compilerOptions', 'sourceMap'], true],
42+
scss: [['sourceMap'], true],
43+
less: [['sourceMap'], {}],
44+
stylus: [['sourcemap'], true],
45+
postcss: [['map'], true],
46+
coffeescript: [['sourceMap'], true],
47+
globalStyle: [['sourceMap'], true],
4848
};
4949

5050
export const ALIAS_MAP = new Map([

Diff for: src/modules/utils.ts

+17
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,20 @@ export function findUp({ what, from }) {
9090

9191
return null;
9292
}
93+
94+
// set deep property in object
95+
export function setProp(obj, keyList, val) {
96+
let i = 0;
97+
98+
for (; i < keyList.length - 1; i++) {
99+
const key = keyList[i];
100+
101+
if (typeof obj[key] !== 'object') {
102+
obj[key] = {};
103+
}
104+
105+
obj = obj[key];
106+
}
107+
108+
obj[keyList[i]] = val;
109+
}

Diff for: test/autoProcess/sourceMaps.test.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { transformer as scssTransformer } from '../../src/transformers/scss';
99
import { transformer as stylusTransformer } from '../../src/transformers/stylus';
1010
import { transformer as typescriptTransformer } from '../../src/transformers/typescript';
1111
import { SOURCE_MAP_PROP_MAP } from '../../src/modules/language';
12+
import { setProp } from '../../src/modules/utils';
1213

1314
const TRANSFORMERS: Record<string, any> = {
1415
babel: {
@@ -84,13 +85,15 @@ describe(`sourcemap generation`, () => {
8485
[transformerName]: true,
8586
});
8687

87-
const [key, val] = SOURCE_MAP_PROP_MAP[transformerName];
88+
const expectedOptions = {};
89+
90+
setProp(expectedOptions, ...SOURCE_MAP_PROP_MAP[transformerName]);
8891

8992
await preprocess(template, opts);
9093

9194
expect(transformer).toHaveBeenCalledWith(
9295
expect.objectContaining({
93-
options: expect.objectContaining({ [key]: val }),
96+
options: expect.objectContaining(expectedOptions),
9497
}),
9598
);
9699
});

0 commit comments

Comments
 (0)