astro 6.2.0 → 7.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/dist/assets/utils/assets.d.ts +4 -5
  2. package/dist/assets/utils/node.d.ts +2 -2
  3. package/dist/assets/vite-plugin-assets.js +1 -1
  4. package/dist/cli/infra/build-time-astro-version-provider.js +1 -1
  5. package/dist/content/content-layer.js +3 -3
  6. package/dist/content/runtime-assets.d.ts +2 -2
  7. package/dist/content/utils.d.ts +2 -3
  8. package/dist/core/build/add-rolldown-input.d.ts +2 -0
  9. package/dist/core/build/{add-rollup-input.js → add-rolldown-input.js} +3 -3
  10. package/dist/core/build/graph.d.ts +8 -8
  11. package/dist/core/build/plugins/plugin-analyzer.js +1 -1
  12. package/dist/core/build/plugins/plugin-component-entry.d.ts +1 -1
  13. package/dist/core/build/plugins/plugin-component-entry.js +3 -3
  14. package/dist/core/build/plugins/plugin-internals.js +1 -1
  15. package/dist/core/build/plugins/plugin-prerender.js +1 -1
  16. package/dist/core/build/static-build.d.ts +2 -2
  17. package/dist/core/build/static-build.js +31 -31
  18. package/dist/core/build/util.d.ts +2 -2
  19. package/dist/core/build/util.js +2 -2
  20. package/dist/core/compile/compile.d.ts +1 -1
  21. package/dist/core/compile/compile.js +20 -16
  22. package/dist/core/config/schemas/base.d.ts +0 -2
  23. package/dist/core/config/schemas/base.js +0 -2
  24. package/dist/core/config/schemas/relative.d.ts +0 -3
  25. package/dist/core/constants.js +1 -1
  26. package/dist/core/create-vite.js +2 -0
  27. package/dist/core/dev/dev.js +1 -1
  28. package/dist/core/errors/dev/utils.js +1 -1
  29. package/dist/core/head-propagation/hint.d.ts +4 -0
  30. package/dist/core/head-propagation/hint.js +7 -0
  31. package/dist/core/messages/runtime.js +1 -1
  32. package/dist/core/middleware/vite-plugin.js +2 -2
  33. package/dist/toolbar/vite-plugin-dev-toolbar.js +1 -23
  34. package/dist/types/public/config.d.ts +0 -1
  35. package/dist/types/public/content.d.ts +3 -3
  36. package/dist/types/public/integrations.d.ts +2 -2
  37. package/dist/vite-plugin-adapter-config/index.js +1 -1
  38. package/dist/vite-plugin-astro/compile.d.ts +3 -5
  39. package/dist/vite-plugin-astro/compile.js +4 -60
  40. package/dist/vite-plugin-astro/index.js +15 -28
  41. package/dist/vite-plugin-astro/types.d.ts +2 -1
  42. package/dist/vite-plugin-head/index.js +9 -3
  43. package/dist/vite-plugin-pages/util.d.ts +1 -1
  44. package/package.json +7 -9
  45. package/templates/content/module.mjs +1 -1
  46. package/dist/core/build/add-rollup-input.d.ts +0 -2
  47. package/dist/core/compile/compile-rs.d.ts +0 -25
  48. package/dist/core/compile/compile-rs.js +0 -111
  49. package/dist/core/head-propagation/comment.d.ts +0 -7
  50. package/dist/core/head-propagation/comment.js +0 -7
  51. package/dist/vite-plugin-astro/compile-rs.d.ts +0 -12
  52. package/dist/vite-plugin-astro/compile-rs.js +0 -39
@@ -3,7 +3,7 @@ import {
3
3
  normalizePath as viteNormalizePath
4
4
  } from "vite";
5
5
  import { getServerOutputDirectory } from "../../prerender/utils.js";
6
- import { addRollupInput } from "../build/add-rollup-input.js";
6
+ import { addRolldownInput } from "../build/add-rolldown-input.js";
7
7
  import { ASTRO_VITE_ENVIRONMENT_NAMES, MIDDLEWARE_PATH_SEGMENT_NAME } from "../constants.js";
8
8
  import { MissingMiddlewareForInternationalization } from "../errors/errors-data.js";
9
9
  import { AstroError } from "../errors/index.js";
@@ -119,7 +119,7 @@ function vitePluginMiddlewareBuild(opts, internals) {
119
119
  },
120
120
  options(options) {
121
121
  if (canSplitMiddleware) {
122
- return addRollupInput(options, [MIDDLEWARE_MODULE_ID]);
122
+ return addRolldownInput(options, [MIDDLEWARE_MODULE_ID]);
123
123
  } else {
124
124
  }
125
125
  },
@@ -1,4 +1,3 @@
1
- import { readFileSync, writeFileSync } from "node:fs";
2
1
  import { telemetry } from "../events/index.js";
3
2
  import { eventAppToggled } from "../events/toolbar.js";
4
3
  const VIRTUAL_MODULE_ID = "astro:toolbar:internal";
@@ -15,28 +14,7 @@ function astroDevToolbar({ settings, logger }) {
15
14
  "astro > aria-query",
16
15
  "astro > axobject-query",
17
16
  ...settings.devToolbarApps.length > 0 ? ["astro/toolbar"] : []
18
- ],
19
- esbuildOptions: {
20
- plugins: [
21
- {
22
- name: "astro:strip-toolbar-sourcemap",
23
- setup(build) {
24
- build.onEnd((result) => {
25
- if (!result.metafile) return;
26
- for (const outputPath of Object.keys(result.metafile.outputs)) {
27
- if (!outputPath.includes("entrypoint") || !outputPath.endsWith(".js"))
28
- continue;
29
- const code = readFileSync(outputPath, "utf-8");
30
- const stripped = code.replace(/\/\/# sourceMappingURL=.*$/m, "");
31
- if (stripped !== code) {
32
- writeFileSync(outputPath, stripped);
33
- }
34
- }
35
- });
36
- }
37
- }
38
- ]
39
- }
17
+ ]
40
18
  }
41
19
  };
42
20
  },
@@ -2811,7 +2811,6 @@ export interface AstroUserConfig<TLocales extends Locales = never, TDriver exten
2811
2811
  * ```
2812
2812
  */
2813
2813
  routeRules?: RouteRules;
2814
- rustCompiler?: boolean;
2815
2814
  /**
2816
2815
  * @name experimental.queuedRendering
2817
2816
  * @type {boolean | { poolSize?: number; cache?: boolean }}
@@ -1,9 +1,9 @@
1
1
  import type { MarkdownHeading } from '@astrojs/markdown-remark';
2
- import type * as rollup from 'rollup';
3
2
  import type { DataEntry, RenderedContent } from '../../content/data-store.js';
4
3
  import type { LiveCollectionError } from '../../content/loaders/errors.js';
5
4
  import type { AstroComponentFactory } from '../../runtime/server/index.js';
6
5
  import type { AstroConfig } from './config.js';
6
+ import type { Rolldown } from 'vite';
7
7
  export interface AstroInstance {
8
8
  file: string;
9
9
  url: string | undefined;
@@ -96,11 +96,11 @@ export interface ContentEntryType {
96
96
  fileUrl: URL;
97
97
  contents: string;
98
98
  }): GetContentEntryInfoReturnType | Promise<GetContentEntryInfoReturnType>;
99
- getRenderModule?(this: rollup.PluginContext, params: {
99
+ getRenderModule?(this: Rolldown.PluginContext, params: {
100
100
  contents: string;
101
101
  fileUrl: URL;
102
102
  viteId: string;
103
- }): rollup.LoadResult | Promise<rollup.LoadResult>;
103
+ }): Rolldown.LoadResult | Promise<Rolldown.LoadResult>;
104
104
  contentModuleTypes?: string;
105
105
  getRenderFunction?(config: AstroConfig): Promise<ContentEntryRenderFunction>;
106
106
  /**
@@ -112,7 +112,7 @@ interface AdapterExplicitProperties {
112
112
  * or `"explicit"` (default, but deprecated):
113
113
  *
114
114
  * - **`"auto"` (recommended):** You are responsible for providing a valid module as an entrypoint
115
- * using either `serverEntrypoint` or, if you need further customization at the Vite level using `vite.build.rollupOptions.input`.
115
+ * using either `serverEntrypoint` or, if you need further customization at the Vite level using `vite.build.rolldownOptions.input`.
116
116
  * - **`"explicit"` (deprecated)**: You must provide the exports required by the host in the server entrypoint
117
117
  * using a `createExports()` function before passing them to `setAdapter()` as an [`exports`](#exports) list. This supports
118
118
  * adapters built using the Astro 5 version of the Adapter API. By default, all adapters will receive this value to allow backwards
@@ -143,7 +143,7 @@ interface AdapterAutoProperties {
143
143
  * or `"explicit"` (default, but deprecated):
144
144
  *
145
145
  * - **`"auto"` (recommended):** You are responsible for providing a valid module as an entrypoint
146
- * using either `serverEntrypoint` or, if you need further customization at the Vite level using `vite.build.rollupOptions.input`.
146
+ * using either `serverEntrypoint` or, if you need further customization at the Vite level using `vite.build.rolldownOptions.input`.
147
147
  * - **`"explicit"` (deprecated)**: You must provide the exports required by the host in the server entrypoint
148
148
  * using a `createExports()` function before passing them to `setAdapter()` as an [`exports`](#exports) list. This supports
149
149
  * adapters built using the Astro 5 version of the Adapter API. By default, all adapters will receive this value to allow backwards
@@ -13,7 +13,7 @@ function vitePluginAdapterConfig(settings) {
13
13
  environments: {
14
14
  [ASTRO_VITE_ENVIRONMENT_NAMES.ssr]: {
15
15
  build: {
16
- rollupOptions: {
16
+ rolldownOptions: {
17
17
  input: {
18
18
  index: typeof adapter.serverEntrypoint === "string" ? adapter.serverEntrypoint : fileURLToPath(adapter.serverEntrypoint)
19
19
  }
@@ -1,14 +1,12 @@
1
+ import type { Rolldown } from 'vite';
1
2
  import { type CompileProps, type CompileResult } from '../core/compile/index.js';
2
- import type { AstroLogger } from '../core/logger/core.js';
3
3
  import type { CompileMetadata } from './types.js';
4
- import type { SourceMapInput } from 'rollup';
5
4
  interface CompileAstroOption {
6
5
  compileProps: CompileProps;
7
6
  astroFileToCompileMetadata: Map<string, CompileMetadata>;
8
- logger: AstroLogger;
9
7
  }
10
8
  export interface CompileAstroResult extends Omit<CompileResult, 'map'> {
11
- map: SourceMapInput;
9
+ map: Rolldown.SourceMapInput;
12
10
  }
13
- export declare function compileAstro({ compileProps, astroFileToCompileMetadata, logger, }: CompileAstroOption): Promise<CompileAstroResult>;
11
+ export declare function compileAstro({ compileProps, astroFileToCompileMetadata, }: CompileAstroOption): Promise<CompileAstroResult>;
14
12
  export {};
@@ -1,38 +1,10 @@
1
- import { transformWithEsbuild } from "vite";
2
1
  import { compile } from "../core/compile/index.js";
3
2
  import { getFileInfo } from "../vite-plugin-utils/index.js";
4
- import { frontmatterRE } from "./utils.js";
5
3
  async function compileAstro({
6
4
  compileProps,
7
- astroFileToCompileMetadata,
8
- logger
5
+ astroFileToCompileMetadata
9
6
  }) {
10
- let transformResult;
11
- let esbuildResult;
12
- try {
13
- transformResult = await compile(compileProps);
14
- esbuildResult = await transformWithEsbuild(transformResult.code, compileProps.filename, {
15
- ...compileProps.viteConfig.esbuild,
16
- loader: "ts",
17
- sourcemap: "external",
18
- tsconfigRaw: {
19
- compilerOptions: {
20
- // Ensure client:only imports are treeshaken
21
- verbatimModuleSyntax: false,
22
- importsNotUsedAsValues: "remove"
23
- }
24
- }
25
- });
26
- } catch (err) {
27
- await enhanceCompileError({
28
- err,
29
- id: compileProps.filename,
30
- source: compileProps.source,
31
- config: compileProps.astroConfig,
32
- logger
33
- });
34
- throw err;
35
- }
7
+ const transformResult = await compile(compileProps);
36
8
  const { fileId: file, fileUrl: url } = getFileInfo(
37
9
  compileProps.filename,
38
10
  compileProps.astroConfig
@@ -58,38 +30,10 @@ const $$url = ${JSON.stringify(
58
30
  });
59
31
  return {
60
32
  ...transformResult,
61
- code: esbuildResult.code + SUFFIX,
62
- map: esbuildResult.map
33
+ code: transformResult.code + SUFFIX,
34
+ map: transformResult.map || null
63
35
  };
64
36
  }
65
- async function enhanceCompileError({
66
- err,
67
- id,
68
- source
69
- }) {
70
- const lineText = err.loc?.lineText;
71
- const scannedFrontmatter = frontmatterRE.exec(source);
72
- if (scannedFrontmatter) {
73
- const frontmatter = scannedFrontmatter[1].replace(/\breturn\s*;/g, "throw 0;").replace(/\breturn\b/g, "throw ");
74
- if (lineText && !frontmatter.includes(lineText)) throw err;
75
- try {
76
- await transformWithEsbuild(frontmatter, id, {
77
- loader: "ts",
78
- target: "esnext",
79
- sourcemap: false
80
- });
81
- } catch (frontmatterErr) {
82
- if (frontmatterErr?.message) {
83
- frontmatterErr.message = frontmatterErr.message.replace(
84
- "end of file",
85
- "end of frontmatter"
86
- );
87
- }
88
- throw frontmatterErr;
89
- }
90
- }
91
- throw err;
92
- }
93
37
  export {
94
38
  compileAstro
95
39
  };
@@ -3,7 +3,6 @@ import { ASTRO_VITE_ENVIRONMENT_NAMES } from "../core/constants.js";
3
3
  import { isAstroServerEnvironment } from "../environments.js";
4
4
  import { normalizeFilename, specialQueriesRE } from "../vite-plugin-utils/index.js";
5
5
  import { compileAstro } from "./compile.js";
6
- import { compileAstro as compileAstroRs } from "./compile-rs.js";
7
6
  import { handleHotUpdate } from "./hmr.js";
8
7
  import { parseAstroRequest } from "./query.js";
9
8
  import { loadId } from "./utils.js";
@@ -61,23 +60,15 @@ function astro({ settings, logger }) {
61
60
  async configResolved(viteConfig) {
62
61
  const toolbarEnabled = await settings.preferences.get("devToolbar.enabled");
63
62
  compile = (code, filename) => {
64
- const compileProps = {
65
- astroConfig: config,
66
- viteConfig,
67
- toolbarEnabled,
68
- filename,
69
- source: code
70
- };
71
- if (config.experimental.rustCompiler) {
72
- return compileAstroRs({
73
- compileProps,
74
- astroFileToCompileMetadata
75
- });
76
- }
77
63
  return compileAstro({
78
- compileProps,
79
- astroFileToCompileMetadata,
80
- logger
64
+ compileProps: {
65
+ astroConfig: config,
66
+ viteConfig,
67
+ toolbarEnabled,
68
+ filename,
69
+ source: code
70
+ },
71
+ astroFileToCompileMetadata
81
72
  });
82
73
  };
83
74
  },
@@ -143,7 +134,8 @@ function astro({ settings, logger }) {
143
134
  }
144
135
  if (isAstroServerEnvironment(this.environment)) {
145
136
  return {
146
- code: `/* client script, empty in SSR: ${id} */`
137
+ code: `/* client script, empty in SSR: ${id} */`,
138
+ moduleType: "ts"
147
139
  };
148
140
  }
149
141
  const script = compileMetadata.scripts[query.index];
@@ -165,6 +157,7 @@ File: ${id}`
165
157
  }
166
158
  const result = {
167
159
  code: "",
160
+ moduleType: "ts",
168
161
  meta: {
169
162
  vite: {
170
163
  lang: "ts"
@@ -173,13 +166,11 @@ File: ${id}`
173
166
  };
174
167
  switch (script.type) {
175
168
  case "inline": {
176
- const { code, map } = script;
177
- result.code = appendSourceMap(code, map);
169
+ result.code = script.code ?? "";
178
170
  break;
179
171
  }
180
172
  case "external": {
181
- const { src } = script;
182
- result.code = `import "${src}"`;
173
+ result.code = `import "${script.src}"`;
183
174
  break;
184
175
  }
185
176
  }
@@ -216,6 +207,7 @@ File: ${id}`
216
207
  );
217
208
  }
218
209
  : {};`,
210
+ moduleType: "ts",
219
211
  meta: { vite: { lang: "ts" } }
220
212
  };
221
213
  }
@@ -234,6 +226,7 @@ File: ${id}`
234
226
  return {
235
227
  code: transformResult.code,
236
228
  map: transformResult.map,
229
+ moduleType: "ts",
237
230
  meta: {
238
231
  astro: astroMetadata,
239
232
  vite: {
@@ -263,12 +256,6 @@ File: ${id}`
263
256
  }
264
257
  ];
265
258
  }
266
- function appendSourceMap(content, map) {
267
- if (!map) return content;
268
- return `${content}${"\n//#"} sourceMappingURL=data:application/json;charset=utf-8;base64,${Buffer.from(
269
- map
270
- ).toString("base64")}`;
271
- }
272
259
  export {
273
260
  astro as default,
274
261
  getAstroMetadata
@@ -1,9 +1,10 @@
1
- import type { HoistedScript, TransformResult } from '@astrojs/compiler';
1
+ import type { HoistedScript, TransformResult } from '@astrojs/compiler-rs';
2
2
  import type { CompileCssResult } from '../core/compile/types.js';
3
3
  import type { PropagationHint } from '../types/public/internal.js';
4
4
  interface PageOptions {
5
5
  prerender?: boolean;
6
6
  }
7
+ export type AstroComponent = TransformResult['hydratedComponents'][number];
7
8
  export interface PluginMetadata {
8
9
  astro: {
9
10
  hydratedComponents: TransformResult['hydratedComponents'];
@@ -1,4 +1,4 @@
1
- import { hasHeadInjectComment } from "../core/head-propagation/comment.js";
1
+ import { hasHeadPropagationCall } from "../core/head-propagation/hint.js";
2
2
  import {
3
3
  buildImporterGraphFromModuleInfo,
4
4
  computeInTreeAncestors
@@ -115,7 +115,7 @@ function configHeadVitePlugin() {
115
115
  if (info && getAstroMetadata(info)?.containsHead) {
116
116
  propagateMetadata.call(this, id, "containsHead", true);
117
117
  }
118
- if (hasHeadInjectComment(source)) {
118
+ if (hasHeadPropagationCall(source)) {
119
119
  propagateMetadata.call(this, id, "propagation", "in-tree");
120
120
  }
121
121
  invalidateComponentMetadataModule();
@@ -123,11 +123,17 @@ function configHeadVitePlugin() {
123
123
  };
124
124
  }
125
125
  function astroHeadBuildPlugin(internals) {
126
+ const headPropagationModuleIds = /* @__PURE__ */ new Set();
126
127
  return {
127
128
  name: "astro:head-metadata-build",
128
129
  applyToEnvironment(environment) {
129
130
  return environment.name === ASTRO_VITE_ENVIRONMENT_NAMES.ssr || environment.name === ASTRO_VITE_ENVIRONMENT_NAMES.prerender;
130
131
  },
132
+ transform(source, id) {
133
+ if (hasHeadPropagationCall(source)) {
134
+ headPropagationModuleIds.add(id);
135
+ }
136
+ },
131
137
  generateBundle(_opts, bundle) {
132
138
  const map = internals.componentMetadata;
133
139
  const moduleIds = /* @__PURE__ */ new Set();
@@ -159,7 +165,7 @@ function astroHeadBuildPlugin(internals) {
159
165
  selfPropagationSeeds.add(id);
160
166
  }
161
167
  }
162
- if (mod.code && hasHeadInjectComment(mod.code)) {
168
+ if (mod.code && hasHeadPropagationCall(mod.code) || headPropagationModuleIds.has(id)) {
163
169
  commentPropagationSeeds.add(id);
164
170
  }
165
171
  }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Prevents Rollup from triggering other plugins in the process by masking the extension (hence the virtual file).
2
+ * Prevents Rolldown from triggering other plugins in the process by masking the extension (hence the virtual file).
3
3
  * Inverse function of getComponentFromVirtualModulePageName() below.
4
4
  * @param virtualModulePrefix The prefix used to create the virtual module
5
5
  * @param path Page component path
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "astro",
3
- "version": "6.2.0",
3
+ "version": "7.0.0-alpha.0",
4
4
  "description": "Astro is a modern site builder with web best practices, performance, and DX front-of-mind.",
5
5
  "type": "module",
6
6
  "author": "withastro",
@@ -106,7 +106,7 @@
106
106
  "README.md"
107
107
  ],
108
108
  "dependencies": {
109
- "@astrojs/compiler": "^4.0.0",
109
+ "@astrojs/compiler-rs": "^0.1.10",
110
110
  "@capsizecss/unpack": "^4.0.0",
111
111
  "@clack/prompts": "^1.1.0",
112
112
  "@oslojs/encoding": "^1.1.0",
@@ -152,20 +152,19 @@
152
152
  "unist-util-visit": "^5.1.0",
153
153
  "unstorage": "^1.17.5",
154
154
  "vfile": "^6.0.3",
155
- "vite": "^7.3.2",
155
+ "vite": "^8.0.8",
156
156
  "vitefu": "^1.1.2",
157
157
  "xxhash-wasm": "^1.1.0",
158
158
  "yargs-parser": "^22.0.0",
159
159
  "zod": "^4.3.6",
160
160
  "@astrojs/internal-helpers": "0.9.0",
161
- "@astrojs/telemetry": "3.3.1",
162
- "@astrojs/markdown-remark": "7.1.1"
161
+ "@astrojs/markdown-remark": "7.1.1",
162
+ "@astrojs/telemetry": "3.3.1"
163
163
  },
164
164
  "optionalDependencies": {
165
165
  "sharp": "^0.34.0"
166
166
  },
167
167
  "devDependencies": {
168
- "@astrojs/compiler-rs": "^0.1.6",
169
168
  "@playwright/test": "1.58.2",
170
169
  "@types/aria-query": "^5.0.4",
171
170
  "@types/hast": "^3.0.4",
@@ -188,14 +187,13 @@
188
187
  "rehype-slug": "^6.0.0",
189
188
  "rehype-toc": "^3.0.2",
190
189
  "remark-code-titles": "^0.1.2",
191
- "rollup": "^4.58.0",
192
190
  "sass": "^1.98.0",
193
191
  "typescript": "^5.9.3",
194
192
  "undici": "^7.22.0",
195
193
  "unified": "^11.0.5",
196
194
  "vitest": "^4.1.0",
197
- "astro-scripts": "0.0.14",
198
- "@astrojs/check": "0.9.9"
195
+ "@astrojs/check": "0.9.9",
196
+ "astro-scripts": "0.0.14"
199
197
  },
200
198
  "engines": {
201
199
  "node": ">=22.12.0",
@@ -1,4 +1,4 @@
1
- // astro-head-inject
1
+ 'use astro:head-inject';
2
2
  import {
3
3
  createDeprecatedFunction,
4
4
  createGetCollection,
@@ -1,2 +0,0 @@
1
- import type { Rollup } from 'vite';
2
- export declare function addRollupInput(inputOptions: Rollup.InputOptions, newInputs: string[]): Rollup.InputOptions;
@@ -1,25 +0,0 @@
1
- import type { ResolvedConfig } from 'vite';
2
- import type { AstroConfig } from '../../types/public/config.js';
3
- import type { CompileCssResult } from './types.js';
4
- export interface CompileProps {
5
- astroConfig: AstroConfig;
6
- viteConfig: ResolvedConfig;
7
- toolbarEnabled: boolean;
8
- filename: string;
9
- source: string;
10
- }
11
- export interface CompileResult {
12
- code: string;
13
- map: string;
14
- scope: string;
15
- css: CompileCssResult[];
16
- scripts: any[];
17
- hydratedComponents: any[];
18
- clientOnlyComponents: any[];
19
- serverComponents: any[];
20
- containsHead: boolean;
21
- propagation: boolean;
22
- styleError: string[];
23
- diagnostics: any[];
24
- }
25
- export declare function compile({ astroConfig, viteConfig, toolbarEnabled, filename, source, }: CompileProps): Promise<CompileResult>;
@@ -1,111 +0,0 @@
1
- import { fileURLToPath } from "node:url";
2
- import { AggregateError, CompilerError } from "../errors/errors.js";
3
- import { AstroErrorData } from "../errors/index.js";
4
- import { normalizePath, resolvePath } from "../viteUtils.js";
5
- import { createStylePreprocessor } from "./style.js";
6
- async function compile({
7
- astroConfig,
8
- viteConfig,
9
- toolbarEnabled,
10
- filename,
11
- source
12
- }) {
13
- let preprocessStyles;
14
- let transform;
15
- try {
16
- ({ preprocessStyles, transform } = await import("@astrojs/compiler-rs"));
17
- } catch (err) {
18
- throw new Error(
19
- `Failed to load @astrojs/compiler-rs. Make sure it is installed and up to date. Original error: ${err}`
20
- );
21
- }
22
- const cssPartialCompileResults = [];
23
- const cssTransformErrors = [];
24
- let transformResult;
25
- try {
26
- const preprocessedStyles = await preprocessStyles(
27
- source,
28
- createStylePreprocessor({
29
- filename,
30
- viteConfig,
31
- astroConfig,
32
- cssPartialCompileResults,
33
- cssTransformErrors
34
- })
35
- );
36
- transformResult = transform(source, {
37
- compact: astroConfig.compressHTML,
38
- filename,
39
- normalizedFilename: normalizeFilename(filename, astroConfig.root),
40
- sourcemap: "both",
41
- internalURL: "astro/compiler-runtime",
42
- // TODO: remove in Astro v7
43
- astroGlobalArgs: JSON.stringify(astroConfig.site),
44
- scopedStyleStrategy: astroConfig.scopedStyleStrategy,
45
- resultScopedSlot: true,
46
- transitionsAnimationURL: "astro/components/viewtransitions.css",
47
- annotateSourceFile: viteConfig.command === "serve" && astroConfig.devToolbar && astroConfig.devToolbar.enabled && toolbarEnabled,
48
- preprocessedStyles,
49
- resolvePath(specifier) {
50
- return resolvePath(specifier, filename);
51
- }
52
- });
53
- } catch (err) {
54
- throw new CompilerError({
55
- ...AstroErrorData.UnknownCompilerError,
56
- message: err.message ?? "Unknown compiler error",
57
- stack: err.stack,
58
- location: {
59
- file: filename
60
- }
61
- });
62
- }
63
- handleCompileResultErrors(filename, transformResult, cssTransformErrors);
64
- return {
65
- ...transformResult,
66
- css: transformResult.css.map((code, i) => ({
67
- ...cssPartialCompileResults[i],
68
- code
69
- }))
70
- };
71
- }
72
- function handleCompileResultErrors(filename, result, cssTransformErrors) {
73
- const compilerError = result.diagnostics.find((diag) => diag.severity === "error");
74
- if (compilerError) {
75
- throw new CompilerError({
76
- name: "CompilerError",
77
- message: compilerError.text,
78
- location: {
79
- line: compilerError.labels[0].line,
80
- column: compilerError.labels[0].column,
81
- file: filename
82
- },
83
- hint: compilerError.hint
84
- });
85
- }
86
- switch (cssTransformErrors.length) {
87
- case 0:
88
- break;
89
- case 1: {
90
- throw cssTransformErrors[0];
91
- }
92
- default: {
93
- throw new AggregateError({
94
- ...cssTransformErrors[0],
95
- errors: cssTransformErrors
96
- });
97
- }
98
- }
99
- }
100
- function normalizeFilename(filename, root) {
101
- const normalizedFilename = normalizePath(filename);
102
- const normalizedRoot = normalizePath(fileURLToPath(root));
103
- if (normalizedFilename.startsWith(normalizedRoot)) {
104
- return normalizedFilename.slice(normalizedRoot.length - 1);
105
- } else {
106
- return normalizedFilename;
107
- }
108
- }
109
- export {
110
- compile
111
- };
@@ -1,7 +0,0 @@
1
- /**
2
- * Returns true when source contains the `astro-head-inject` marker comment.
3
- *
4
- * @example
5
- * `//! astro-head-inject` in a helper module marks parent importers as `in-tree`.
6
- */
7
- export declare function hasHeadInjectComment(source: string): boolean;
@@ -1,7 +0,0 @@
1
- const HEAD_INJECT_COMMENT_EXP = /(?:^\/\/|\/\/!)\s*astro-head-inject/;
2
- function hasHeadInjectComment(source) {
3
- return HEAD_INJECT_COMMENT_EXP.test(source);
4
- }
5
- export {
6
- hasHeadInjectComment
7
- };
@@ -1,12 +0,0 @@
1
- import type { SourceMapInput } from 'rollup';
2
- import { type CompileProps, type CompileResult } from '../core/compile/compile-rs.js';
3
- import type { CompileMetadata } from './types.js';
4
- interface CompileAstroOption {
5
- compileProps: CompileProps;
6
- astroFileToCompileMetadata: Map<string, CompileMetadata>;
7
- }
8
- export interface CompileAstroResult extends Omit<CompileResult, 'map'> {
9
- map: SourceMapInput;
10
- }
11
- export declare function compileAstro({ compileProps, astroFileToCompileMetadata, }: CompileAstroOption): Promise<CompileAstroResult>;
12
- export {};
@@ -1,39 +0,0 @@
1
- import { compile } from "../core/compile/compile-rs.js";
2
- import { getFileInfo } from "../vite-plugin-utils/index.js";
3
- async function compileAstro({
4
- compileProps,
5
- astroFileToCompileMetadata
6
- }) {
7
- const transformResult = await compile(compileProps);
8
- const { fileId: file, fileUrl: url } = getFileInfo(
9
- compileProps.filename,
10
- compileProps.astroConfig
11
- );
12
- let SUFFIX = "";
13
- SUFFIX += `
14
- const $$file = ${JSON.stringify(file)};
15
- const $$url = ${JSON.stringify(
16
- url
17
- )};export { $$file as file, $$url as url };
18
- `;
19
- if (!compileProps.viteConfig.isProduction) {
20
- let i = 0;
21
- while (i < transformResult.scripts.length) {
22
- SUFFIX += `import "${compileProps.filename}?astro&type=script&index=${i}&lang.ts";`;
23
- i++;
24
- }
25
- }
26
- astroFileToCompileMetadata.set(compileProps.filename, {
27
- originalCode: compileProps.source,
28
- css: transformResult.css,
29
- scripts: transformResult.scripts
30
- });
31
- return {
32
- ...transformResult,
33
- code: transformResult.code + SUFFIX,
34
- map: transformResult.map || null
35
- };
36
- }
37
- export {
38
- compileAstro
39
- };