-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
feat: add new experimental Rust compiler #15543
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
b5e4ab3
feat: use the rust compiler
Princesseuh 9425ba7
fix: adjust for new compile preprocessStyle API
Princesseuh 8b1e286
fix: update for diagnostic shape
Princesseuh 1edf103
feat: experimental flag
Princesseuh f2bcaa4
Merge branch 'main' into feat/rust-compiler
Princesseuh 4af051e
fix: unrelated changes
Princesseuh f2c1665
chore: lockfile
Princesseuh c683fe7
fix: make tests pass on both compilers
Princesseuh 4b3b062
chore: docs
Princesseuh 4132ca9
Merge branch 'main' into feat/rust-compiler
Princesseuh 4df3926
chore: clean up
Princesseuh 081412a
Apply suggestions from code review
Princesseuh beece3a
fix: add tests and bump
Princesseuh 7292d35
Merge branch 'main' into feat/rust-compiler
Princesseuh a39997f
Update .changeset/rust-compiler-experimental.md
Princesseuh fc2b0bf
Apply suggestions from code review
Princesseuh 1255d5f
Merge branch 'main' into feat/rust-compiler
Princesseuh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| --- | ||
| 'astro': minor | ||
| --- | ||
|
|
||
| Adds a new `experimental.rustCompiler` flag to opt into the experimental Rust-based Astro compiler | ||
|
|
||
| This experimental compiler is faster, provides better error messages, and generally has better support for modern JavaScript, TypeScript, and CSS features. | ||
|
|
||
| After enabling in your Astro config, the `@astrojs/compiler-rs` package must also be installed into your project separately: | ||
|
|
||
| ```js | ||
| import { defineConfig } from "astro/config"; | ||
|
|
||
| export default defineConfig({ | ||
| experimental: { | ||
| rustCompiler: true | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
| This new compiler is still in early development and may exhibit some differences compared to the existing Go-based compiler. Notably, this compiler is generally more strict in regard to invalid HTML syntax and may throw errors in cases where the Go-based compiler would have been more lenient. For example, unclosed tags (e.g. `<p>My paragraph`) will now result in errors. | ||
|
|
||
| For more information about using this experimental feature in your project, especially regarding expected differences and limitations, please see the [experimental Rust compiler reference docs](https://v6.docs.astro.build/en/reference/experimental-flags/rust-compiler/). To give feedback on the compiler, or to keep up with its development, see the [RFC for a new compiler for Astro](https://github.com/withastro/roadmap/discussions/1306) for more information and discussion. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import { fileURLToPath } from 'node:url'; | ||
| import type { ResolvedConfig } from 'vite'; | ||
| import type { AstroConfig } from '../../types/public/config.js'; | ||
| import type { AstroError } from '../errors/errors.js'; | ||
| import { AggregateError, CompilerError } from '../errors/errors.js'; | ||
| import { AstroErrorData } from '../errors/index.js'; | ||
| import { normalizePath, resolvePath } from '../viteUtils.js'; | ||
| import { createStylePreprocessor, type PartialCompileCssResult } from './style.js'; | ||
| import type { CompileCssResult } from './types.js'; | ||
|
|
||
| export interface CompileProps { | ||
| astroConfig: AstroConfig; | ||
| viteConfig: ResolvedConfig; | ||
| toolbarEnabled: boolean; | ||
| filename: string; | ||
| source: string; | ||
| } | ||
|
|
||
| export interface CompileResult { | ||
| code: string; | ||
| map: string; | ||
| scope: string; | ||
| css: CompileCssResult[]; | ||
| scripts: any[]; | ||
| hydratedComponents: any[]; | ||
| clientOnlyComponents: any[]; | ||
| serverComponents: any[]; | ||
| containsHead: boolean; | ||
| propagation: boolean; | ||
| styleError: string[]; | ||
| diagnostics: any[]; | ||
| } | ||
|
|
||
| export async function compile({ | ||
| astroConfig, | ||
| viteConfig, | ||
| toolbarEnabled, | ||
| filename, | ||
| source, | ||
| }: CompileProps): Promise<CompileResult> { | ||
| let preprocessStyles; | ||
| let transform; | ||
| try { | ||
| ({ preprocessStyles, transform } = await import('@astrojs/compiler-rs')); | ||
| } | ||
| catch (err: unknown) { | ||
| throw new Error(`Failed to load @astrojs/compiler-rs. Make sure it is installed and up to date. Original error: ${err}`); | ||
| } | ||
|
|
||
| const cssPartialCompileResults: PartialCompileCssResult[] = []; | ||
| const cssTransformErrors: AstroError[] = []; | ||
| let transformResult: any; | ||
|
|
||
| try { | ||
| const preprocessedStyles = await preprocessStyles( | ||
| source, | ||
| createStylePreprocessor({ | ||
| filename, | ||
| viteConfig, | ||
| astroConfig, | ||
| cssPartialCompileResults, | ||
| cssTransformErrors, | ||
| }), | ||
| ); | ||
|
|
||
| transformResult = transform(source, { | ||
| compact: astroConfig.compressHTML, | ||
| filename, | ||
| normalizedFilename: normalizeFilename(filename, astroConfig.root), | ||
| sourcemap: 'both', | ||
| internalURL: 'astro/compiler-runtime', | ||
| // TODO: remove in Astro v7 | ||
| astroGlobalArgs: JSON.stringify(astroConfig.site), | ||
| scopedStyleStrategy: astroConfig.scopedStyleStrategy, | ||
| resultScopedSlot: true, | ||
| transitionsAnimationURL: 'astro/components/viewtransitions.css', | ||
| annotateSourceFile: | ||
| viteConfig.command === 'serve' && | ||
| astroConfig.devToolbar && | ||
| astroConfig.devToolbar.enabled && | ||
| toolbarEnabled, | ||
| preprocessedStyles, | ||
| resolvePath(specifier) { | ||
| return resolvePath(specifier, filename); | ||
| }, | ||
| }); | ||
| } catch (err: any) { | ||
| // The compiler should be able to handle errors by itself, however | ||
| // for the rare cases where it can't let's directly throw here with as much info as possible | ||
| throw new CompilerError({ | ||
| ...AstroErrorData.UnknownCompilerError, | ||
| message: err.message ?? 'Unknown compiler error', | ||
| stack: err.stack, | ||
| location: { | ||
| file: filename, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| handleCompileResultErrors(filename, transformResult, cssTransformErrors); | ||
|
|
||
| return { | ||
| ...transformResult, | ||
| css: transformResult.css.map((code: string, i: number) => ({ | ||
| ...cssPartialCompileResults[i], | ||
| code, | ||
| })), | ||
| }; | ||
| } | ||
|
|
||
| function handleCompileResultErrors( | ||
| filename: string, | ||
| result: any, | ||
| cssTransformErrors: AstroError[], | ||
| ) { | ||
| const compilerError = result.diagnostics.find((diag: any) => diag.severity === 'error'); | ||
|
|
||
| if (compilerError) { | ||
| throw new CompilerError({ | ||
| name: 'CompilerError', | ||
| message: compilerError.text, | ||
| location: { | ||
| line: compilerError.labels[0].line, | ||
| column: compilerError.labels[0].column, | ||
| file: filename, | ||
| }, | ||
| hint: compilerError.hint, | ||
| }); | ||
| } | ||
|
|
||
| switch (cssTransformErrors.length) { | ||
| case 0: | ||
| break; | ||
| case 1: { | ||
| throw cssTransformErrors[0]; | ||
| } | ||
| default: { | ||
| throw new AggregateError({ | ||
| ...cssTransformErrors[0], | ||
| errors: cssTransformErrors, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function normalizeFilename(filename: string, root: URL) { | ||
| const normalizedFilename = normalizePath(filename); | ||
| const normalizedRoot = normalizePath(fileURLToPath(root)); | ||
| if (normalizedFilename.startsWith(normalizedRoot)) { | ||
| return normalizedFilename.slice(normalizedRoot.length - 1); | ||
| } else { | ||
| return normalizedFilename; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import type { SourceMapInput } from 'rollup'; | ||
| import { type CompileProps, type CompileResult, compile } from '../core/compile/compile-rs.js'; | ||
| import { getFileInfo } from '../vite-plugin-utils/index.js'; | ||
| import type { CompileMetadata } from './types.js'; | ||
|
|
||
| interface CompileAstroOption { | ||
| compileProps: CompileProps; | ||
| astroFileToCompileMetadata: Map<string, CompileMetadata>; | ||
| } | ||
|
|
||
| export interface CompileAstroResult extends Omit<CompileResult, 'map'> { | ||
| map: SourceMapInput; | ||
| } | ||
|
|
||
| export async function compileAstro({ | ||
| compileProps, | ||
| astroFileToCompileMetadata, | ||
| }: CompileAstroOption): Promise<CompileAstroResult> { | ||
| const transformResult = await compile(compileProps); | ||
|
|
||
| const { fileId: file, fileUrl: url } = getFileInfo( | ||
| compileProps.filename, | ||
| compileProps.astroConfig, | ||
| ); | ||
|
|
||
| let SUFFIX = ''; | ||
| SUFFIX += `\nconst $$file = ${JSON.stringify(file)};\nconst $$url = ${JSON.stringify( | ||
| url, | ||
| )};export { $$file as file, $$url as url };\n`; | ||
|
|
||
| // Add HMR handling in dev mode. | ||
| if (!compileProps.viteConfig.isProduction) { | ||
| let i = 0; | ||
| while (i < transformResult.scripts.length) { | ||
| SUFFIX += `import "${compileProps.filename}?astro&type=script&index=${i}&lang.ts";`; | ||
| i++; | ||
| } | ||
| } | ||
|
|
||
| // Attach compile metadata to map for use by virtual modules | ||
| astroFileToCompileMetadata.set(compileProps.filename, { | ||
| originalCode: compileProps.source, | ||
| css: transformResult.css, | ||
| scripts: transformResult.scripts, | ||
| }); | ||
|
|
||
| return { | ||
| ...transformResult, | ||
| code: transformResult.code + SUFFIX, | ||
| map: transformResult.map || null, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noting that it's expected that there's no
@docstag here for an experimental feature! Everything is documented on the experimental docs page!