-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Webpack magic-comments doesn't work with svelte 3.17 #4268
Comments
Weird. code-red does preserve the comment when asked to parse and stringify that dynamic import in isolation, so presumably this is a bug in Svelte itself. |
Presumably this would involve writing an |
Frankensteining code-red into import * as acorn from 'acorn';
import { walk } from 'estree-walker';
const Parser = acorn.Parser;
export const parse = (source: string) => {
const comments = [];
const ast = Parser.parse(source, {
sourceType: 'module',
ecmaVersion: 11,
locations: true,
onComment: (block: boolean, value: string, start: number, end: number) => {
if (block && /\n/.test(value)) {
let a = start;
while (a > 0 && source[a - 1] !== '\n') a -= 1;
let b = a;
while (/[ \t]/.test(source[b])) b += 1;
const indentation = source.slice(a, b);
value = value.replace(new RegExp(`^${indentation}`, 'gm'), '');
}
comments.push({ type: block ? 'Block' : 'Line', value, start, end });
}
});
// @ts-ignore
walk(ast, {
enter(node) {
let comment;
while (comments[0] && comments[0].start < (node as any).start) {
comment = comments.shift();
const next = comments[0] || node;
(comment as any).has_trailing_newline = (
comment.type === 'Line' ||
/\n/.test(source.slice(comment.end, (next as any).start))
);
(node.leadingComments || (node.leadingComments = [])).push(comment);
}
}
});
return ast;
};
export const parse_expression_at = (source: string, index: number) => Parser.parseExpressionAt(source, index, {
ecmaVersion: 11,
locations: true
}); This preserves the comment in the situation in this issue, and the only test that fails is a sample output js one involving comments. This gives me hope for adding this as a feature in code-red, which would be a much nicer solution than duplicating this here. I don't think the mechanism that code-red uses to attach comments to the AST should be part of its public API anyway. |
Fixed in 3.17.2 - https://svelte.dev/repl/48ebe0eb58b841b68cac56ab75fec4fb?version=3.17.2 |
Describe the bug
I was using in component the following code:
this code lazy-load a javascript module, and tells webpack to use a specific name for the generated chunk.
It worked in svelte 3.12, however Svelte 3.17 removes the comment when generating javascript.
To Reproduce
https://svelte.dev/repl/48ebe0eb58b841b68cac56ab75fec4fb?version=3.17.1
Expected behavior
The commentaries are not removed by svelte.
Information about your Svelte project:
The text was updated successfully, but these errors were encountered: