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

chore: source maps for preprocessors + tests #10459

Merged
merged 26 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6e9a428
add preprocessor and source map option to test compileDirectory helper
dummdidumm Feb 12, 2024
dbfb0a9
start porting over tests to new style
dummdidumm Feb 12, 2024
7b46f3d
use preprocessor map, adjust test suite
dummdidumm Feb 12, 2024
5c6459f
sources comparison
dummdidumm Feb 12, 2024
60771e9
also test preprocessed Svelte code source maps
dummdidumm Feb 12, 2024
e799dac
fix sources string, more detailed css source map
dummdidumm Feb 13, 2024
135e733
fix bugs around source file path handling
dummdidumm Feb 14, 2024
46d242f
fix test setup
dummdidumm Feb 14, 2024
c9e17eb
skip test and note that it's skipped in Svelte 4, too
dummdidumm Feb 14, 2024
211a91a
add location in some places for better source maps
dummdidumm Feb 15, 2024
6f0ee66
use locator
dummdidumm Feb 15, 2024
07c7d2e
get external files tests working
dummdidumm Feb 15, 2024
b5f8745
inline css map in dev mode
dummdidumm Feb 15, 2024
21657f4
Merge branch 'main' into source-maps
dummdidumm Feb 15, 2024
d83de87
fix compiler options
dummdidumm Feb 15, 2024
bf74b67
update tests
dummdidumm Feb 15, 2024
577d9c0
only apply when injecting css into js
dummdidumm Feb 15, 2024
f9aa7c6
use TraceMap for more robust mapping checks
dummdidumm Feb 15, 2024
64c830f
try this
dummdidumm Feb 15, 2024
4e6b5a2
try this
dummdidumm Feb 15, 2024
913326a
fix
dummdidumm Feb 15, 2024
0e5daa9
debug
dummdidumm Feb 15, 2024
1d5b7f6
ffs
dummdidumm Feb 15, 2024
917f9c5
dude..
dummdidumm Feb 15, 2024
041d6fc
remove enableSourcemap option
dummdidumm Feb 15, 2024
86f2de6
changeset
dummdidumm Feb 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silly-laws-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte": patch
---

fix: add proper source map support
30 changes: 20 additions & 10 deletions packages/svelte/src/compiler/css/Stylesheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import MagicString from 'magic-string';
import { walk } from 'zimmerframe';
import { ComplexSelector } from './Selector.js';
import { hash } from './utils.js';
// import compiler_warnings from '../compiler_warnings.js';
// import { extract_ignores_above_position } from '../utils/extract_svelte_ignore.js';
import { create_attribute } from '../phases/nodes.js'; // TODO move this
import { merge_with_preprocessor_map } from '../utils/mapped_code.js';

const regex_css_browser_prefix = /^-((webkit)|(moz)|(o)|(ms))-/;
const regex_name_boundary = /^[\s,;}]$/;
Expand Down Expand Up @@ -337,7 +336,7 @@ export class Stylesheet {
/** @type {import('#compiler').Style | null} */
ast;

/** @type {string} */
/** @type {string} Path of Svelte file the CSS is in */
filename;

/** @type {boolean} */
Expand Down Expand Up @@ -471,20 +470,23 @@ export class Stylesheet {
}

/**
* @param {string} file
* @param {string} source
* @param {boolean} dev
* @param {import('#compiler').ValidatedCompileOptions} options
*/
render(file, source, dev) {
render(source, options) {
// TODO neaten this up
if (!this.ast) throw new Error('Unexpected error');

const code = new MagicString(source);

// Generate source mappings for the style sheet nodes we have.
// Note that resolution is a bit more coarse than in Svelte 4 because
// our own CSS AST is not as detailed with regards to the node values.
walk(/** @type {import('#compiler').Css.Node} */ (this.ast), null, {
_: (node) => {
_: (node, { next }) => {
code.addSourcemapLocation(node.start);
code.addSourcemapLocation(node.end);
next();
}
});

Expand All @@ -495,19 +497,27 @@ export class Stylesheet {
code.remove(0, this.ast.content.start);

for (const child of this.children) {
child.prune(code, dev);
child.prune(code, options.dev);
}

code.remove(/** @type {number} */ (this.ast.content.end), source.length);

return {
const css = {
code: code.toString(),
map: code.generateMap({
// include source content; makes it easier/more robust looking up the source map code
includeContent: true,
// generateMap takes care of calculating source relative to file
source: this.filename,
file
file: options.cssOutputFilename || this.filename
})
};
merge_with_preprocessor_map(css, options, css.map.sources[0]);
if (options.dev && options.css === 'injected' && css.code) {
css.code += `\n/*# sourceMappingURL=${css.map.toUrl()} */`;
}

return css;
}

/** @param {import('../phases/types.js').ComponentAnalysis} analysis */
Expand Down
17 changes: 16 additions & 1 deletion packages/svelte/src/compiler/phases/1-parse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import full_char_code_at from './utils/full_char_code_at.js';
import { error } from '../../errors.js';
import { create_fragment } from './utils/create.js';
import read_options from './read/options.js';
import { getLocator } from 'locate-character';

const regex_position_indicator = / \(\d+:\d+\)$/;

Expand Down Expand Up @@ -41,13 +42,16 @@ export class Parser {
/** @type {LastAutoClosedTag | undefined} */
last_auto_closed_tag;

locate;

/** @param {string} template */
constructor(template) {
if (typeof template !== 'string') {
throw new TypeError('Template must be a string');
}

this.template = template.trimEnd();
this.locate = getLocator(this.template, { offsetLine: 1 });

let match_lang;

Expand Down Expand Up @@ -133,6 +137,18 @@ export class Parser {
}
}

/**
* offset -> line/column
* @param {number} start
* @param {number} end
*/
get_location(start, end) {
return {
start: /** @type {import('locate-character').Location_1} */ (this.locate(start)),
end: /** @type {import('locate-character').Location_1} */ (this.locate(end))
};
}

current() {
return this.stack[this.stack.length - 1];
}
Expand Down Expand Up @@ -297,7 +313,6 @@ export class Parser {
*/
export function parse(template) {
const parser = new Parser(template);

return parser.root;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export default function read_pattern(parser) {
type: 'Identifier',
name,
start,
loc: parser.get_location(start, parser.index),
end: parser.index,
typeAnnotation: annotation
};
Expand Down
Loading
Loading