mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
refactor: move renderers to their own plugin (#7166)
This commit is contained in:
parent
4ce8bf7c62
commit
626dd41d0a
7 changed files with 90 additions and 24 deletions
5
.changeset/early-cars-kick.md
Normal file
5
.changeset/early-cars-kick.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Move generation of renderers code into their own file
|
|
@ -11,12 +11,14 @@ import { pluginMiddleware } from './plugin-middleware.js';
|
|||
import { pluginPages } from './plugin-pages.js';
|
||||
import { pluginPrerender } from './plugin-prerender.js';
|
||||
import { pluginSSR } from './plugin-ssr.js';
|
||||
import { pluginRenderers } from './plugin-renderers.js';
|
||||
|
||||
export function registerAllPlugins({ internals, options, register }: AstroBuildPluginContainer) {
|
||||
register(pluginComponentEntry(internals));
|
||||
register(pluginAliasResolve(internals));
|
||||
register(pluginAnalyzer(internals));
|
||||
register(pluginInternals(internals));
|
||||
register(pluginRenderers(options, internals));
|
||||
register(pluginMiddleware(options, internals));
|
||||
register(pluginPages(options, internals));
|
||||
register(pluginCSS(options, internals));
|
||||
|
|
|
@ -5,6 +5,7 @@ import { eachPageData, type BuildInternals } from '../internal.js';
|
|||
import type { AstroBuildPlugin } from '../plugin';
|
||||
import type { StaticBuildOptions } from '../types';
|
||||
import { MIDDLEWARE_MODULE_ID } from './plugin-middleware.js';
|
||||
import { RENDERERS_MODULE_ID } from './plugin-renderers.js';
|
||||
|
||||
function vitePluginPages(opts: StaticBuildOptions, internals: BuildInternals): VitePlugin {
|
||||
return {
|
||||
|
@ -25,8 +26,12 @@ function vitePluginPages(opts: StaticBuildOptions, internals: BuildInternals): V
|
|||
async load(id) {
|
||||
if (id === resolvedPagesVirtualModuleId) {
|
||||
let importMap = '';
|
||||
let imports = [];
|
||||
const imports: string[] = [];
|
||||
const exports: string[] = [];
|
||||
const content: string[] = [];
|
||||
let i = 0;
|
||||
imports.push(`import { renderers } from "${RENDERERS_MODULE_ID}";`);
|
||||
exports.push(`export { renderers };`);
|
||||
for (const pageData of eachPageData(internals)) {
|
||||
const variable = `_page${i}`;
|
||||
imports.push(
|
||||
|
@ -36,31 +41,14 @@ function vitePluginPages(opts: StaticBuildOptions, internals: BuildInternals): V
|
|||
i++;
|
||||
}
|
||||
|
||||
i = 0;
|
||||
let rendererItems = '';
|
||||
for (const renderer of opts.settings.renderers) {
|
||||
const variable = `_renderer${i}`;
|
||||
// Use unshift so that renderers are imported before user code, in case they set globals
|
||||
// that user code depends on.
|
||||
imports.unshift(`import ${variable} from '${renderer.serverEntrypoint}';`);
|
||||
rendererItems += `Object.assign(${JSON.stringify(renderer)}, { ssr: ${variable} }),`;
|
||||
i++;
|
||||
if (opts.settings.config.experimental.middleware) {
|
||||
imports.push(`import * as _middleware from "${MIDDLEWARE_MODULE_ID}";`);
|
||||
exports.push(`export const middleware = _middleware;`);
|
||||
}
|
||||
|
||||
const def = `${imports.join('\n')}
|
||||
content.push(`export const pageMap = new Map([${importMap}]);`);
|
||||
|
||||
${
|
||||
opts.settings.config.experimental.middleware
|
||||
? `import * as _middleware from "${MIDDLEWARE_MODULE_ID}";`
|
||||
: ''
|
||||
}
|
||||
|
||||
export const pageMap = new Map([${importMap}]);
|
||||
export const renderers = [${rendererItems}];
|
||||
${opts.settings.config.experimental.middleware ? `export const middleware = _middleware;` : ''}
|
||||
`;
|
||||
|
||||
return def;
|
||||
return `${imports.join('\n')}${content.join('\n')}${exports.join('\n')}`;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
|
66
packages/astro/src/core/build/plugins/plugin-renderers.ts
Normal file
66
packages/astro/src/core/build/plugins/plugin-renderers.ts
Normal file
|
@ -0,0 +1,66 @@
|
|||
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')}`;
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function pluginRenderers(
|
||||
opts: StaticBuildOptions,
|
||||
internals: BuildInternals
|
||||
): AstroBuildPlugin {
|
||||
return {
|
||||
build: 'ssr',
|
||||
hooks: {
|
||||
'build:before': () => {
|
||||
return {
|
||||
vitePlugin: vitePluginRenderers(opts, internals),
|
||||
};
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
|
@ -15,6 +15,7 @@ import { addRollupInput } from '../add-rollup-input.js';
|
|||
import { getOutFile, getOutFolder } from '../common.js';
|
||||
import { cssOrder, mergeInlineCss, type BuildInternals } from '../internal.js';
|
||||
import type { AstroBuildPlugin } from '../plugin';
|
||||
import { RENDERERS_MODULE_ID } from './plugin-renderers.js';
|
||||
|
||||
export const virtualModuleId = '@astrojs-ssr-virtual-entry';
|
||||
const resolvedVirtualModuleId = '\0' + virtualModuleId;
|
||||
|
@ -44,6 +45,7 @@ function vitePluginSSR(
|
|||
middleware = 'middleware: _main.middleware';
|
||||
}
|
||||
return `import * as adapter from '${adapter.serverEntrypoint}';
|
||||
import { renderers } from '${RENDERERS_MODULE_ID}';
|
||||
import * as _main from '${pagesVirtualModuleId}';
|
||||
import { deserializeManifest as _deserializeManifest } from 'astro/app';
|
||||
import { _privateSetManifestDontUseThis } from 'astro:ssr-manifest';
|
||||
|
|
|
@ -28,6 +28,7 @@ import { registerAllPlugins } from './plugins/index.js';
|
|||
import { RESOLVED_MIDDLEWARE_MODULE_ID } from './plugins/plugin-middleware.js';
|
||||
import type { PageBuildData, StaticBuildOptions } from './types';
|
||||
import { getTimeStat } from './util.js';
|
||||
import { RESOLVED_RENDERERS_MODULE_ID } from './plugins/plugin-renderers.js';
|
||||
|
||||
export async function viteBuild(opts: StaticBuildOptions) {
|
||||
const { allPages, settings } = opts;
|
||||
|
@ -175,6 +176,8 @@ async function ssrBuild(
|
|||
return opts.buildConfig.serverEntry;
|
||||
} else if (chunkInfo.facadeModuleId === RESOLVED_MIDDLEWARE_MODULE_ID) {
|
||||
return 'middleware.mjs';
|
||||
} else if (chunkInfo.facadeModuleId === RESOLVED_RENDERERS_MODULE_ID) {
|
||||
return 'renderers.mjs';
|
||||
} else {
|
||||
return '[name].mjs';
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ describe('Sourcemap', async () => {
|
|||
});
|
||||
|
||||
it('Builds non-empty sourcemap', async () => {
|
||||
const map = await fixture.readFile('entry.mjs.map');
|
||||
const map = await fixture.readFile('renderers.mjs.map');
|
||||
expect(map).to.not.include('"sources":[]');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue