From d2c1e0eb02feb74ccef3dc2d01f173996a40e2bf Mon Sep 17 00:00:00 2001 From: bluwy Date: Mon, 11 Oct 2021 23:45:40 +0800 Subject: [PATCH 1/8] feat: add enableSourcemap option --- src/compiler/compile/Component.ts | 29 +++++++++++++++--------- src/compiler/compile/index.ts | 3 ++- src/compiler/compile/render_dom/index.ts | 12 +++++++--- src/compiler/compile/render_ssr/index.ts | 4 +++- src/compiler/interfaces.ts | 1 + 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index 202e14fd9813..346c1a869b86 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -342,22 +342,29 @@ export default class Component { css = compile_options.customElement ? { code: null, map: null } : result.css; + + const jsSourcemapEnabled = compile_options.enableSourcemap === true || compile_options.enableSourcemap === 'js' - const sourcemap_source_filename = get_sourcemap_source_filename(compile_options); + if (!jsSourcemapEnabled) { + js = print(program); + js.map = null; + } else { + const sourcemap_source_filename = get_sourcemap_source_filename(compile_options); - js = print(program, { - sourceMapSource: sourcemap_source_filename - }); + js = print(program, { + sourceMapSource: sourcemap_source_filename, + }); - js.map.sources = [ - sourcemap_source_filename - ]; + js.map.sources = [ + sourcemap_source_filename + ]; - js.map.sourcesContent = [ - this.source - ]; + js.map.sourcesContent = [ + this.source + ]; - js.map = apply_preprocessor_sourcemap(sourcemap_source_filename, js.map, compile_options.sourcemap as (string | RawSourceMap | DecodedSourceMap)); + js.map = apply_preprocessor_sourcemap(sourcemap_source_filename, js.map, compile_options.sourcemap as (string | RawSourceMap | DecodedSourceMap)); + } } return { diff --git a/src/compiler/compile/index.ts b/src/compiler/compile/index.ts index 96b24bceeed8..afe9c56cf4ca 100644 --- a/src/compiler/compile/index.ts +++ b/src/compiler/compile/index.ts @@ -13,6 +13,7 @@ const valid_options = [ 'name', 'filename', 'sourcemap', + 'enableSourcemap', 'generate', 'errorMode', 'varsReport', @@ -82,7 +83,7 @@ function validate_options(options: CompileOptions, warnings: Warning[]) { } export default function compile(source: string, options: CompileOptions = {}) { - options = Object.assign({ generate: 'dom', dev: false }, options); + options = Object.assign({ generate: 'dom', dev: false, enableSourcemap: true }, options); const stats = new Stats(); const warnings = []; diff --git a/src/compiler/compile/render_dom/index.ts b/src/compiler/compile/render_dom/index.ts index f74f4cdf1ccb..26f9ad9de3c1 100644 --- a/src/compiler/compile/render_dom/index.ts +++ b/src/compiler/compile/render_dom/index.ts @@ -33,10 +33,16 @@ export default function dom( } const css = component.stylesheet.render(options.filename, !options.customElement); + + const cssSourcemapEnabled = options.enableSourcemap === true || options.enableSourcemap === 'css'; - css.map = apply_preprocessor_sourcemap(options.filename, css.map, options.sourcemap as string | RawSourceMap | DecodedSourceMap); + if (cssSourcemapEnabled) { + css.map = apply_preprocessor_sourcemap(options.filename, css.map, options.sourcemap as string | RawSourceMap | DecodedSourceMap); + } else { + css.map = null; + } - const styles = component.stylesheet.has_styles && options.dev + const styles = cssSourcemapEnabled && component.stylesheet.has_styles && options.dev ? `${css.code}\n/*# sourceMappingURL=${css.map.toUrl()} */` : css.code; @@ -521,7 +527,7 @@ export default function dom( constructor(options) { super(); - ${css.code && b`this.shadowRoot.innerHTML = \`\`;`} + ${css.code && b`this.shadowRoot.innerHTML = \`\`;`} @init(this, { target: this.shadowRoot, props: ${init_props}, customElement: true }, ${definition}, ${has_create_fragment ? 'create_fragment' : 'null'}, ${not_equal}, ${prop_indexes}, null, ${dirty}); diff --git a/src/compiler/compile/render_ssr/index.ts b/src/compiler/compile/render_ssr/index.ts index b35a6ce6ffee..e7ee9dfe5e8e 100644 --- a/src/compiler/compile/render_ssr/index.ts +++ b/src/compiler/compile/render_ssr/index.ts @@ -200,11 +200,13 @@ export default function ssr( main ].filter(Boolean); + const cssSourcemapEnabled = options.enableSourcemap === true || options.enableSourcemap === 'css'; + const js = b` ${css.code ? b` const #css = { code: "${css.code}", - map: ${css.map ? string_literal(css.map.toString()) : 'null'} + map: ${cssSourcemapEnabled && css.map ? string_literal(css.map.toString()) : 'null'} };` : null} ${component.extract_javascript(component.ast.module)} diff --git a/src/compiler/interfaces.ts b/src/compiler/interfaces.ts index 7446c4c14b57..3b2f93278e76 100644 --- a/src/compiler/interfaces.ts +++ b/src/compiler/interfaces.ts @@ -147,6 +147,7 @@ export interface CompileOptions { varsReport?: 'full' | 'strict' | false; sourcemap?: object | string; + enableSourcemap?: boolean | 'js' | 'css'; outputFilename?: string; cssOutputFilename?: string; sveltePath?: string; From 35d4fd7e33876d5a4b0293e116658bbb91880d6e Mon Sep 17 00:00:00 2001 From: bluwy Date: Mon, 11 Oct 2021 23:45:57 +0800 Subject: [PATCH 2/8] chore: add tests --- test/sourcemaps/index.ts | 12 +++++++----- test/sourcemaps/samples/no-sourcemap/_config.js | 5 +++++ test/sourcemaps/samples/no-sourcemap/input.svelte | 11 +++++++++++ test/sourcemaps/samples/no-sourcemap/test.js | 4 ++++ .../sourcemaps/samples/only-css-sourcemap/_config.js | 5 +++++ .../samples/only-css-sourcemap/input.svelte | 11 +++++++++++ test/sourcemaps/samples/only-css-sourcemap/test.js | 4 ++++ test/sourcemaps/samples/only-js-sourcemap/_config.js | 5 +++++ .../samples/only-js-sourcemap/input.svelte | 11 +++++++++++ test/sourcemaps/samples/only-js-sourcemap/test.js | 4 ++++ 10 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 test/sourcemaps/samples/no-sourcemap/_config.js create mode 100644 test/sourcemaps/samples/no-sourcemap/input.svelte create mode 100644 test/sourcemaps/samples/no-sourcemap/test.js create mode 100644 test/sourcemaps/samples/only-css-sourcemap/_config.js create mode 100644 test/sourcemaps/samples/only-css-sourcemap/input.svelte create mode 100644 test/sourcemaps/samples/only-css-sourcemap/test.js create mode 100644 test/sourcemaps/samples/only-js-sourcemap/_config.js create mode 100644 test/sourcemaps/samples/only-js-sourcemap/input.svelte create mode 100644 test/sourcemaps/samples/only-js-sourcemap/test.js diff --git a/test/sourcemaps/index.ts b/test/sourcemaps/index.ts index 903629c06b1b..fc021cd6a7ca 100644 --- a/test/sourcemaps/index.ts +++ b/test/sourcemaps/index.ts @@ -85,11 +85,13 @@ describe('sourcemaps', () => { ); } - assert.deepEqual( - js.map.sources.slice().sort(), - (config.js_map_sources || ['input.svelte']).sort(), - 'js.map.sources is wrong' - ); + if (js.map) { + assert.deepEqual( + js.map.sources.slice().sort(), + (config.js_map_sources || ['input.svelte']).sort(), + 'js.map.sources is wrong' + ); + } if (css.map) { assert.deepEqual( css.map.sources.slice().sort(), diff --git a/test/sourcemaps/samples/no-sourcemap/_config.js b/test/sourcemaps/samples/no-sourcemap/_config.js new file mode 100644 index 000000000000..5b84d4055408 --- /dev/null +++ b/test/sourcemaps/samples/no-sourcemap/_config.js @@ -0,0 +1,5 @@ +export default { + compile_options: { + enableSourcemap: false + } +}; diff --git a/test/sourcemaps/samples/no-sourcemap/input.svelte b/test/sourcemaps/samples/no-sourcemap/input.svelte new file mode 100644 index 000000000000..91a483c9aa37 --- /dev/null +++ b/test/sourcemaps/samples/no-sourcemap/input.svelte @@ -0,0 +1,11 @@ + + +

{foo}

+ + \ No newline at end of file diff --git a/test/sourcemaps/samples/no-sourcemap/test.js b/test/sourcemaps/samples/no-sourcemap/test.js new file mode 100644 index 000000000000..127459a54e6e --- /dev/null +++ b/test/sourcemaps/samples/no-sourcemap/test.js @@ -0,0 +1,4 @@ +export function test({ assert, js, css }) { + assert.equal(js.map, null); + assert.equal(css.map, null); +} diff --git a/test/sourcemaps/samples/only-css-sourcemap/_config.js b/test/sourcemaps/samples/only-css-sourcemap/_config.js new file mode 100644 index 000000000000..011ea33f9193 --- /dev/null +++ b/test/sourcemaps/samples/only-css-sourcemap/_config.js @@ -0,0 +1,5 @@ +export default { + compile_options: { + enableSourcemap: 'css' + } +}; diff --git a/test/sourcemaps/samples/only-css-sourcemap/input.svelte b/test/sourcemaps/samples/only-css-sourcemap/input.svelte new file mode 100644 index 000000000000..91a483c9aa37 --- /dev/null +++ b/test/sourcemaps/samples/only-css-sourcemap/input.svelte @@ -0,0 +1,11 @@ + + +

{foo}

+ + \ No newline at end of file diff --git a/test/sourcemaps/samples/only-css-sourcemap/test.js b/test/sourcemaps/samples/only-css-sourcemap/test.js new file mode 100644 index 000000000000..a7ac6a9b0bdd --- /dev/null +++ b/test/sourcemaps/samples/only-css-sourcemap/test.js @@ -0,0 +1,4 @@ +export function test({ assert, js, css }) { + assert.equal(js.map, null); + assert.notEqual(css.map, null); +} diff --git a/test/sourcemaps/samples/only-js-sourcemap/_config.js b/test/sourcemaps/samples/only-js-sourcemap/_config.js new file mode 100644 index 000000000000..9a5bd733a1b5 --- /dev/null +++ b/test/sourcemaps/samples/only-js-sourcemap/_config.js @@ -0,0 +1,5 @@ +export default { + compile_options: { + enableSourcemap: 'js' + } +}; diff --git a/test/sourcemaps/samples/only-js-sourcemap/input.svelte b/test/sourcemaps/samples/only-js-sourcemap/input.svelte new file mode 100644 index 000000000000..91a483c9aa37 --- /dev/null +++ b/test/sourcemaps/samples/only-js-sourcemap/input.svelte @@ -0,0 +1,11 @@ + + +

{foo}

+ + \ No newline at end of file diff --git a/test/sourcemaps/samples/only-js-sourcemap/test.js b/test/sourcemaps/samples/only-js-sourcemap/test.js new file mode 100644 index 000000000000..b150653c3d72 --- /dev/null +++ b/test/sourcemaps/samples/only-js-sourcemap/test.js @@ -0,0 +1,4 @@ +export function test({ assert, js, css }) { + assert.notEqual(js.map, null); + assert.equal(css.map, null); +} From 5c4d77d939b704df4aa20748b540ad414f6a2c00 Mon Sep 17 00:00:00 2001 From: bluwy Date: Mon, 11 Oct 2021 23:55:50 +0800 Subject: [PATCH 3/8] chore: add docs --- site/content/docs/04-compile-time.md | 1 + src/compiler/compile/Component.ts | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/site/content/docs/04-compile-time.md b/site/content/docs/04-compile-time.md index ed6977f81fc8..3dd59244ccc8 100644 --- a/site/content/docs/04-compile-time.md +++ b/site/content/docs/04-compile-time.md @@ -82,6 +82,7 @@ The following options can be passed to the compiler. None are required: | `loopGuardTimeout` | 0 | A `number` that tells Svelte to break the loop if it blocks the thread for more than `loopGuardTimeout` ms. This is useful to prevent infinite loops. **Only available when `dev: true`** | `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. +| `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`. | `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 346c1a869b86..885c3391e41e 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -343,7 +343,7 @@ export default class Component { ? { code: null, map: null } : result.css; - const jsSourcemapEnabled = compile_options.enableSourcemap === true || compile_options.enableSourcemap === 'js' + const jsSourcemapEnabled = compile_options.enableSourcemap === true || compile_options.enableSourcemap === 'js'; if (!jsSourcemapEnabled) { js = print(program); @@ -352,7 +352,7 @@ export default class Component { const sourcemap_source_filename = get_sourcemap_source_filename(compile_options); js = print(program, { - sourceMapSource: sourcemap_source_filename, + sourceMapSource: sourcemap_source_filename }); js.map.sources = [ From 8a7ce000c0dcfb8e96e5f57c9ba536ba90f2ec6a Mon Sep 17 00:00:00 2001 From: bluwy Date: Wed, 13 Oct 2021 00:25:19 +0800 Subject: [PATCH 4/8] chore: apply pr suggestions --- site/content/docs/04-compile-time.md | 1 + test/sourcemaps/samples/no-sourcemap/input.svelte | 2 +- test/sourcemaps/samples/only-css-sourcemap/input.svelte | 2 +- test/sourcemaps/samples/only-js-sourcemap/input.svelte | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/site/content/docs/04-compile-time.md b/site/content/docs/04-compile-time.md index 3dd59244ccc8..2848cfb7f499 100644 --- a/site/content/docs/04-compile-time.md +++ b/site/content/docs/04-compile-time.md @@ -82,6 +82,7 @@ The following options can be passed to the compiler. None are required: | `loopGuardTimeout` | 0 | A `number` that tells Svelte to break the loop if it blocks the thread for more than `loopGuardTimeout` ms. This is useful to prevent infinite loops. **Only available when `dev: true`** | `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. | `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`. | `outputFilename` | `null` | A `string` used for your JavaScript sourcemap. | `cssOutputFilename` | `null` | A `string` used for your CSS sourcemap. diff --git a/test/sourcemaps/samples/no-sourcemap/input.svelte b/test/sourcemaps/samples/no-sourcemap/input.svelte index 91a483c9aa37..6d39eaad0e79 100644 --- a/test/sourcemaps/samples/no-sourcemap/input.svelte +++ b/test/sourcemaps/samples/no-sourcemap/input.svelte @@ -8,4 +8,4 @@ p { color: red; } - \ No newline at end of file + diff --git a/test/sourcemaps/samples/only-css-sourcemap/input.svelte b/test/sourcemaps/samples/only-css-sourcemap/input.svelte index 91a483c9aa37..6d39eaad0e79 100644 --- a/test/sourcemaps/samples/only-css-sourcemap/input.svelte +++ b/test/sourcemaps/samples/only-css-sourcemap/input.svelte @@ -8,4 +8,4 @@ p { color: red; } - \ No newline at end of file + diff --git a/test/sourcemaps/samples/only-js-sourcemap/input.svelte b/test/sourcemaps/samples/only-js-sourcemap/input.svelte index 91a483c9aa37..6d39eaad0e79 100644 --- a/test/sourcemaps/samples/only-js-sourcemap/input.svelte +++ b/test/sourcemaps/samples/only-js-sourcemap/input.svelte @@ -8,4 +8,4 @@ p { color: red; } - \ No newline at end of file + From 81b0ddf157d9671018c7b787ef0c1dd6a65d9c60 Mon Sep 17 00:00:00 2001 From: bluwy Date: Wed, 13 Oct 2021 00:31:32 +0800 Subject: [PATCH 5/8] docs: further explain sourcemap --- site/content/docs/04-compile-time.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/content/docs/04-compile-time.md b/site/content/docs/04-compile-time.md index 2848cfb7f499..9459829edfae 100644 --- a/site/content/docs/04-compile-time.md +++ b/site/content/docs/04-compile-time.md @@ -82,7 +82,7 @@ The following options can be passed to the compiler. None are required: | `loopGuardTimeout` | 0 | A `number` that tells Svelte to break the loop if it blocks the thread for more than `loopGuardTimeout` ms. This is useful to prevent infinite loops. **Only available when `dev: true`** | `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. +| `sourcemap` | `object | string` | An initial sourcemap that will be merged into the final output sourcemap. Usually assigned with the preprocess 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`. | `outputFilename` | `null` | A `string` used for your JavaScript sourcemap. | `cssOutputFilename` | `null` | A `string` used for your CSS sourcemap. From 797818dc1573f2f271d2bb8b50362f80479d1329 Mon Sep 17 00:00:00 2001 From: Bjorn Lu <34116392+bluwy@users.noreply.github.com> Date: Wed, 13 Oct 2021 01:20:28 +0800 Subject: [PATCH 6/8] Update site/content/docs/04-compile-time.md Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> --- site/content/docs/04-compile-time.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/site/content/docs/04-compile-time.md b/site/content/docs/04-compile-time.md index 9459829edfae..948cf1713ec7 100644 --- a/site/content/docs/04-compile-time.md +++ b/site/content/docs/04-compile-time.md @@ -82,7 +82,7 @@ The following options can be passed to the compiler. None are required: | `loopGuardTimeout` | 0 | A `number` that tells Svelte to break the loop if it blocks the thread for more than `loopGuardTimeout` ms. This is useful to prevent infinite loops. **Only available when `dev: true`** | `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. Usually assigned with the preprocess sourcemap. +| `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`. | `outputFilename` | `null` | A `string` used for your JavaScript sourcemap. | `cssOutputFilename` | `null` | A `string` used for your CSS sourcemap. From ea2854ae7395c67ad4ee3e2c242713b4baee2e3e Mon Sep 17 00:00:00 2001 From: bluwy Date: Wed, 13 Oct 2021 11:35:28 +0800 Subject: [PATCH 7/8] 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 948cf1713ec7..8e709d7c37cc 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 885c3391e41e..ef440d556015 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 26f9ad9de3c1..01110b95a55e 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 e7ee9dfe5e8e..696444e68c67 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 000000000000..51f07c353ac1 --- /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 3b2f93278e76..b999fbd803dd 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 011ea33f9193..767e10a4b9af 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 9a5bd733a1b5..0b3b7987f174 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 } } }; From 5544b15c33ead16581d0d65440ecccb53e9955c2 Mon Sep 17 00:00:00 2001 From: Conduitry Date: Sun, 17 Oct 2021 12:55:22 -0400 Subject: [PATCH 8/8] snake_case --- src/compiler/compile/Component.ts | 6 +++--- src/compiler/compile/render_dom/index.ts | 10 +++++----- src/compiler/compile/render_ssr/index.ts | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/compiler/compile/Component.ts b/src/compiler/compile/Component.ts index ef440d556015..4d4cf909fda6 100644 --- a/src/compiler/compile/Component.ts +++ b/src/compiler/compile/Component.ts @@ -343,10 +343,10 @@ export default class Component { css = compile_options.customElement ? { code: null, map: null } : result.css; - - const jsSourcemapEnabled = check_enable_sourcemap(compile_options.enableSourcemap, 'js'); - if (!jsSourcemapEnabled) { + const js_sourcemap_enabled = check_enable_sourcemap(compile_options.enableSourcemap, 'js'); + + if (!js_sourcemap_enabled) { js = print(program); js.map = null; } else { diff --git a/src/compiler/compile/render_dom/index.ts b/src/compiler/compile/render_dom/index.ts index 01110b95a55e..89af0c297c51 100644 --- a/src/compiler/compile/render_dom/index.ts +++ b/src/compiler/compile/render_dom/index.ts @@ -34,16 +34,16 @@ export default function dom( } const css = component.stylesheet.render(options.filename, !options.customElement); - - const cssSourcemapEnabled = check_enable_sourcemap(options.enableSourcemap, 'css'); - if (cssSourcemapEnabled) { + const css_sourcemap_enabled = check_enable_sourcemap(options.enableSourcemap, 'css'); + + if (css_sourcemap_enabled) { css.map = apply_preprocessor_sourcemap(options.filename, css.map, options.sourcemap as string | RawSourceMap | DecodedSourceMap); } else { css.map = null; } - const styles = cssSourcemapEnabled && component.stylesheet.has_styles && options.dev + const styles = css_sourcemap_enabled && component.stylesheet.has_styles && options.dev ? `${css.code}\n/*# sourceMappingURL=${css.map.toUrl()} */` : css.code; @@ -528,7 +528,7 @@ export default function dom( constructor(options) { super(); - ${css.code && b`this.shadowRoot.innerHTML = \`\`;`} + ${css.code && b`this.shadowRoot.innerHTML = \`\`;`} @init(this, { target: this.shadowRoot, props: ${init_props}, customElement: true }, ${definition}, ${has_create_fragment ? 'create_fragment' : 'null'}, ${not_equal}, ${prop_indexes}, null, ${dirty}); diff --git a/src/compiler/compile/render_ssr/index.ts b/src/compiler/compile/render_ssr/index.ts index 696444e68c67..9d2c1cc60b68 100644 --- a/src/compiler/compile/render_ssr/index.ts +++ b/src/compiler/compile/render_ssr/index.ts @@ -201,13 +201,13 @@ export default function ssr( main ].filter(Boolean); - const cssSourcemapEnabled = check_enable_sourcemap(options.enableSourcemap, 'css'); - + const css_sourcemap_enabled = check_enable_sourcemap(options.enableSourcemap, 'css'); + const js = b` ${css.code ? b` const #css = { code: "${css.code}", - map: ${cssSourcemapEnabled && css.map ? string_literal(css.map.toString()) : 'null'} + map: ${css_sourcemap_enabled && css.map ? string_literal(css.map.toString()) : 'null'} };` : null} ${component.extract_javascript(component.ast.module)}