From ea2854ae7395c67ad4ee3e2c242713b4baee2e3e Mon Sep 17 00:00:00 2001 From: bluwy Date: Wed, 13 Oct 2021 11:35:28 +0800 Subject: [PATCH] fix: use object type --- site/content/docs/04-compile-time.md | 2 +- src/compiler/compile/Component.ts | 3 ++- src/compiler/compile/render_dom/index.ts | 3 ++- src/compiler/compile/render_ssr/index.ts | 3 ++- src/compiler/compile/utils/check_enable_sourcemap.ts | 10 ++++++++++ src/compiler/interfaces.ts | 4 +++- test/sourcemaps/samples/only-css-sourcemap/_config.js | 2 +- test/sourcemaps/samples/only-js-sourcemap/_config.js | 2 +- 8 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 src/compiler/compile/utils/check_enable_sourcemap.ts diff --git a/site/content/docs/04-compile-time.md b/site/content/docs/04-compile-time.md index 948cf1713e..8e709d7c37 100644 --- a/site/content/docs/04-compile-time.md +++ b/site/content/docs/04-compile-time.md @@ -83,7 +83,7 @@ The following options can be passed to the compiler. None are required: | `preserveComments` | `false` | If `true`, your HTML comments will be preserved during server-side rendering. By default, they are stripped out. | `preserveWhitespace` | `false` | If `true`, whitespace inside and between elements is kept as you typed it, rather than removed or collapsed to a single space where possible. | `sourcemap` | `object | string` | An initial sourcemap that will be merged into the final output sourcemap. This is usually the preprocessor sourcemap. -| `enableSourcemap` | `boolean | 'js' | 'css'` | If `true`, Svelte generate sourcemaps for components. Use `js` or `css` for more granular control of sourcemap generation. By default, this is `true`. +| `enableSourcemap` | `boolean | { js: boolean; css: boolean; }` | If `true`, Svelte generate sourcemaps for components. Use an object with `js` or `css` for more granular control of sourcemap generation. By default, this is `true`. | `outputFilename` | `null` | A `string` used for your JavaScript sourcemap. | `cssOutputFilename` | `null` | A `string` used for your CSS sourcemap. | `sveltePath` | `"svelte"` | The location of the `svelte` package. Any imports from `svelte` or `svelte/[module]` will be modified accordingly. diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index 885c3391e4..ef440d5560 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -36,6 +36,7 @@ import { clone } from '../utils/clone'; import compiler_warnings from './compiler_warnings'; import compiler_errors from './compiler_errors'; import { extract_ignores_above_position, extract_svelte_ignore_from_comments } from '../utils/extract_svelte_ignore'; +import check_enable_sourcemap from './utils/check_enable_sourcemap'; interface ComponentOptions { namespace?: string; @@ -343,7 +344,7 @@ export default class Component { ? { code: null, map: null } : result.css; - const jsSourcemapEnabled = compile_options.enableSourcemap === true || compile_options.enableSourcemap === 'js'; + const jsSourcemapEnabled = check_enable_sourcemap(compile_options.enableSourcemap, 'js'); if (!jsSourcemapEnabled) { js = print(program); diff --git a/src/compiler/compile/render_dom/index.ts b/src/compiler/compile/render_dom/index.ts index 26f9ad9de3..01110b95a5 100644 --- a/src/compiler/compile/render_dom/index.ts +++ b/src/compiler/compile/render_dom/index.ts @@ -10,6 +10,7 @@ import { ImportDeclaration, ClassDeclaration, FunctionExpression, Node, Statemen import { apply_preprocessor_sourcemap } from '../../utils/mapped_code'; import { RawSourceMap, DecodedSourceMap } from '@ampproject/remapping/dist/types/types'; import { flatten } from '../../utils/flatten'; +import check_enable_sourcemap from '../utils/check_enable_sourcemap'; export default function dom( component: Component, @@ -34,7 +35,7 @@ export default function dom( const css = component.stylesheet.render(options.filename, !options.customElement); - const cssSourcemapEnabled = options.enableSourcemap === true || options.enableSourcemap === 'css'; + const cssSourcemapEnabled = check_enable_sourcemap(options.enableSourcemap, 'css'); if (cssSourcemapEnabled) { css.map = apply_preprocessor_sourcemap(options.filename, css.map, options.sourcemap as string | RawSourceMap | DecodedSourceMap); diff --git a/src/compiler/compile/render_ssr/index.ts b/src/compiler/compile/render_ssr/index.ts index e7ee9dfe5e..696444e68c 100644 --- a/src/compiler/compile/render_ssr/index.ts +++ b/src/compiler/compile/render_ssr/index.ts @@ -10,6 +10,7 @@ import { extract_names } from 'periscopic'; import { walk } from 'estree-walker'; import { invalidate } from '../render_dom/invalidate'; +import check_enable_sourcemap from '../utils/check_enable_sourcemap'; export default function ssr( component: Component, @@ -200,7 +201,7 @@ export default function ssr( main ].filter(Boolean); - const cssSourcemapEnabled = options.enableSourcemap === true || options.enableSourcemap === 'css'; + const cssSourcemapEnabled = check_enable_sourcemap(options.enableSourcemap, 'css'); const js = b` ${css.code ? b` diff --git a/src/compiler/compile/utils/check_enable_sourcemap.ts b/src/compiler/compile/utils/check_enable_sourcemap.ts new file mode 100644 index 0000000000..51f07c353a --- /dev/null +++ b/src/compiler/compile/utils/check_enable_sourcemap.ts @@ -0,0 +1,10 @@ +import { EnableSourcemap } from '../../interfaces'; + +export default function check_enable_sourcemap( + enable_sourcemap: EnableSourcemap, + namespace: keyof Extract +) { + return typeof enable_sourcemap === 'boolean' + ? enable_sourcemap + : enable_sourcemap[namespace]; +} diff --git a/src/compiler/interfaces.ts b/src/compiler/interfaces.ts index 3b2f93278e..b999fbd803 100644 --- a/src/compiler/interfaces.ts +++ b/src/compiler/interfaces.ts @@ -131,6 +131,8 @@ export interface Warning { export type ModuleFormat = 'esm' | 'cjs'; +export type EnableSourcemap = boolean | { js: boolean; css: boolean }; + export type CssHashGetter = (args: { name: string; filename: string | undefined; @@ -147,7 +149,7 @@ export interface CompileOptions { varsReport?: 'full' | 'strict' | false; sourcemap?: object | string; - enableSourcemap?: boolean | 'js' | 'css'; + enableSourcemap?: EnableSourcemap; outputFilename?: string; cssOutputFilename?: string; sveltePath?: string; diff --git a/test/sourcemaps/samples/only-css-sourcemap/_config.js b/test/sourcemaps/samples/only-css-sourcemap/_config.js index 011ea33f91..767e10a4b9 100644 --- a/test/sourcemaps/samples/only-css-sourcemap/_config.js +++ b/test/sourcemaps/samples/only-css-sourcemap/_config.js @@ -1,5 +1,5 @@ export default { compile_options: { - enableSourcemap: 'css' + enableSourcemap: { css: true } } }; diff --git a/test/sourcemaps/samples/only-js-sourcemap/_config.js b/test/sourcemaps/samples/only-js-sourcemap/_config.js index 9a5bd733a1..0b3b7987f1 100644 --- a/test/sourcemaps/samples/only-js-sourcemap/_config.js +++ b/test/sourcemaps/samples/only-js-sourcemap/_config.js @@ -1,5 +1,5 @@ export default { compile_options: { - enableSourcemap: 'js' + enableSourcemap: { js: true } } };