Skip to content

Commit

Permalink
window support refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed May 3, 2024
1 parent 6779ca9 commit 61c4c3e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 6 additions & 2 deletions packages/cli/src/config/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,16 +278,20 @@ function greenwoodPatchSsrPagesEntryPointRuntimeImport(compilation) {
return {
name: 'greenwood-patch-ssr-pages-entry-point-runtime-import',
generateBundle(options, bundle) {
const { pagesDir, scratchDir } = compilation.context;

Object.keys(bundle).forEach((key) => {
// map rollup bundle names back to original SSR pages for output bundles and paths
if (key.startsWith('_')) {
const needle = bundle[key].code.match(/___GWD_ENTRY_FILE_URL=(.*.)___/);

if (bundle[key].facadeModuleId.startsWith(compilation.context.scratchDir.pathname) && needle) {
// handle windows shenanigans for facadeModuleId and path separators
if (new URL(`file://${bundle[key].facadeModuleId}`).pathname.startsWith(scratchDir.pathname) && needle) {
const entryPathMatch = needle[1];

Object.keys(bundle).forEach((_) => {
if (bundle[_].facadeModuleId === `${compilation.context.pagesDir.pathname}${entryPathMatch}`) {
// handle windows shenanigans for facadeModuleId and path separators
if (new URL(`file://${bundle[_].facadeModuleId}`).pathname === `${pagesDir.pathname}${entryPathMatch}`) {
bundle[key].code = bundle[key].code.replace(/'___GWD_ENTRY_FILE_URL=(.*.)___'/, `new URL('./${bundle[_].fileName}', import.meta.url)`);

compilation.graph.forEach((page, idx) => {
Expand Down
10 changes: 8 additions & 2 deletions packages/cli/src/lifecycles/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,10 @@ async function bundleSsrPages(compilation) {

for (const page of compilation.graph) {
if (page.isSSR && !page.prerender) {
const { filename, imports, route, template, title, relativeWorkspacePagePath } = page;
const { imports, route, template, title, relativeWorkspacePagePath } = page;
const entryFileUrl = new URL(`./_${relativeWorkspacePagePath.replace('/', '')}`, scratchDir);
const moduleUrl = new URL(`./${relativeWorkspacePagePath.replace('/', '')}`, 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
Expand All @@ -205,8 +206,13 @@ async function bundleSsrPages(compilation) {
staticHtml = await (await htmlOptimizer.optimize(new URL(`http://localhost:8080${route}`), new Response(staticHtml))).text();
staticHtml = staticHtml.replace(/[`\\$]/g, '\\$&'); // https://stackoverflow.com/a/75688937/417806

if (!await checkResourceExists(outputPathRootUrl)) {
await fs.mkdir(outputPathRootUrl, {
recursive: true
});
}

// better way to write out this inline code?
await fs.mkdir(entryFileUrl.pathname.replace(filename, ''), { recursive: true });
await fs.writeFile(entryFileUrl, `
import { executeRouteModule } from '${normalizePathnameForWindows(executeModuleUrl)}';
Expand Down

0 comments on commit 61c4c3e

Please sign in to comment.