astro-ghostcms/.pnpm-store/v3/files/aa/18a60ebcfc3689d5873d35d81e8...

62 lines
1.6 KiB
Plaintext
Raw Normal View History

2024-02-14 14:10:47 +00:00
function createPluginContainer(options, internals) {
const plugins = /* @__PURE__ */ new Map();
const allPlugins = /* @__PURE__ */ new Set();
for (const target of ["client", "server"]) {
plugins.set(target, []);
}
return {
options,
internals,
register(plugin) {
allPlugins.add(plugin);
for (const target of plugin.targets) {
const targetPlugins = plugins.get(target) ?? [];
targetPlugins.push(plugin);
plugins.set(target, targetPlugins);
}
},
// Hooks
async runBeforeHook(target, input) {
let targetPlugins = plugins.get(target) ?? [];
let vitePlugins = [];
let lastVitePlugins = [];
for (const plugin of targetPlugins) {
if (plugin.hooks?.["build:before"]) {
let result = await plugin.hooks["build:before"]({ target, input });
if (result.vitePlugin) {
vitePlugins.push(result.vitePlugin);
}
}
}
return {
vitePlugins,
lastVitePlugins
};
},
async runPostHook(ssrOutputs, clientOutputs) {
const mutations = /* @__PURE__ */ new Map();
const mutate = (chunk, targets, newCode) => {
chunk.code = newCode;
mutations.set(chunk.fileName, {
targets,
code: newCode
});
};
for (const plugin of allPlugins) {
const postHook = plugin.hooks?.["build:post"];
if (postHook) {
await postHook({
ssrOutputs,
clientOutputs,
mutate
});
}
}
return mutations;
}
};
}
export {
createPluginContainer
};