Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug/issue 1223 nested SSR pages release branch rebase and refactor #1236

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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 @@ -290,7 +315,7 @@
}
}
} else {
// TODO figure out how to handle URL chunk from SSR pages

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

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO figure out how to handle URL chunk...'

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

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO figure out how to handle URL chunk...'

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

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO figure out how to handle URL chunk...'

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

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO figure out how to handle URL chunk...'
// https://github.com/ProjectEvergreen/greenwood/issues/1163
}

Expand Down Expand Up @@ -427,44 +452,51 @@
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...'
// 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
Loading