Skip to content

fix typescript: translate options.sourceMap to options.compilerOptions.sourceMap (#286) #299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 deletions src/modules/language.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export function getLanguageDefaults(lang: string): null | Record<string, any> {
return defaults;
}

export const SOURCE_MAP_PROP_MAP: Record<string, [string, any]> = {
export const SOURCE_MAP_PROP_MAP: Record<string, any[]> = {
babel: ['sourceMaps', true],
typescript: ['sourceMap', true],
typescript: ['compilerOptions', 'sourceMap', true],
scss: ['sourceMap', true],
less: ['sourceMap', {}],
stylus: ['sourcemap', true],
Expand Down
18 changes: 18 additions & 0 deletions src/modules/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,21 @@ export function findUp({ what, from }) {

return null;
}

// set deep property in object
// args = array of prop-names, last arg = value
export function setProp(obj, ...args) {
let i = 0;

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

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

obj = obj[key];
}

obj[args[i]] = args[i + 1];
}
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