Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(tailwind): update layout without altering formatting #74

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

Closed
wants to merge 16 commits into from

Conversation

benmccann
Copy link
Member

@benmccann benmccann commented Oct 9, 2024

This just uses the Svelte 4 slot for now. #69 was attempting to tackle that

Some other items we should do:

  • Use guessIndentString and guess single vs double quote
  • Put those values into the workspace so that we only have to do them once
  • Simplify the call signature for addEmpty. We don't necessarily want to pass in the Ast, MagicString, and workspace as separate items

Copy link

pkg-pr-new bot commented Oct 9, 2024

Open in Stackblitz

pnpm add https://pkg.pr.new/sveltejs/cli/@svelte-cli/ast-tooling@74
pnpm add https://pkg.pr.new/sveltejs/cli/sv@74
pnpm add https://pkg.pr.new/sveltejs/cli/@svelte-cli/core@74

commit: 4e92d59

Comment on lines +7 to +35
export function addEmpty(ast: AST.Root, contents: MagicString, importFrom: string): void {
const body = ast.instance?.content?.body || [];

addImportIfNecessary(ast, expectedImportDeclaration);
if (!body.length) {
contents.prepend(dedent`
<script>
import '${importFrom}';
</script>

`);
return;
}

// check if already imported
for (const statement of body) {
if (statement.type === 'ImportDeclaration' && statement.source.value === importFrom) {
return;
}
}

const first_statement = body[0];
if (!first_statement.range) {
throw new Error(`${JSON.stringify(first_statement)} is missing range information.}`);
}
const is_first_line_import = first_statement.type === 'ImportDeclaration';
contents.prependLeft(
first_statement.range[0],
`import '${importFrom}';` + (is_first_line_import ? '\n\t' : '\n\n\t')
);
Copy link
Member

@AdrianGonz97 AdrianGonz97 Oct 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one issue with this implementation is that it's not really focused on applying the import to JS modules. for instance, it's injecting the <script> tags when there's no body.
this would break if we were to run addEmpty to a JS file like:

name: () => 'foo.js',
content: ({ content }) => {
	const { ast, generateCode } = parseScript(content);
	const ms = new MagicString(content);
	imports.addEmpty(ast, ms, '../app.css');
	return ms.toString();
}

Comment on lines +117 to +121
content: ({ content }) => {
content ||= '<slot />';
const { ast, source } = svelteMagicAst(content);
imports.addEmpty(ast, source, '../app.css');
return source.toString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manuel3108
Copy link
Member

I might be missing something, but now that #75 is in, do we still need this?

@benmccann
Copy link
Member Author

I'll close it for now. This is a more fine-grained approach, but there's not time to pursue it before launch

@benmccann benmccann closed this Oct 11, 2024
@manuel3108 manuel3108 deleted the tailwind-magic-string branch October 11, 2024 04:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants