Skip to content

Commit

Permalink
fix: translate options.sourceMap to options.compilerOptions.sourceMap…
Browse files Browse the repository at this point in the history
… 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]>
  • Loading branch information
milahu and kaisermann authored Jan 21, 2021
1 parent c23f11a commit c8a3cd6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
6 changes: 2 additions & 4 deletions src/autoProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
Transformers,
Options,
} from './types';
import { hasDepInstalled, concat } from './modules/utils';
import { hasDepInstalled, concat, setProp } from './modules/utils';
import { getTagInfo } from './modules/tagInfo';
import {
addLanguageAlias,
Expand Down Expand Up @@ -127,9 +127,7 @@ export function sveltePreprocess(
}

if (sourceMap && name in SOURCE_MAP_PROP_MAP) {
const [propName, value] = SOURCE_MAP_PROP_MAP[name];

opts[propName] = value;
setProp(opts, ...SOURCE_MAP_PROP_MAP[name]);
}

return opts;
Expand Down
18 changes: 9 additions & 9 deletions src/modules/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ export function getLanguageDefaults(lang: string): null | Record<string, any> {
return defaults;
}

export const SOURCE_MAP_PROP_MAP: Record<string, [string, any]> = {
babel: ['sourceMaps', true],
typescript: ['sourceMap', true],
scss: ['sourceMap', true],
less: ['sourceMap', {}],
stylus: ['sourcemap', true],
postcss: ['map', true],
coffeescript: ['sourceMap', true],
globalStyle: ['sourceMap', true],
export const SOURCE_MAP_PROP_MAP: Record<string, [string[], any]> = {
babel: [['sourceMaps'], true],
typescript: [['compilerOptions', 'sourceMap'], true],
scss: [['sourceMap'], true],
less: [['sourceMap'], {}],
stylus: [['sourcemap'], true],
postcss: [['map'], true],
coffeescript: [['sourceMap'], true],
globalStyle: [['sourceMap'], true],
};

export const ALIAS_MAP = new Map([
Expand Down
17 changes: 17 additions & 0 deletions src/modules/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,20 @@ export function findUp({ what, from }) {

return null;
}

// set deep property in object
export function setProp(obj, keyList, val) {
let i = 0;

for (; i < keyList.length - 1; i++) {
const key = keyList[i];

if (typeof obj[key] !== 'object') {
obj[key] = {};
}

obj = obj[key];
}

obj[keyList[i]] = val;
}
7 changes: 5 additions & 2 deletions test/autoProcess/sourceMaps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { transformer as scssTransformer } from '../../src/transformers/scss';
import { transformer as stylusTransformer } from '../../src/transformers/stylus';
import { transformer as typescriptTransformer } from '../../src/transformers/typescript';
import { SOURCE_MAP_PROP_MAP } from '../../src/modules/language';
import { setProp } from '../../src/modules/utils';

const TRANSFORMERS: Record<string, any> = {
babel: {
Expand Down Expand Up @@ -84,13 +85,15 @@ describe(`sourcemap generation`, () => {
[transformerName]: true,
});

const [key, val] = SOURCE_MAP_PROP_MAP[transformerName];
const expectedOptions = {};

setProp(expectedOptions, ...SOURCE_MAP_PROP_MAP[transformerName]);

await preprocess(template, opts);

expect(transformer).toHaveBeenCalledWith(
expect.objectContaining({
options: expect.objectContaining({ [key]: val }),
options: expect.objectContaining(expectedOptions),
}),
);
});
Expand Down

0 comments on commit c8a3cd6

Please sign in to comment.