Skip to content

Commit

Permalink
pre-bundle SSR entry points
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Jun 18, 2023
1 parent efea74f commit c366c32
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
22 changes: 20 additions & 2 deletions packages/cli/src/config/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,28 @@ const getRollupConfigForSsr = async (compilation, input) => {
},
plugins: [
greenwoodJsonLoader(),
nodeResolve(),
// TODO should this be used in all configs?
nodeResolve({
preferBuiltins: true
}),
commonjs(),
importMetaAssets()
]
],
onwarn: (errorObj) => {
const { code, message } = errorObj;

switch (code) {

case 'CIRCULAR_DEPENDENCY':
// let this through for lit to enable nodeResolve({ preferBuiltins: true })
// https://github.com/lit/lit/issues/449
break;
default:
// otherwise, log all warnings from rollup
console.debug(message);

}
}
}];
};

Expand Down
22 changes: 7 additions & 15 deletions packages/cli/src/lifecycles/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@ async function bundleSsrPages(compilation) {
const htmlOptimizer = compilation.config.plugins.find(plugin => plugin.name === 'plugin-standard-html').provider(compilation);
const { executeModuleUrl } = compilation.config.plugins.find(plugin => plugin.type === 'renderer').provider();
const { executeRouteModule } = await import(executeModuleUrl);
const { outputDir, pagesDir } = compilation.context;
const { outputDir, pagesDir, scratchDir } = compilation.context;

for (const page of compilation.graph) {
if (page.isSSR && !page.data.static) {
const { filename, imports, route, template, title } = page;
const outputUrl = new URL(`./${filename}`, outputDir);
const entryFileUrl = new URL(`./_${filename}`, scratchDir);
const moduleUrl = new URL(`./${filename}`, pagesDir);
// TODO getTemplate has to be static (for now?)
// const { getTemplate = null } = await import(new URL(`./${filename}`, pagesDir));
Expand All @@ -217,9 +217,10 @@ async function bundleSsrPages(compilation) {
// better way to write out this inline code?
// TODO does executeRouteModule need to get bundled?
// TODO do we need to bundle this too since we reference executeModuleUrl.href directly?
await fs.writeFile(outputUrl, `
await fs.writeFile(entryFileUrl, `
import { executeRouteModule } from '${normalizePathnameForWindows(executeModuleUrl)}';
export async function handler(request) {
const { executeRouteModule } = await import('${executeModuleUrl.href}');
const compilation = JSON.parse('${JSON.stringify(compilation)}');
const page = JSON.parse('${JSON.stringify(page)}');
const moduleUrl = new URL('./_${filename}', '${outputDir.href}');
Expand All @@ -238,24 +239,15 @@ async function bundleSsrPages(compilation) {
}
`);

// console.log('=========================');

// TODO should we bundle entry points?
input.push(normalizePathnameForWindows(new URL(`./${filename}`, pagesDir)));
input.push(normalizePathnameForWindows(moduleUrl));
input.push(normalizePathnameForWindows(entryFileUrl));
}
}

const [rollupConfig] = await getRollupConfigForSsr(compilation, input);

// TODO do we need templates anymore?
if (rollupConfig.input.length > 0) {
const { userTemplatesDir, outputDir } = compilation.context;

// TODO can this be removed
if (await checkResourceExists(userTemplatesDir)) {
await fs.cp(userTemplatesDir, new URL('./_templates/', outputDir), { recursive: true });
}

const bundle = await rollup(rollupConfig);
await bundle.write(rollupConfig.output);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/lifecycles/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ async function getHybridServer(compilation) {
});

if (!config.prerender && matchingRoute.isSSR && !matchingRoute.data.static) {
const { handler } = await import(new URL(`./${matchingRoute.filename}`, outputDir));
const { handler } = await import(new URL(`./__${matchingRoute.filename}`, outputDir));
// TODO passing compilation this way too hacky?
// https://github.com/ProjectEvergreen/greenwood/issues/1008
const response = await handler(request, compilation);
Expand Down

0 comments on commit c366c32

Please sign in to comment.