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

Webpack magic-comments doesn't work with svelte 3.17 #4268

Closed
j3rem1e opened this issue Jan 15, 2020 · 4 comments · Fixed by #4293
Closed

Webpack magic-comments doesn't work with svelte 3.17 #4268

j3rem1e opened this issue Jan 15, 2020 · 4 comments · Fixed by #4293
Labels

Comments

@j3rem1e
Copy link

j3rem1e commented Jan 15, 2020

Describe the bug

I was using in component the following code:

  def grid = import(/* webpackChunkName: "mychunk" */ 'myModule);

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:

  • Chrome 77
  • Windows 7
  • Svelte 3.17.1
  • Webpack
@Conduitry Conduitry added the bug label Jan 15, 2020
@Conduitry
Copy link
Member

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.

@Conduitry
Copy link
Member

Presumably this would involve writing an onComment handler for Acorn, similar to how code-red does, and use this to attach the non-standard leadingComments and trailingComments values on each node. I'm hoping code-red would be able to just pick them up and include them in the output. Perhaps code-red could expose a wrapper around Acorn that adds the appropriate comment properties to the AST.

@Conduitry
Copy link
Member

Frankensteining code-red into acorn.ts in Svelte seems to work:

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.

@Conduitry
Copy link
Member

Fixed in 3.17.2 - https://svelte.dev/repl/48ebe0eb58b841b68cac56ab75fec4fb?version=3.17.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants