Skip to content

Commit

Permalink
[feat] add enableSourcemap option (#6835)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Oct 17, 2021
1 parent 3f11232 commit be1eff0
Show file tree
Hide file tree
Showing 17 changed files with 117 additions and 21 deletions.
2 changes: 2 additions & 0 deletions site/content/docs/04-compile-time.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ 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. This is usually the preprocessor sourcemap.
| `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.
Expand Down
30 changes: 19 additions & 11 deletions src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -343,21 +344,28 @@ export default class Component {
? { code: null, map: null }
: result.css;

const sourcemap_source_filename = get_sourcemap_source_filename(compile_options);
const js_sourcemap_enabled = check_enable_sourcemap(compile_options.enableSourcemap, 'js');

js = print(program, {
sourceMapSource: sourcemap_source_filename
});
if (!js_sourcemap_enabled) {
js = print(program);
js.map = null;
} else {
const sourcemap_source_filename = get_sourcemap_source_filename(compile_options);

js.map.sources = [
sourcemap_source_filename
];
js = print(program, {
sourceMapSource: sourcemap_source_filename
});

js.map.sourcesContent = [
this.source
];
js.map.sources = [
sourcemap_source_filename
];

js.map = apply_preprocessor_sourcemap(sourcemap_source_filename, js.map, compile_options.sourcemap as (string | RawSourceMap | DecodedSourceMap));
js.map.sourcesContent = [
this.source
];

js.map = apply_preprocessor_sourcemap(sourcemap_source_filename, js.map, compile_options.sourcemap as (string | RawSourceMap | DecodedSourceMap));
}
}

return {
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const valid_options = [
'name',
'filename',
'sourcemap',
'enableSourcemap',
'generate',
'errorMode',
'varsReport',
Expand Down Expand Up @@ -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 = [];
Expand Down
13 changes: 10 additions & 3 deletions src/compiler/compile/render_dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -34,9 +35,15 @@ export default function dom(

const css = component.stylesheet.render(options.filename, !options.customElement);

css.map = apply_preprocessor_sourcemap(options.filename, css.map, options.sourcemap as string | RawSourceMap | DecodedSourceMap);
const css_sourcemap_enabled = check_enable_sourcemap(options.enableSourcemap, 'css');

const styles = component.stylesheet.has_styles && options.dev
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 = css_sourcemap_enabled && component.stylesheet.has_styles && options.dev
? `${css.code}\n/*# sourceMappingURL=${css.map.toUrl()} */`
: css.code;

Expand Down Expand Up @@ -521,7 +528,7 @@ export default function dom(
constructor(options) {
super();
${css.code && b`this.shadowRoot.innerHTML = \`<style>${css.code.replace(/\\/g, '\\\\')}${options.dev ? `\n/*# sourceMappingURL=${css.map.toUrl()} */` : ''}</style>\`;`}
${css.code && b`this.shadowRoot.innerHTML = \`<style>${css.code.replace(/\\/g, '\\\\')}${css_sourcemap_enabled && options.dev ? `\n/*# sourceMappingURL=${css.map.toUrl()} */` : ''}</style>\`;`}
@init(this, { target: this.shadowRoot, props: ${init_props}, customElement: true }, ${definition}, ${has_create_fragment ? 'create_fragment' : 'null'}, ${not_equal}, ${prop_indexes}, null, ${dirty});
Expand Down
5 changes: 4 additions & 1 deletion src/compiler/compile/render_ssr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -200,11 +201,13 @@ export default function ssr(
main
].filter(Boolean);

const css_sourcemap_enabled = check_enable_sourcemap(options.enableSourcemap, 'css');

const js = b`
${css.code ? b`
const #css = {
code: "${css.code}",
map: ${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)}
Expand Down
10 changes: 10 additions & 0 deletions src/compiler/compile/utils/check_enable_sourcemap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { EnableSourcemap } from '../../interfaces';

export default function check_enable_sourcemap(
enable_sourcemap: EnableSourcemap,
namespace: keyof Extract<EnableSourcemap, object>
) {
return typeof enable_sourcemap === 'boolean'
? enable_sourcemap
: enable_sourcemap[namespace];
}
3 changes: 3 additions & 0 deletions src/compiler/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -147,6 +149,7 @@ export interface CompileOptions {
varsReport?: 'full' | 'strict' | false;

sourcemap?: object | string;
enableSourcemap?: EnableSourcemap;
outputFilename?: string;
cssOutputFilename?: string;
sveltePath?: string;
Expand Down
12 changes: 7 additions & 5 deletions test/sourcemaps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
5 changes: 5 additions & 0 deletions test/sourcemaps/samples/no-sourcemap/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
compile_options: {
enableSourcemap: false
}
};
11 changes: 11 additions & 0 deletions test/sourcemaps/samples/no-sourcemap/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
export let foo;
</script>

<p>{foo}</p>

<style>
p {
color: red;
}
</style>
4 changes: 4 additions & 0 deletions test/sourcemaps/samples/no-sourcemap/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function test({ assert, js, css }) {
assert.equal(js.map, null);
assert.equal(css.map, null);
}
5 changes: 5 additions & 0 deletions test/sourcemaps/samples/only-css-sourcemap/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
compile_options: {
enableSourcemap: { css: true }
}
};
11 changes: 11 additions & 0 deletions test/sourcemaps/samples/only-css-sourcemap/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
export let foo;
</script>

<p>{foo}</p>

<style>
p {
color: red;
}
</style>
4 changes: 4 additions & 0 deletions test/sourcemaps/samples/only-css-sourcemap/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function test({ assert, js, css }) {
assert.equal(js.map, null);
assert.notEqual(css.map, null);
}
5 changes: 5 additions & 0 deletions test/sourcemaps/samples/only-js-sourcemap/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
compile_options: {
enableSourcemap: { js: true }
}
};
11 changes: 11 additions & 0 deletions test/sourcemaps/samples/only-js-sourcemap/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
export let foo;
</script>

<p>{foo}</p>

<style>
p {
color: red;
}
</style>
4 changes: 4 additions & 0 deletions test/sourcemaps/samples/only-js-sourcemap/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function test({ assert, js, css }) {
assert.notEqual(js.map, null);
assert.equal(css.map, null);
}

0 comments on commit be1eff0

Please sign in to comment.