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

rfc/issue 955 layouts and pages #1212

Merged
merged 24 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e8c2daf
feature/discussion 1117 Isolation Mode (v1) (#1206)
thescientist13 Mar 10, 2024
a7b464f
enhancement/Issue-1118: Single File Bundles for SSR and API routes (#…
DevLab2425 Mar 10, 2024
0a863c1
v0.30.0-alpha.1
thescientist13 Mar 16, 2024
ae44511
feature/issue 923 native import attributes for CSS and JSON (#1215)
thescientist13 Apr 29, 2024
8b9ed0c
first pass on resource tracking and bundling refactor with lit polyfi…
thescientist13 Dec 26, 2023
2a69b1e
interim work around to solve double rendering and undefined WCC bugs
thescientist13 Dec 27, 2023
cc92114
refactor frontmatter for graph and standard html plugin for SSR
thescientist13 Dec 27, 2023
50ce060
rename templates directory to layouts
thescientist13 Dec 29, 2023
9c04d37
refactor over bundling of static script assets in bundleSsrPages
thescientist13 Dec 31, 2023
c1f7a5d
handle bundling styles within bundleSsrPages
thescientist13 Dec 31, 2023
116e650
post rebase tweaks
thescientist13 Mar 16, 2024
5600d70
custom elements as layouts
thescientist13 May 19, 2024
712f60f
post rebase tweaks
thescientist13 May 19, 2024
a316173
WCC patched support for TS pages
thescientist13 Jun 15, 2024
123ce94
support and tests for API routes as a custom dynamic format
thescientist13 Jun 15, 2024
dd7b9a2
restore TS tests and make servePage default
thescientist13 Jun 16, 2024
84b4bfd
document custom page format support
thescientist13 Jun 16, 2024
335900f
fix lint
thescientist13 Jun 16, 2024
4ae525d
patch latest WCC TypeScript changes
thescientist13 Jun 18, 2024
de67e52
cleanup default app layout content
thescientist13 Jun 20, 2024
1be3fe1
handle rollup circular dependency warnings for API routes
thescientist13 Jun 20, 2024
dc05943
rename test cases from templates to layout
thescientist13 Jun 21, 2024
b997e7d
collapse API routes directory into pages directory
thescientist13 Jun 22, 2024
1b8bbd5
bump to wc-compiler 0.14.0
thescientist13 Jun 22, 2024
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
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ The [layout](https://github.com/ProjectEvergreen/greenwood/tree/master/packages/
- _lib/_ - Custom utility and client facing files
- _lifecycles/_ - Tasks that can be composed by commands to support the full needs of that command
- _plugins/_ - Custom default plugins maintained by the CLI project
- _templates/_ - Default templates and / or pages provided by Greenwood.
- _layouts/_ - Default layouts and / or pages provided by Greenwood.


#### Lifecycles
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"remark-rehype": "^7.0.0",
"rollup": "^3.29.4",
"unified": "^9.2.0",
"wc-compiler": "~0.13.0"
"wc-compiler": "~0.14.0"
},
"devDependencies": {
"@babel/runtime": "^7.10.4",
Expand Down
64 changes: 1 addition & 63 deletions packages/cli/src/commands/build.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,10 @@
import { bundleCompilation } from '../lifecycles/bundle.js';
import { checkResourceExists, trackResourcesForRoute } from '../lib/resource-utils.js';
import { checkResourceExists } from '../lib/resource-utils.js';
import { copyAssets } from '../lifecycles/copy.js';
import fs from 'fs/promises';
import { preRenderCompilationWorker, preRenderCompilationCustom, staticRenderCompilation } from '../lifecycles/prerender.js';
import { ServerInterface } from '../lib/server-interface.js';

// TODO a lot of these are duplicated in the prerender lifecycle too
// would be good to refactor
async function servePage(url, request, plugins) {
let response = new Response('');

for (const plugin of plugins) {
if (plugin.shouldServe && await plugin.shouldServe(url, request)) {
response = await plugin.serve(url, request);
break;
}
}

return response;
}

async function interceptPage(url, request, plugins, body) {
let response = new Response(body, {
headers: new Headers({ 'Content-Type': 'text/html' })
});

for (const plugin of plugins) {
if (plugin.shouldPreIntercept && await plugin.shouldPreIntercept(url, request, response)) {
response = await plugin.preIntercept(url, request, response);
}

if (plugin.shouldIntercept && await plugin.shouldIntercept(url, request, response)) {
response = await plugin.intercept(url, request, response);
}
}

return response;
}

function getPluginInstances (compilation) {
return [...compilation.config.plugins]
.filter(plugin => plugin.type === 'resource' && plugin.name !== 'plugin-node-modules:resource')
.map((plugin) => {
return plugin.provider(compilation);
});
}

// TODO does this make more sense in bundle lifecycle?
// https://github.com/ProjectEvergreen/greenwood/issues/970
// or could this be done sooner (like in appTemplate building in html resource plugin)?
// Or do we need to ensure userland code / plugins have gone first
async function trackResourcesForRoutes(compilation) {
const plugins = getPluginInstances(compilation);

for (const page of compilation.graph) {
const { route } = page;
const url = new URL(`http://localhost:${compilation.config.port}${route}`);
const request = new Request(url);

let body = await (await servePage(url, request, plugins)).text();
body = await (await interceptPage(url, request, plugins, body)).text();

await trackResourcesForRoute(body, compilation, route);
}
}

const runProductionBuild = async (compilation) => {

return new Promise(async (resolve, reject) => {
Expand Down Expand Up @@ -106,13 +46,11 @@ const runProductionBuild = async (compilation) => {
}));

if (prerenderPlugin.executeModuleUrl) {
await trackResourcesForRoutes(compilation);
await preRenderCompilationWorker(compilation, prerenderPlugin);
} else {
await preRenderCompilationCustom(compilation, prerenderPlugin);
}
} else {
await trackResourcesForRoutes(compilation);
await staticRenderCompilation(compilation);
}

Expand Down
31 changes: 26 additions & 5 deletions packages/cli/src/config/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,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 @@ -423,10 +423,10 @@
};

const getRollupConfigForApis = async (compilation) => {
const { outputDir, userWorkspace } = compilation.context;
const { outputDir, pagesDir } = compilation.context;

return [...compilation.manifest.apis.values()]
.map(api => normalizePathnameForWindows(new URL(`.${api.path}`, userWorkspace)))
.map(api => normalizePathnameForWindows(new URL(`.${api.path}`, pagesDir)))
.map(filepath => ({
input: filepath,
output: {
Expand All @@ -445,7 +445,27 @@
}),
commonjs(),
greenwoodImportMetaUrl(compilation)
]
],
onwarn: (errorObj) => {
const { code, message } = errorObj;

switch (code) {

case 'CIRCULAR_DEPENDENCY':
// let this through for WCC + sucrase
// Circular dependency: ../../../../../node_modules/sucrase/dist/esm/parser/tokenizer/index.js ->
// ../../../../../node_modules/sucrase/dist/esm/parser/traverser/util.js -> ../../../../../node_modules/sucrase/dist/esm/parser/tokenizer/index.js
// Circular dependency: ../../../../../node_modules/sucrase/dist/esm/parser/tokenizer/index.js ->
// ../../../../../node_modules/sucrase/dist/esm/parser/tokenizer/readWord.js -> ../../../../../node_modules/sucrase/dist/esm/parser/tokenizer/index.js
// https://github.com/ProjectEvergreen/greenwood/pull/1212
// https://github.com/lit/lit/issues/449#issuecomment-416688319
break;
default:
// otherwise, log all warnings from rollup
console.debug(message);

}
}
}));
};

Expand Down Expand Up @@ -483,11 +503,12 @@
switch (code) {

case 'CIRCULAR_DEPENDENCY':
// TODO let this through for lit by suppressing it
// let this through for lit
// 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
// https://github.com/lit/lit/issues/449#issuecomment-416688319
// https://github.com/rollup/rollup/issues/1089#issuecomment-402109607
break;
default:
// otherwise, log all warnings from rollup
Expand Down
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions packages/cli/src/lib/execute-route-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

async function executeRouteModule({ moduleUrl, compilation, page = {}, prerender = false, htmlContents = null, scripts = [], request }) {
const data = {
template: null,
layout: null,
body: null,
frontmatter: null,
html: null
Expand All @@ -15,7 +15,7 @@
data.html = html;
} else {
const module = await import(moduleUrl).then(module => module);
const { prerender = false, getTemplate = null, getBody = null, getFrontmatter = null, isolation } = module;
const { prerender = false, getLayout = null, getBody = null, getFrontmatter = null, isolation } = module;

if (module.default) {
const { html } = await renderToString(new URL(moduleUrl), false, request);
Expand All @@ -27,15 +27,15 @@
}
}

if (getTemplate) {
data.template = await getTemplate(compilation, page);
if (getLayout) {
data.layout = await getLayout(compilation, page);
}

if (getFrontmatter) {
data.frontmatter = await getFrontmatter(compilation, page);
}

// TODO cant we get these from just pulling from the file during the graph phase?

Check warning on line 38 in packages/cli/src/lib/execute-route-module.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO cant we get these from just pulling...'

Check warning on line 38 in packages/cli/src/lib/execute-route-module.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO cant we get these from just pulling...'

Check warning on line 38 in packages/cli/src/lib/execute-route-module.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected 'todo' comment: 'TODO cant we get these from just pulling...'

Check warning on line 38 in packages/cli/src/lib/execute-route-module.js

View workflow job for this annotation

GitHub Actions / build (18)

Unexpected ' TODO' comment: 'TODO cant we get these from just pulling...'
// https://github.com/ProjectEvergreen/greenwood/issues/991
data.prerender = prerender;
data.isolation = isolation;
Expand Down
Loading
Loading