/
plugin-renderers.ts
70 lines (60 loc) · 1.74 KB
/
plugin-renderers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import type { Plugin as VitePlugin } from 'vite';
import { addRollupInput } from '../add-rollup-input.js';
import type { BuildInternals } from '../internal.js';
import type { AstroBuildPlugin } from '../plugin';
import type { StaticBuildOptions } from '../types';
export const RENDERERS_MODULE_ID = '@astro-renderers';
export const RESOLVED_RENDERERS_MODULE_ID = `\0${RENDERERS_MODULE_ID}`;
let inputs: Set<string> = new Set();
export function vitePluginRenderers(
opts: StaticBuildOptions,
_internals: BuildInternals
): VitePlugin {
return {
name: '@astro/plugin-renderers',
options(options) {
return addRollupInput(options, [RENDERERS_MODULE_ID]);
},
resolveId(id) {
if (id === RENDERERS_MODULE_ID) {
return RESOLVED_RENDERERS_MODULE_ID;
}
},
async load(id) {
if (id === RESOLVED_RENDERERS_MODULE_ID) {
if (opts.settings.renderers.length > 0) {
const imports: string[] = [];
const exports: string[] = [];
let i = 0;
let rendererItems = '';
for (const renderer of opts.settings.renderers) {
const variable = `_renderer${i}`;
imports.push(`import ${variable} from '${renderer.serverEntrypoint}';`);
rendererItems += `Object.assign(${JSON.stringify(renderer)}, { ssr: ${variable} }),`;
i++;
}
exports.push(`export const renderers = [${rendererItems}];`);
return `${imports.join('\n')}\n${exports.join('\n')}`;
}
}
},
outputOptions(options) {
options.entryFileNames = 'renderers.mjs';
},
};
}
export function pluginRenderers(
opts: StaticBuildOptions,
internals: BuildInternals
): AstroBuildPlugin {
return {
build: 'ssr',
hooks: {
'build:before': () => {
return {
vitePlugin: vitePluginRenderers(opts, internals),
};
},
},
};
}