Skip to content
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

[feat] add enableSourcemap option #6835

Merged
merged 8 commits into from
Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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' | 'css'` | If `true`, Svelte generate sourcemaps for components. Use `js` or `css` for more granular control of sourcemap generation. By default, this is `true`.
bluwy marked this conversation as resolved.
Show resolved Hide resolved
| `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
29 changes: 18 additions & 11 deletions src/compiler/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
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
12 changes: 9 additions & 3 deletions src/compiler/compile/render_dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -521,7 +527,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, '\\\\')}${cssSourcemapEnabled && 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
4 changes: 3 additions & 1 deletion src/compiler/compile/render_ssr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down
1 change: 1 addition & 0 deletions src/compiler/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export interface CompileOptions {
varsReport?: 'full' | 'strict' | false;

sourcemap?: object | string;
enableSourcemap?: boolean | 'js' | 'css';
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'
}
};
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'
}
};
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);
}