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

Refactor Astro plugin and compile flow #5506

Merged
merged 14 commits into from Dec 5, 2022
Merged

Refactor Astro plugin and compile flow #5506

merged 14 commits into from Dec 5, 2022

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Dec 1, 2022

Changes

  • Refactor vite-plugin-astro to split of core logic out of index.ts so they can be tested.
  • Remove /@fs usage when compiling (uses resolve.dedupe to achieve this)

It's easier to review the changes by checking each commit individually.

Testing

All existing tests should pass.

Docs

N/A. refactors.

@changeset-bot
Copy link

changeset-bot bot commented Dec 1, 2022

🦋 Changeset detected

Latest commit: be75a66

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Dec 1, 2022
// For CSS / hoisted scripts, the main Astro module should already be cached
const filename = normalizeFilename(parsedId.filename, config);
const compileResult = getCachedCompileResult(config, filename);
if (!compileResult) {
return null;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I found this.resolve and normalizeFilename seems to guard the same thing conceptually, so I opt for normalizeFilename as it's done by other plugins too.

transformResult = await cachedCompilation(compileProps);
// Compile all TypeScript to JavaScript.
// Also, catches invalid JS/TS in the compiled output before returning.
esbuildResult = await transformWithEsbuild(transformResult.code, rawId, {
Copy link
Member Author

Choose a reason for hiding this comment

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

Swapping our use of esbuild.transform to transformWithEsbuild so we use Vite's esbuild instance, and get the benefits of respecting the tsconfig.

I'm planning to refactor this more for the JSX plugin to when I backport vitejs/vite#11120 to Vite 3.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh interesting, how was our esbuild not respecting it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Vite has this code that passes the resolved tsconfig for that file during transform, as esbuild doesn't handle that by default. It should make the compiled output a bit more accurate, but in most case not noticable.

// Also, catches invalid JS/TS in the compiled output before returning.
esbuildResult = await transformWithEsbuild(transformResult.code, rawId, {
loader: 'ts',
target: 'esnext',
Copy link
Member Author

Choose a reason for hiding this comment

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

Use esnext as we don't need down-levelling (same behaviour as esbuild.transform and how Vite works currently).

@bluwy bluwy marked this pull request as draft December 1, 2022 17:44
}
}

export async function cachedCompilation(props: CompileProps): Promise<CompileResult> {
Copy link
Contributor

Choose a reason for hiding this comment

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

I like that the caching part is split into a separate module now!

// For CSS / hoisted scripts, the main Astro module should already be cached
const filename = normalizeFilename(parsedId.filename, config);
const compileResult = getCachedCompileResult(config, filename);
if (!compileResult) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure this is right. At least looking at the old code we used to do await cachedCompilation(compileProps); for styles. Although thinking about it now, there shouldn't be a time where styles are loaded before we have compiled 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

I noticed that for styles too but we always guarded this part with a:

let source = getCachedSource(config, raw.id);
if (!source) {
	return null;
}

So technically we're expecting the main module to be compiled before we process the styles, otherwise we bail.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would verify that all of the tests pass with this.

@matthewp
Copy link
Contributor

matthewp commented Dec 1, 2022

I love how much smaller the module has gotten! Good refactor, not sure why the tests are failing though.

@bluwy bluwy marked this pull request as ready for review December 2, 2022 16:00
@bluwy
Copy link
Member Author

bluwy commented Dec 2, 2022

Windows has blessed me with a green check 😌 (though I had to rerun once but I'll take it)

Turns out I made some changes to normalizeFilename that accidentally preserved backslashes. (Should be normalized as forward slashes). And it caused errors in the compiled output as the compiler inlines pathname as-is without escaping backslashes.

@bluwy bluwy merged commit f536a34 into main Dec 5, 2022
@bluwy bluwy deleted the astro-plugin-refactor branch December 5, 2022 13:34
@astrobot-houston astrobot-houston mentioned this pull request Dec 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants