Skip to content

Commit

Permalink
bug/issue 1226 nested SSR pages release branch rebase and refactor (#…
Browse files Browse the repository at this point in the history
…1236)

* rebase and refactor nested SSR pages work

* clean up console logs

* windows path handling

* better windows pathname support when syncing SSR pages
  • Loading branch information
thescientist13 authored Jun 1, 2024
1 parent bb4fdc5 commit 1a21f68
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 40 deletions.
102 changes: 67 additions & 35 deletions packages/cli/src/config/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,31 @@ function greenwoodSyncPageResourceBundlesPlugin(compilation) {
};
}

function greenwoodSyncSsrEntryPointsOutputPaths(compilation) {
return {
name: 'greenwood-sync-ssr-pages-entry-point-output-paths',
generateBundle(options, bundle) {
const { basePath } = compilation.config;
const { scratchDir } = compilation.context;

// map rollup bundle names back to original SSR pages for syncing input <> output bundle names
Object.keys(bundle).forEach((key) => {
if (bundle[key].exports?.find(exp => exp === 'handler')) {
const ext = bundle[key].facadeModuleId.split('.').pop();
// account for windows pathname shenanigans by "casting" facadeModuleId to a URL first
const route = new URL(`file://${bundle[key].facadeModuleId}`).pathname.replace(scratchDir.pathname, `${basePath}/`).replace(`.${ext}`, '/').replace('/index/', '/');

compilation.graph.forEach((page, idx) => {
if (page.route === route) {
compilation.graph[idx].outputPath = key;
}
});
}
});
}
};
}

function getMetaImportPath(node) {
return node.arguments[0].value.split('/').join(path.sep)
.replace(/\\/g, '/'); // handle Windows style paths
Expand Down Expand Up @@ -427,44 +452,51 @@ const getRollupConfigForApis = async (compilation) => {
const getRollupConfigForSsr = async (compilation, input) => {
const { outputDir } = compilation.context;

return input.map(filepath => ({
input: filepath,
output: {
dir: normalizePathnameForWindows(outputDir),
entryFileNames: `${path.basename(filepath).split('.')[0]}.route.js`,
chunkFileNames: `${path.basename(filepath).split('.')[0]}.route.chunk.[hash].js`
},
plugins: [
greenwoodResourceLoader(compilation),
// support node export conditions for SSR pages
// https://github.com/ProjectEvergreen/greenwood/issues/1118
// https://github.com/rollup/plugins/issues/362#issuecomment-873448461
nodeResolve({
exportConditions: ['node'],
preferBuiltins: true
}),
commonjs(),
greenwoodImportMetaUrl(compilation)
],
onwarn: (errorObj) => {
const { code, message } = errorObj;

switch (code) {
return input.map((filepath) => {
const ext = filepath.split('.').pop();
// account for windows pathname shenanigans by "casting" filepath to a URL first
const entryName = new URL(`file://${filepath}`).pathname.replace(compilation.context.scratchDir.pathname, '').replace('/', '-').replace(`.${ext}`, '');

case 'CIRCULAR_DEPENDENCY':
// TODO let this through for lit by suppressing it
// Error: the string "Circular dependency: ../../../../../node_modules/@lit-labs/ssr/lib/render-lit-html.js ->
// ../../../../../node_modules/@lit-labs/ssr/lib/lit-element-renderer.js -> ../../../../../node_modules/@lit-labs/ssr/lib/render-lit-html.js\n" was thrown, throw an Error :)
// https://github.com/lit/lit/issues/449
// https://github.com/ProjectEvergreen/greenwood/issues/1118
break;
default:
// otherwise, log all warnings from rollup
console.debug(message);
return {
input: filepath,
output: {
dir: normalizePathnameForWindows(outputDir),
entryFileNames: `${entryName}.route.js`,
chunkFileNames: `${entryName}.route.chunk.[hash].js`
},
plugins: [
greenwoodResourceLoader(compilation),
// support node export conditions for SSR pages
// https://github.com/ProjectEvergreen/greenwood/issues/1118
// https://github.com/rollup/plugins/issues/362#issuecomment-873448461
nodeResolve({
exportConditions: ['node'],
preferBuiltins: true
}),
commonjs(),
greenwoodImportMetaUrl(compilation),
greenwoodSyncSsrEntryPointsOutputPaths(compilation)
],
onwarn: (errorObj) => {
const { code, message } = errorObj;

switch (code) {

case 'CIRCULAR_DEPENDENCY':
// TODO let this through for lit by suppressing it

Check warning on line 486 in packages/cli/src/config/rollup.config.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO let this through for lit by...'

Check warning on line 486 in packages/cli/src/config/rollup.config.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO let this through for lit by...'

Check warning on line 486 in packages/cli/src/config/rollup.config.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO let this through for lit by...'

Check warning on line 486 in packages/cli/src/config/rollup.config.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO let this through for lit by...'

Check warning on line 486 in packages/cli/src/config/rollup.config.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO let this through for lit by...'

Check warning on line 486 in packages/cli/src/config/rollup.config.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO let this through for lit by...'
// Error: the string "Circular dependency: ../../../../../node_modules/@lit-labs/ssr/lib/render-lit-html.js ->
// ../../../../../node_modules/@lit-labs/ssr/lib/lit-element-renderer.js -> ../../../../../node_modules/@lit-labs/ssr/lib/render-lit-html.js\n" was thrown, throw an Error :)
// https://github.com/lit/lit/issues/449
// https://github.com/ProjectEvergreen/greenwood/issues/1118
break;
default:
// otherwise, log all warnings from rollup
console.debug(message);

}
}
}
}));
};
});
};

export {
Expand Down
12 changes: 7 additions & 5 deletions packages/cli/src/lifecycles/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,18 @@ async function bundleSsrPages(compilation) {

for (const page of compilation.graph) {
if (page.isSSR && !page.prerender) {
const { imports, route, template, title, relativeWorkspacePagePath } = page;
const entryFileUrl = new URL(`./_${relativeWorkspacePagePath.replace('/', '')}`, scratchDir);
const moduleUrl = new URL(`./${relativeWorkspacePagePath.replace('/', '')}`, pagesDir);
const { filename, imports, route, template, title, relativeWorkspacePagePath } = page;
const entryFileUrl = new URL(`.${relativeWorkspacePagePath}`, scratchDir);
const moduleUrl = new URL(`.${relativeWorkspacePagePath}`, pagesDir);
const outputPathRootUrl = new URL(`file://${path.dirname(entryFileUrl.pathname)}`);
const request = new Request(moduleUrl); // TODO not really sure how to best no-op this?
// TODO getTemplate has to be static (for now?)
// https://github.com/ProjectEvergreen/greenwood/issues/955
const data = await executeRouteModule({ moduleUrl, compilation, page, prerender: false, htmlContents: null, scripts: [], request });
const pagesPathDiff = compilation.context.pagesDir.pathname.replace(compilation.context.projectDirectory.pathname, '');

const relativeDepth = relativeWorkspacePagePath.replace(`/${filename}`, '') === ''
? '../'
: '../'.repeat(relativeWorkspacePagePath.replace(`/${filename}`, '').split('/').length);
let staticHtml = '';

staticHtml = data.template ? data.template : await getPageTemplate(staticHtml, compilation.context, template, []);
Expand All @@ -266,7 +268,7 @@ async function bundleSsrPages(compilation) {
await fs.writeFile(entryFileUrl, `
import { executeRouteModule } from '${normalizePathnameForWindows(executeModuleUrl)}';
const moduleUrl = new URL('../${pagesPathDiff}${relativeWorkspacePagePath}', import.meta.url);
const moduleUrl = new URL('${relativeDepth}${pagesPathDiff}${relativeWorkspacePagePath.replace('/', '')}', import.meta.url);
export async function handler(request) {
const compilation = JSON.parse('${JSON.stringify(compilation)}');
Expand Down

0 comments on commit 1a21f68

Please sign in to comment.