From c8a3cd6736054510bc0eeccc242b8f5c4f0b7c5a Mon Sep 17 00:00:00 2001 From: milahu Date: Thu, 21 Jan 2021 23:38:00 +0100 Subject: [PATCH] fix: translate options.sourceMap to options.compilerOptions.sourceMap for ts (#286) (#299) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- src/autoProcess.ts | 6 ++---- src/modules/language.ts | 18 +++++++++--------- src/modules/utils.ts | 17 +++++++++++++++++ test/autoProcess/sourceMaps.test.ts | 7 +++++-- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/autoProcess.ts b/src/autoProcess.ts index ef2e5ae4..7917eacb 100644 --- a/src/autoProcess.ts +++ b/src/autoProcess.ts @@ -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, @@ -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; diff --git a/src/modules/language.ts b/src/modules/language.ts index ae1fbe35..255a160d 100644 --- a/src/modules/language.ts +++ b/src/modules/language.ts @@ -36,15 +36,15 @@ export function getLanguageDefaults(lang: string): null | Record { return defaults; } -export const SOURCE_MAP_PROP_MAP: Record = { - 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 = { + 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([ diff --git a/src/modules/utils.ts b/src/modules/utils.ts index 41184f92..58f50b52 100644 --- a/src/modules/utils.ts +++ b/src/modules/utils.ts @@ -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; +} diff --git a/test/autoProcess/sourceMaps.test.ts b/test/autoProcess/sourceMaps.test.ts index e24b1ba1..ff49e7d4 100644 --- a/test/autoProcess/sourceMaps.test.ts +++ b/test/autoProcess/sourceMaps.test.ts @@ -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 = { babel: { @@ -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), }), ); });