kjelsrud.dev/node_modules/astro/dist/vite-plugin-head/index.js
2023-07-19 21:31:30 +02:00

93 lines
3 KiB
JavaScript

import { getTopLevelPages, walkParentInfos } from "../core/build/graph.js";
import { getAstroMetadata } from "../vite-plugin-astro/index.js";
const injectExp = /(^\/\/|\/\/!)\s*astro-head-inject/;
function configHeadVitePlugin() {
let server;
function propagateMetadata(id, prop, value, seen = /* @__PURE__ */ new Set()) {
if (seen.has(id))
return;
seen.add(id);
const mod = server.moduleGraph.getModuleById(id);
const info = this.getModuleInfo(id);
if (info == null ? void 0 : info.meta.astro) {
const astroMetadata = getAstroMetadata(info);
if (astroMetadata) {
Reflect.set(astroMetadata, prop, value);
}
}
for (const parent of (mod == null ? void 0 : mod.importers) || []) {
if (parent.id) {
propagateMetadata.call(this, parent.id, prop, value, seen);
}
}
}
return {
name: "astro:head-metadata",
configureServer(_server) {
server = _server;
},
transform(source, id) {
var _a;
if (!server) {
return;
}
let info = this.getModuleInfo(id);
if (info && ((_a = getAstroMetadata(info)) == null ? void 0 : _a.containsHead)) {
propagateMetadata.call(this, id, "containsHead", true);
}
if (injectExp.test(source)) {
propagateMetadata.call(this, id, "propagation", "in-tree");
}
}
};
}
function astroHeadBuildPlugin(internals) {
return {
build: "ssr",
hooks: {
"build:before"() {
return {
vitePlugin: {
name: "astro:head-metadata-build",
generateBundle(_opts, bundle) {
var _a;
const map = internals.componentMetadata;
function getOrCreateMetadata(id) {
if (map.has(id))
return map.get(id);
const metadata = {
propagation: "none",
containsHead: false
};
map.set(id, metadata);
return metadata;
}
for (const [, output] of Object.entries(bundle)) {
if (output.type !== "chunk")
continue;
for (const [id, mod] of Object.entries(output.modules)) {
const modinfo = this.getModuleInfo(id);
if (modinfo && ((_a = getAstroMetadata(modinfo)) == null ? void 0 : _a.containsHead)) {
for (const [pageInfo] of getTopLevelPages(id, this)) {
let metadata = getOrCreateMetadata(pageInfo.id);
metadata.containsHead = true;
}
}
if (mod.code && injectExp.test(mod.code)) {
for (const [info] of walkParentInfos(id, this)) {
getOrCreateMetadata(info.id).propagation = "in-tree";
}
}
}
}
}
}
};
}
}
};
}
export {
astroHeadBuildPlugin,
configHeadVitePlugin as default
};