astro 2.0.6 → 2.0.7

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 (96) hide show
  1. package/dist/cli/sync/index.js +19 -3
  2. package/dist/content/index.d.ts +3 -2
  3. package/dist/content/index.js +5 -7
  4. package/dist/content/internal.js +8 -1
  5. package/dist/content/server-listeners.d.ts +13 -0
  6. package/dist/content/server-listeners.js +65 -0
  7. package/dist/content/types-generator.d.ts +5 -3
  8. package/dist/content/types-generator.js +16 -29
  9. package/dist/content/utils.d.ts +12 -1
  10. package/dist/content/utils.js +36 -17
  11. package/dist/content/vite-plugin-content-assets.d.ts +3 -3
  12. package/dist/content/vite-plugin-content-assets.js +81 -22
  13. package/dist/content/vite-plugin-content-imports.d.ts +8 -0
  14. package/dist/content/vite-plugin-content-imports.js +105 -0
  15. package/dist/core/app/common.js +2 -0
  16. package/dist/core/app/index.js +1 -0
  17. package/dist/core/app/types.d.ts +4 -2
  18. package/dist/core/build/generate.js +1 -0
  19. package/dist/core/build/index.js +5 -0
  20. package/dist/core/build/internal.d.ts +2 -0
  21. package/dist/core/build/internal.js +2 -1
  22. package/dist/core/build/page-data.js +4 -2
  23. package/dist/core/build/plugin.d.ts +41 -0
  24. package/dist/core/build/plugin.js +82 -0
  25. package/dist/core/build/plugins/index.d.ts +2 -0
  26. package/dist/core/build/plugins/index.js +25 -0
  27. package/dist/core/build/{vite-plugin-alias-resolve.d.ts → plugins/plugin-alias-resolve.d.ts} +3 -1
  28. package/dist/core/build/{vite-plugin-alias-resolve.js → plugins/plugin-alias-resolve.js} +13 -0
  29. package/dist/core/build/plugins/plugin-analyzer.d.ts +5 -0
  30. package/dist/core/build/{vite-plugin-analyzer.js → plugins/plugin-analyzer.js} +63 -13
  31. package/dist/core/build/{vite-plugin-css.d.ts → plugins/plugin-css.d.ts} +4 -2
  32. package/dist/core/build/{vite-plugin-css.js → plugins/plugin-css.js} +37 -27
  33. package/dist/core/build/plugins/plugin-hoisted-scripts.d.ts +7 -0
  34. package/dist/core/build/{vite-plugin-hoisted-scripts.js → plugins/plugin-hoisted-scripts.js} +15 -2
  35. package/dist/core/build/plugins/plugin-internals.d.ts +5 -0
  36. package/dist/core/build/{vite-plugin-internals.js → plugins/plugin-internals.js} +13 -0
  37. package/dist/core/build/plugins/plugin-pages.d.ts +6 -0
  38. package/dist/core/build/{vite-plugin-pages.js → plugins/plugin-pages.js} +16 -3
  39. package/dist/core/build/plugins/plugin-prerender.d.ts +6 -0
  40. package/dist/core/build/plugins/plugin-prerender.js +39 -0
  41. package/dist/core/build/plugins/plugin-ssr.d.ts +9 -0
  42. package/dist/core/build/{vite-plugin-ssr.js → plugins/plugin-ssr.js} +39 -15
  43. package/dist/core/build/plugins/util.d.ts +9 -0
  44. package/dist/core/build/plugins/util.js +29 -0
  45. package/dist/core/build/static-build.js +28 -40
  46. package/dist/core/build/types.d.ts +10 -2
  47. package/dist/core/compile/compile.js +1 -0
  48. package/dist/core/constants.js +1 -1
  49. package/dist/core/create-vite.js +7 -4
  50. package/dist/core/dev/dev.js +3 -1
  51. package/dist/core/errors/errors-data.d.ts +13 -0
  52. package/dist/core/errors/errors-data.js +6 -0
  53. package/dist/core/messages.js +2 -2
  54. package/dist/core/render/dev/css.js +2 -4
  55. package/dist/core/render/dev/util.d.ts +3 -0
  56. package/dist/core/render/dev/util.js +8 -0
  57. package/dist/core/render/dev/vite.js +3 -3
  58. package/dist/core/render/paginate.js +5 -4
  59. package/dist/core/render/result.js +13 -9
  60. package/dist/runtime/server/index.d.ts +2 -2
  61. package/dist/runtime/server/index.js +8 -0
  62. package/dist/runtime/server/jsx.js +4 -3
  63. package/dist/runtime/server/render/astro/factory.js +3 -3
  64. package/dist/runtime/server/render/astro/index.d.ts +1 -1
  65. package/dist/runtime/server/render/astro/instance.d.ts +2 -5
  66. package/dist/runtime/server/render/astro/instance.js +7 -4
  67. package/dist/runtime/server/render/common.js +20 -0
  68. package/dist/runtime/server/render/component.js +2 -1
  69. package/dist/runtime/server/render/head.d.ts +2 -1
  70. package/dist/runtime/server/render/head.js +2 -7
  71. package/dist/runtime/server/render/index.d.ts +3 -2
  72. package/dist/runtime/server/render/index.js +5 -0
  73. package/dist/runtime/server/render/page.js +3 -1
  74. package/dist/runtime/server/render/scope.d.ts +13 -0
  75. package/dist/runtime/server/render/scope.js +31 -0
  76. package/dist/runtime/server/render/slot.d.ts +6 -2
  77. package/dist/runtime/server/render/slot.js +7 -5
  78. package/dist/runtime/server/render/types.d.ts +6 -1
  79. package/dist/runtime/server/render/util.d.ts +0 -5
  80. package/dist/runtime/server/render/util.js +0 -6
  81. package/dist/vite-plugin-astro-server/route.js +1 -1
  82. package/dist/vite-plugin-head-propagation/index.d.ts +4 -0
  83. package/dist/vite-plugin-head-propagation/index.js +42 -0
  84. package/dist/vite-plugin-scanner/index.js +1 -0
  85. package/package.json +2 -2
  86. package/dist/content/vite-plugin-content-server.d.ts +0 -13
  87. package/dist/content/vite-plugin-content-server.js +0 -169
  88. package/dist/core/build/vite-plugin-analyzer.d.ts +0 -3
  89. package/dist/core/build/vite-plugin-hoisted-scripts.d.ts +0 -4
  90. package/dist/core/build/vite-plugin-internals.d.ts +0 -3
  91. package/dist/core/build/vite-plugin-pages.d.ts +0 -4
  92. package/dist/core/build/vite-plugin-prerender.d.ts +0 -4
  93. package/dist/core/build/vite-plugin-prerender.js +0 -34
  94. package/dist/core/build/vite-plugin-ssr.d.ts +0 -7
  95. package/dist/core/render/util.d.ts +0 -2
  96. package/dist/core/render/util.js +0 -18
@@ -0,0 +1,105 @@
1
+ import * as devalue from "devalue";
2
+ import { pathToFileURL } from "url";
3
+ import { AstroErrorData } from "../core/errors/errors-data.js";
4
+ import { AstroError } from "../core/errors/errors.js";
5
+ import { escapeViteEnvReferences, getFileInfo } from "../vite-plugin-utils/index.js";
6
+ import { contentFileExts, CONTENT_FLAG } from "./consts.js";
7
+ import {
8
+ getContentPaths,
9
+ getEntryData,
10
+ getEntryInfo,
11
+ getEntrySlug,
12
+ getEntryType,
13
+ globalContentConfigObserver,
14
+ parseFrontmatter
15
+ } from "./utils.js";
16
+ function isContentFlagImport(viteId) {
17
+ const { pathname, searchParams } = new URL(viteId, "file://");
18
+ return searchParams.has(CONTENT_FLAG) && contentFileExts.some((ext) => pathname.endsWith(ext));
19
+ }
20
+ function astroContentImportPlugin({
21
+ fs,
22
+ settings
23
+ }) {
24
+ const contentPaths = getContentPaths(settings.config);
25
+ return {
26
+ name: "astro:content-imports",
27
+ async load(id) {
28
+ const { fileId } = getFileInfo(id, settings.config);
29
+ if (isContentFlagImport(id)) {
30
+ const observable = globalContentConfigObserver.get();
31
+ if (observable.status === "init") {
32
+ throw new AstroError({
33
+ ...AstroErrorData.UnknownContentCollectionError,
34
+ message: "Content config failed to load."
35
+ });
36
+ }
37
+ let contentConfig = observable.status === "loaded" ? observable.config : void 0;
38
+ if (observable.status === "loading") {
39
+ contentConfig = await new Promise((resolve) => {
40
+ const unsubscribe = globalContentConfigObserver.subscribe((ctx) => {
41
+ if (ctx.status === "loaded") {
42
+ resolve(ctx.config);
43
+ unsubscribe();
44
+ } else if (ctx.status === "error") {
45
+ resolve(void 0);
46
+ unsubscribe();
47
+ }
48
+ });
49
+ });
50
+ }
51
+ const rawContents = await fs.promises.readFile(fileId, "utf-8");
52
+ const {
53
+ content: body,
54
+ data: unparsedData,
55
+ matter: rawData = ""
56
+ } = parseFrontmatter(rawContents, fileId);
57
+ const entryInfo = getEntryInfo({
58
+ entry: pathToFileURL(fileId),
59
+ contentDir: contentPaths.contentDir
60
+ });
61
+ if (entryInfo instanceof Error)
62
+ return;
63
+ const _internal = { filePath: fileId, rawData };
64
+ const partialEntry = { data: unparsedData, body, _internal, ...entryInfo };
65
+ const slug = getEntrySlug(partialEntry);
66
+ const collectionConfig = contentConfig == null ? void 0 : contentConfig.collections[entryInfo.collection];
67
+ const data = collectionConfig ? await getEntryData(partialEntry, collectionConfig) : unparsedData;
68
+ const code = escapeViteEnvReferences(`
69
+ export const id = ${JSON.stringify(entryInfo.id)};
70
+ export const collection = ${JSON.stringify(entryInfo.collection)};
71
+ export const slug = ${JSON.stringify(slug)};
72
+ export const body = ${JSON.stringify(body)};
73
+ export const data = ${devalue.uneval(data)};
74
+ export const _internal = {
75
+ filePath: ${JSON.stringify(fileId)},
76
+ rawData: ${JSON.stringify(rawData)},
77
+ };
78
+ `);
79
+ return { code };
80
+ }
81
+ },
82
+ configureServer(viteServer) {
83
+ viteServer.watcher.on("all", async (event, entry) => {
84
+ if (["add", "unlink", "change"].includes(event) && getEntryType(entry, contentPaths) === "config") {
85
+ for (const modUrl of viteServer.moduleGraph.urlToModuleMap.keys()) {
86
+ if (isContentFlagImport(modUrl)) {
87
+ const mod = await viteServer.moduleGraph.getModuleByUrl(modUrl);
88
+ if (mod) {
89
+ viteServer.moduleGraph.invalidateModule(mod);
90
+ }
91
+ }
92
+ }
93
+ }
94
+ });
95
+ },
96
+ async transform(code, id) {
97
+ if (isContentFlagImport(id)) {
98
+ return { code: escapeViteEnvReferences(code) };
99
+ }
100
+ }
101
+ };
102
+ }
103
+ export {
104
+ astroContentImportPlugin
105
+ };
@@ -10,9 +10,11 @@ function deserializeManifest(serializedManifest) {
10
10
  route.routeData = deserializeRouteData(serializedRoute.routeData);
11
11
  }
12
12
  const assets = new Set(serializedManifest.assets);
13
+ const propagation = new Map(serializedManifest.propagation);
13
14
  return {
14
15
  ...serializedManifest,
15
16
  assets,
17
+ propagation,
16
18
  routes
17
19
  };
18
20
  }
@@ -189,6 +189,7 @@ renderPage_fn = async function(request, routeData, mod, status = 200) {
189
189
  request,
190
190
  origin: url.origin,
191
191
  pathname,
192
+ propagation: __privateGet(this, _manifest).propagation,
192
193
  scripts,
193
194
  links,
194
195
  route: routeData,
@@ -1,5 +1,5 @@
1
1
  import type { MarkdownRenderingOptions } from '@astrojs/markdown-remark';
2
- import type { ComponentInstance, RouteData, SerializedRouteData, SSRLoadedRenderer } from '../../@types/astro';
2
+ import type { ComponentInstance, PropagationHint, RouteData, SerializedRouteData, SSRLoadedRenderer, SSRResult } from '../../@types/astro';
3
3
  export type ComponentPath = string;
4
4
  export interface RouteInfo {
5
5
  routeData: RouteData;
@@ -26,9 +26,11 @@ export interface SSRManifest {
26
26
  renderers: SSRLoadedRenderer[];
27
27
  entryModules: Record<string, string>;
28
28
  assets: Set<string>;
29
+ propagation: SSRResult['propagation'];
29
30
  }
30
- export type SerializedSSRManifest = Omit<SSRManifest, 'routes' | 'assets'> & {
31
+ export type SerializedSSRManifest = Omit<SSRManifest, 'routes' | 'assets' | 'propagation'> & {
31
32
  routes: SerializedRouteInfo[];
32
33
  assets: string[];
34
+ propagation: readonly [string, PropagationHint][];
33
35
  };
34
36
  export type AdapterCreateExports<T = any> = (manifest: SSRManifest, args?: T) => Record<string, any>;
@@ -262,6 +262,7 @@ async function generatePath(pathname, opts, gopts) {
262
262
  origin,
263
263
  pathname,
264
264
  request: createRequest({ url, headers: new Headers(), logging, ssr }),
265
+ propagation: internals.propagation,
265
266
  scripts,
266
267
  links,
267
268
  route: pageData.route
@@ -54,6 +54,11 @@ class AstroBuilder {
54
54
  { settings: this.settings, logging, mode: "build" }
55
55
  );
56
56
  await runHookConfigDone({ settings: this.settings, logging });
57
+ const { sync } = await import("../../cli/sync/index.js");
58
+ const syncRet = await sync(this.settings, { logging, fs });
59
+ if (syncRet !== 0) {
60
+ return process.exit(syncRet);
61
+ }
57
62
  return { viteConfig };
58
63
  }
59
64
  async build({ viteConfig }) {
@@ -1,5 +1,6 @@
1
1
  import type { OutputChunk, RenderedChunk } from 'rollup';
2
2
  import type { PageBuildData, ViteID } from './types';
3
+ import { SSRResult } from '../../@types/astro';
3
4
  import { PageOptions } from '../../vite-plugin-astro/types';
4
5
  export interface BuildInternals {
5
6
  /**
@@ -47,6 +48,7 @@ export interface BuildInternals {
47
48
  discoveredScripts: Set<string>;
48
49
  staticFiles: Set<string>;
49
50
  ssrEntryChunk?: OutputChunk;
51
+ propagation: SSRResult['propagation'];
50
52
  }
51
53
  /**
52
54
  * Creates internal maps used to coordinate the CSS and HTML plugins.
@@ -16,7 +16,8 @@ function createBuildInternals() {
16
16
  discoveredHydratedComponents: /* @__PURE__ */ new Set(),
17
17
  discoveredClientOnlyComponents: /* @__PURE__ */ new Set(),
18
18
  discoveredScripts: /* @__PURE__ */ new Set(),
19
- staticFiles: /* @__PURE__ */ new Set()
19
+ staticFiles: /* @__PURE__ */ new Set(),
20
+ propagation: /* @__PURE__ */ new Map()
20
21
  };
21
22
  }
22
23
  function trackPageData(internals, component, pageData, componentModuleId, componentURL) {
@@ -28,7 +28,8 @@ async function collectPagesData(opts) {
28
28
  route,
29
29
  moduleSpecifier: "",
30
30
  css: /* @__PURE__ */ new Map(),
31
- contentCollectionCss: /* @__PURE__ */ new Map(),
31
+ propagatedStyles: /* @__PURE__ */ new Map(),
32
+ propagatedScripts: /* @__PURE__ */ new Map(),
32
33
  hoistedScript: void 0
33
34
  };
34
35
  clearInterval(routeCollectionLogTimeout);
@@ -48,7 +49,8 @@ async function collectPagesData(opts) {
48
49
  route,
49
50
  moduleSpecifier: "",
50
51
  css: /* @__PURE__ */ new Map(),
51
- contentCollectionCss: /* @__PURE__ */ new Map(),
52
+ propagatedStyles: /* @__PURE__ */ new Map(),
53
+ propagatedScripts: /* @__PURE__ */ new Map(),
52
54
  hoistedScript: void 0
53
55
  };
54
56
  }
@@ -0,0 +1,41 @@
1
+ import type { Plugin as VitePlugin } from 'vite';
2
+ import type { BuildInternals } from './internal';
3
+ import type { StaticBuildOptions, ViteBuildReturn } from './types';
4
+ type RollupOutputArray = Extract<ViteBuildReturn, Array<any>>;
5
+ type OutputChunkorAsset = RollupOutputArray[number]['output'][number];
6
+ type OutputChunk = Extract<OutputChunkorAsset, {
7
+ type: 'chunk';
8
+ }>;
9
+ type MutateChunk = (chunk: OutputChunk, build: 'server' | 'client', newCode: string) => void;
10
+ export type AstroBuildPlugin = {
11
+ build: 'ssr' | 'client' | 'both';
12
+ hooks?: {
13
+ 'build:before'?: (opts: {
14
+ build: 'ssr' | 'client';
15
+ input: Set<string>;
16
+ }) => {
17
+ enforce?: 'after-user-plugins';
18
+ vitePlugin: VitePlugin | VitePlugin[] | undefined;
19
+ };
20
+ 'build:post'?: (opts: {
21
+ ssrOutputs: RollupOutputArray;
22
+ clientOutputs: RollupOutputArray;
23
+ mutate: MutateChunk;
24
+ }) => void | Promise<void>;
25
+ };
26
+ };
27
+ export declare function createPluginContainer(options: StaticBuildOptions, internals: BuildInternals): {
28
+ options: StaticBuildOptions;
29
+ internals: BuildInternals;
30
+ register(plugin: AstroBuildPlugin): void;
31
+ runBeforeHook(build: 'ssr' | 'client', input: Set<string>): {
32
+ vitePlugins: (VitePlugin | VitePlugin[])[];
33
+ lastVitePlugins: (VitePlugin | VitePlugin[])[];
34
+ };
35
+ runPostHook(ssrReturn: ViteBuildReturn, clientReturn: ViteBuildReturn | null): Promise<Map<string, {
36
+ build: 'server' | 'client';
37
+ code: string;
38
+ }>>;
39
+ };
40
+ export type AstroBuildPluginContainer = ReturnType<typeof createPluginContainer>;
41
+ export {};
@@ -0,0 +1,82 @@
1
+ function createPluginContainer(options, internals) {
2
+ const clientPlugins = [];
3
+ const ssrPlugins = [];
4
+ const allPlugins = /* @__PURE__ */ new Set();
5
+ return {
6
+ options,
7
+ internals,
8
+ register(plugin) {
9
+ allPlugins.add(plugin);
10
+ switch (plugin.build) {
11
+ case "client": {
12
+ clientPlugins.push(plugin);
13
+ break;
14
+ }
15
+ case "ssr": {
16
+ ssrPlugins.push(plugin);
17
+ break;
18
+ }
19
+ case "both": {
20
+ clientPlugins.push(plugin);
21
+ ssrPlugins.push(plugin);
22
+ break;
23
+ }
24
+ }
25
+ },
26
+ runBeforeHook(build, input) {
27
+ var _a;
28
+ let plugins = build === "ssr" ? ssrPlugins : clientPlugins;
29
+ let vitePlugins = [];
30
+ let lastVitePlugins = [];
31
+ for (const plugin of plugins) {
32
+ if ((_a = plugin.hooks) == null ? void 0 : _a["build:before"]) {
33
+ let result = plugin.hooks["build:before"]({ build, input });
34
+ if (result.vitePlugin) {
35
+ vitePlugins.push(result.vitePlugin);
36
+ }
37
+ }
38
+ }
39
+ return {
40
+ vitePlugins,
41
+ lastVitePlugins
42
+ };
43
+ },
44
+ async runPostHook(ssrReturn, clientReturn) {
45
+ var _a;
46
+ const mutations = /* @__PURE__ */ new Map();
47
+ const ssrOutputs = [];
48
+ const clientOutputs = [];
49
+ if (Array.isArray(ssrReturn)) {
50
+ ssrOutputs.push(...ssrReturn);
51
+ } else if ("output" in ssrReturn) {
52
+ ssrOutputs.push(ssrReturn);
53
+ }
54
+ if (Array.isArray(clientReturn)) {
55
+ clientOutputs.push(...clientReturn);
56
+ } else if (clientReturn && "output" in clientReturn) {
57
+ clientOutputs.push(clientReturn);
58
+ }
59
+ const mutate = (chunk, build, newCode) => {
60
+ chunk.code = newCode;
61
+ mutations.set(chunk.fileName, {
62
+ build,
63
+ code: newCode
64
+ });
65
+ };
66
+ for (const plugin of allPlugins) {
67
+ const postHook = (_a = plugin.hooks) == null ? void 0 : _a["build:post"];
68
+ if (postHook) {
69
+ await postHook({
70
+ ssrOutputs,
71
+ clientOutputs,
72
+ mutate
73
+ });
74
+ }
75
+ }
76
+ return mutations;
77
+ }
78
+ };
79
+ }
80
+ export {
81
+ createPluginContainer
82
+ };
@@ -0,0 +1,2 @@
1
+ import type { AstroBuildPluginContainer } from '../plugin';
2
+ export declare function registerAllPlugins({ internals, options, register }: AstroBuildPluginContainer): void;
@@ -0,0 +1,25 @@
1
+ import { astroConfigBuildPlugin } from "../../../content/vite-plugin-content-assets.js";
2
+ import { astroHeadPropagationBuildPlugin } from "../../../vite-plugin-head-propagation/index.js";
3
+ import { pluginAliasResolve } from "./plugin-alias-resolve.js";
4
+ import { pluginAnalyzer } from "./plugin-analyzer.js";
5
+ import { pluginCSS } from "./plugin-css.js";
6
+ import { pluginHoistedScripts } from "./plugin-hoisted-scripts.js";
7
+ import { pluginInternals } from "./plugin-internals.js";
8
+ import { pluginPages } from "./plugin-pages.js";
9
+ import { pluginPrerender } from "./plugin-prerender.js";
10
+ import { pluginSSR } from "./plugin-ssr.js";
11
+ function registerAllPlugins({ internals, options, register }) {
12
+ register(pluginAliasResolve(internals));
13
+ register(pluginAnalyzer(internals));
14
+ register(pluginInternals(internals));
15
+ register(pluginPages(options, internals));
16
+ register(pluginCSS(options, internals));
17
+ register(astroHeadPropagationBuildPlugin(options, internals));
18
+ register(pluginPrerender(options, internals));
19
+ register(astroConfigBuildPlugin(options, internals));
20
+ register(pluginHoistedScripts(options, internals));
21
+ register(pluginSSR(options, internals));
22
+ }
23
+ export {
24
+ registerAllPlugins
25
+ };
@@ -1,8 +1,10 @@
1
1
  import type { Plugin as VitePlugin } from 'vite';
2
- import type { BuildInternals } from '../../core/build/internal.js';
2
+ import type { BuildInternals } from '../internal.js';
3
+ import { AstroBuildPlugin } from '../plugin.js';
3
4
  /**
4
5
  * `@rollup/plugin-alias` doesn't resolve aliases in Rollup input by default. This plugin fixes it
5
6
  * with a partial fork of it's resolve function. https://github.com/rollup/plugins/blob/master/packages/alias/src/index.ts
6
7
  * When https://github.com/rollup/plugins/pull/1402 is merged, we can remove this plugin.
7
8
  */
8
9
  export declare function vitePluginAliasResolve(internals: BuildInternals): VitePlugin;
10
+ export declare function pluginAliasResolve(internals: BuildInternals): AstroBuildPlugin;
@@ -32,6 +32,19 @@ function matches(pattern, importee) {
32
32
  }
33
33
  return importee.startsWith(pattern + "/");
34
34
  }
35
+ function pluginAliasResolve(internals) {
36
+ return {
37
+ build: "client",
38
+ hooks: {
39
+ "build:before": () => {
40
+ return {
41
+ vitePlugin: vitePluginAliasResolve(internals)
42
+ };
43
+ }
44
+ }
45
+ };
46
+ }
35
47
  export {
48
+ pluginAliasResolve,
36
49
  vitePluginAliasResolve
37
50
  };
@@ -0,0 +1,5 @@
1
+ import type { Plugin as VitePlugin } from 'vite';
2
+ import type { BuildInternals } from '../internal.js';
3
+ import type { AstroBuildPlugin } from '../plugin.js';
4
+ export declare function vitePluginAnalyzer(internals: BuildInternals): VitePlugin;
5
+ export declare function pluginAnalyzer(internals: BuildInternals): AstroBuildPlugin;
@@ -1,6 +1,14 @@
1
- import { prependForwardSlash } from "../../core/path.js";
2
- import { getTopLevelPages } from "./graph.js";
3
- import { getPageDataByViteID, trackClientOnlyPageDatas } from "./internal.js";
1
+ import { PROPAGATED_ASSET_FLAG } from "../../../content/consts.js";
2
+ import { prependForwardSlash } from "../../../core/path.js";
3
+ import { getTopLevelPages, moduleIsTopLevelPage, walkParentInfos } from "../graph.js";
4
+ import { getPageDataByViteID, trackClientOnlyPageDatas } from "../internal.js";
5
+ function isPropagatedAsset(id) {
6
+ try {
7
+ return new URL("file://" + id).searchParams.has(PROPAGATED_ASSET_FLAG);
8
+ } catch {
9
+ return false;
10
+ }
11
+ }
4
12
  function vitePluginAnalyzer(internals) {
5
13
  function hoistedScriptScanner() {
6
14
  const uniqueHoistedIds = /* @__PURE__ */ new Map();
@@ -14,26 +22,49 @@ function vitePluginAnalyzer(internals) {
14
22
  hoistedScripts.add(hid);
15
23
  }
16
24
  if (hoistedScripts.size) {
17
- for (const [pageInfo] of getTopLevelPages(from, this)) {
18
- const pageId = pageInfo.id;
19
- for (const hid of hoistedScripts) {
20
- if (pageScripts.has(pageId)) {
21
- (_a = pageScripts.get(pageId)) == null ? void 0 : _a.add(hid);
22
- } else {
23
- pageScripts.set(pageId, /* @__PURE__ */ new Set([hid]));
25
+ for (const [parentInfo] of walkParentInfos(from, this, function until(importer) {
26
+ return isPropagatedAsset(importer);
27
+ })) {
28
+ if (isPropagatedAsset(parentInfo.id)) {
29
+ for (const [nestedParentInfo] of walkParentInfos(from, this)) {
30
+ if (moduleIsTopLevelPage(nestedParentInfo)) {
31
+ for (const hid of hoistedScripts) {
32
+ if (!pageScripts.has(nestedParentInfo.id)) {
33
+ pageScripts.set(nestedParentInfo.id, {
34
+ hoistedSet: /* @__PURE__ */ new Set(),
35
+ propagatedMapByImporter: /* @__PURE__ */ new Map()
36
+ });
37
+ }
38
+ const entry = pageScripts.get(nestedParentInfo.id);
39
+ if (!entry.propagatedMapByImporter.has(parentInfo.id)) {
40
+ entry.propagatedMapByImporter.set(parentInfo.id, /* @__PURE__ */ new Set());
41
+ }
42
+ entry.propagatedMapByImporter.get(parentInfo.id).add(hid);
43
+ }
44
+ }
45
+ }
46
+ } else if (moduleIsTopLevelPage(parentInfo)) {
47
+ for (const hid of hoistedScripts) {
48
+ if (!pageScripts.has(parentInfo.id)) {
49
+ pageScripts.set(parentInfo.id, {
50
+ hoistedSet: /* @__PURE__ */ new Set(),
51
+ propagatedMapByImporter: /* @__PURE__ */ new Map()
52
+ });
53
+ }
54
+ (_a = pageScripts.get(parentInfo.id)) == null ? void 0 : _a.hoistedSet.add(hid);
24
55
  }
25
56
  }
26
57
  }
27
58
  }
28
59
  },
29
60
  finalize() {
30
- for (const [pageId, hoistedScripts] of pageScripts) {
61
+ for (const [pageId, { hoistedSet, propagatedMapByImporter }] of pageScripts) {
31
62
  const pageData = getPageDataByViteID(internals, pageId);
32
63
  if (!pageData)
33
64
  continue;
34
65
  const { component } = pageData;
35
66
  const astroModuleId = prependForwardSlash(component);
36
- const uniqueHoistedId = JSON.stringify(Array.from(hoistedScripts).sort());
67
+ const uniqueHoistedId = JSON.stringify(Array.from(hoistedSet).sort());
37
68
  let moduleId;
38
69
  if (uniqueHoistedIds.has(uniqueHoistedId)) {
39
70
  moduleId = uniqueHoistedIds.get(uniqueHoistedId);
@@ -42,12 +73,18 @@ function vitePluginAnalyzer(internals) {
42
73
  uniqueHoistedIds.set(uniqueHoistedId, moduleId);
43
74
  }
44
75
  internals.discoveredScripts.add(moduleId);
76
+ pageData.propagatedScripts = propagatedMapByImporter;
77
+ for (const propagatedScripts of propagatedMapByImporter.values()) {
78
+ for (const propagatedScript of propagatedScripts) {
79
+ internals.discoveredScripts.add(propagatedScript);
80
+ }
81
+ }
45
82
  if (internals.hoistedScriptIdToPagesMap.has(moduleId)) {
46
83
  const pages = internals.hoistedScriptIdToPagesMap.get(moduleId);
47
84
  pages.add(astroModuleId);
48
85
  } else {
49
86
  internals.hoistedScriptIdToPagesMap.set(moduleId, /* @__PURE__ */ new Set([astroModuleId]));
50
- internals.hoistedScriptIdToHoistedMap.set(moduleId, hoistedScripts);
87
+ internals.hoistedScriptIdToHoistedMap.set(moduleId, hoistedSet);
51
88
  }
52
89
  }
53
90
  }
@@ -96,6 +133,19 @@ function vitePluginAnalyzer(internals) {
96
133
  }
97
134
  };
98
135
  }
136
+ function pluginAnalyzer(internals) {
137
+ return {
138
+ build: "ssr",
139
+ hooks: {
140
+ "build:before": () => {
141
+ return {
142
+ vitePlugin: vitePluginAnalyzer(internals)
143
+ };
144
+ }
145
+ }
146
+ };
147
+ }
99
148
  export {
149
+ pluginAnalyzer,
100
150
  vitePluginAnalyzer
101
151
  };
@@ -1,10 +1,12 @@
1
1
  import { Plugin as VitePlugin } from 'vite';
2
- import type { BuildInternals } from './internal';
3
- import type { StaticBuildOptions } from './types';
2
+ import type { BuildInternals } from '../internal';
3
+ import type { AstroBuildPlugin } from '../plugin';
4
+ import type { StaticBuildOptions } from '../types';
4
5
  interface PluginOptions {
5
6
  internals: BuildInternals;
6
7
  buildOptions: StaticBuildOptions;
7
8
  target: 'client' | 'server';
8
9
  }
9
10
  export declare function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[];
11
+ export declare function pluginCSS(options: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin;
10
12
  export {};
@@ -1,17 +1,18 @@
1
1
  import * as crypto from "node:crypto";
2
2
  import * as npath from "node:path";
3
3
  import { transformWithEsbuild } from "vite";
4
- import { isCSSRequest } from "../render/util.js";
5
- import { PROPAGATED_ASSET_FLAG } from "../../content/consts.js";
6
- import * as assetName from "./css-asset-name.js";
7
- import { moduleIsTopLevelPage, walkParentInfos } from "./graph.js";
4
+ import { isBuildableCSSRequest } from "../../render/dev/util.js";
5
+ import { PROPAGATED_ASSET_FLAG } from "../../../content/consts.js";
6
+ import * as assetName from "../css-asset-name.js";
7
+ import { moduleIsTopLevelPage, walkParentInfos } from "../graph.js";
8
8
  import {
9
9
  eachPageData,
10
10
  getPageDataByViteID,
11
11
  getPageDatasByClientOnlyID,
12
12
  getPageDatasByHoistedScriptId,
13
13
  isHoistedScript
14
- } from "./internal.js";
14
+ } from "../internal.js";
15
+ import { extendManualChunks } from "./util.js";
15
16
  function rollupPluginAstroBuildCSS(options) {
16
17
  const { internals, buildOptions } = options;
17
18
  const { settings } = buildOptions;
@@ -35,32 +36,23 @@ function rollupPluginAstroBuildCSS(options) {
35
36
  {
36
37
  name: "astro:rollup-plugin-build-css",
37
38
  outputOptions(outputOptions) {
38
- const manualChunks = outputOptions.manualChunks || Function.prototype;
39
39
  const assetFileNames = outputOptions.assetFileNames;
40
40
  const namingIncludesHash = assetFileNames == null ? void 0 : assetFileNames.toString().includes("[hash]");
41
41
  const createNameForParentPages = namingIncludesHash ? assetName.shortHashedName : assetName.createSlugger(settings);
42
- outputOptions.manualChunks = function(id, ...args) {
43
- if (typeof manualChunks == "object") {
44
- if (id in manualChunks) {
45
- return manualChunks[id];
46
- }
47
- } else if (typeof manualChunks === "function") {
48
- const outid = manualChunks.call(this, id, ...args);
49
- if (outid) {
50
- return outid;
51
- }
52
- }
53
- if (isCSSRequest(id)) {
54
- for (const [pageInfo] of walkParentInfos(id, {
55
- getModuleInfo: args[0].getModuleInfo
56
- })) {
57
- if (new URL(pageInfo.id, "file://").searchParams.has(PROPAGATED_ASSET_FLAG)) {
58
- return createNameHash(id, [id]);
42
+ extendManualChunks(outputOptions, {
43
+ after(id, meta) {
44
+ if (isBuildableCSSRequest(id)) {
45
+ for (const [pageInfo] of walkParentInfos(id, {
46
+ getModuleInfo: meta.getModuleInfo
47
+ })) {
48
+ if (new URL(pageInfo.id, "file://").searchParams.has(PROPAGATED_ASSET_FLAG)) {
49
+ return createNameHash(id, [id]);
50
+ }
59
51
  }
52
+ return createNameForParentPages(id, meta);
60
53
  }
61
- return createNameForParentPages(id, args[0]);
62
54
  }
63
- };
55
+ });
64
56
  },
65
57
  async generateBundle(_outputOptions, bundle) {
66
58
  const appendCSSToPage = (pageData, meta, depth, order) => {
@@ -125,8 +117,8 @@ function rollupPluginAstroBuildCSS(options) {
125
117
  const pageData = getPageDataByViteID(internals, pageViteID);
126
118
  if (pageData) {
127
119
  for (const css of meta.importedCss) {
128
- const existingCss = pageData.contentCollectionCss.get(pageInfo.id) ?? /* @__PURE__ */ new Set();
129
- pageData.contentCollectionCss.set(
120
+ const existingCss = pageData.propagatedStyles.get(pageInfo.id) ?? /* @__PURE__ */ new Set();
121
+ pageData.propagatedStyles.set(
130
122
  pageInfo.id,
131
123
  /* @__PURE__ */ new Set([...existingCss, css])
132
124
  );
@@ -205,6 +197,24 @@ function rollupPluginAstroBuildCSS(options) {
205
197
  }
206
198
  ];
207
199
  }
200
+ function pluginCSS(options, internals) {
201
+ return {
202
+ build: "both",
203
+ hooks: {
204
+ "build:before": ({ build }) => {
205
+ let plugins = rollupPluginAstroBuildCSS({
206
+ buildOptions: options,
207
+ internals,
208
+ target: build === "ssr" ? "server" : "client"
209
+ });
210
+ return {
211
+ vitePlugin: plugins
212
+ };
213
+ }
214
+ }
215
+ };
216
+ }
208
217
  export {
218
+ pluginCSS,
209
219
  rollupPluginAstroBuildCSS
210
220
  };
@@ -0,0 +1,7 @@
1
+ import type { Plugin as VitePlugin } from 'vite';
2
+ import type { AstroSettings } from '../../../@types/astro';
3
+ import type { BuildInternals } from '../internal.js';
4
+ import { AstroBuildPlugin } from '../plugin';
5
+ import { StaticBuildOptions } from '../types';
6
+ export declare function vitePluginHoistedScripts(settings: AstroSettings, internals: BuildInternals): VitePlugin;
7
+ export declare function pluginHoistedScripts(options: StaticBuildOptions, internals: BuildInternals): AstroBuildPlugin;