Skip to content

Commit

Permalink
Feature/issue 856 hybrid workspace with static router (#874)
Browse files Browse the repository at this point in the history
* refactor SSG and SSR modes

* refactor out mode completely and introduce staticRouter

* hybrid documentation updates and remove mode configuration docs

* rename specs
  • Loading branch information
thescientist13 authored Feb 5, 2022
1 parent b095c2a commit 907a0a6
Show file tree
Hide file tree
Showing 51 changed files with 250 additions and 363 deletions.
2 changes: 1 addition & 1 deletion greenwood.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const FAVICON_HREF = '/favicon.ico';

export default {
workspace: fileURLToPath(new URL('./www', import.meta.url)),
mode: 'mpa',
optimization: 'inline',
staticRouter: true,
title: 'Greenwood',
meta: [
{ name: 'description', content: META_DESCRIPTION },
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/commands/serve.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const runProdServer = async () => {
try {
const compilation = await generateCompilation();
const port = 8080;
const server = compilation.config.mode === 'ssr' ? getHybridServer : getStaticServer;
const hasRoutes = compilation.graph.find(page => page.isSSR);
const server = hasRoutes ? getHybridServer : getStaticServer;

(await server(compilation)).listen(port, () => {
console.info(`Started server at localhost:${port}`);
Expand Down
47 changes: 23 additions & 24 deletions packages/cli/src/lifecycles/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const greenwoodPlugins = (await Promise.all([
};
});

const modes = ['ssg', 'mpa', 'spa', 'ssr'];
const optimizations = ['default', 'none', 'static', 'inline'];
const pluginTypes = ['copy', 'context', 'resource', 'rollup', 'server', 'source', 'renderer'];
const defaultConfig = {
Expand All @@ -40,16 +39,14 @@ const defaultConfig = {
port: 1984,
extensions: []
},
mode: modes[0],
optimization: optimizations[0],
title: 'My App',
meta: [],
plugins: greenwoodPlugins,
markdown: { plugins: [], settings: {} },
prerender: true,
pagesDirectory: 'pages',
templatesDirectory: 'templates',
routesDirectory: 'routes'
templatesDirectory: 'templates'
};

const readAndMergeConfig = async() => {
Expand All @@ -61,7 +58,7 @@ const readAndMergeConfig = async() => {

if (fs.existsSync(path.join(process.cwd(), 'greenwood.config.js'))) {
const userCfgFile = (await import(pathToFileURL(path.join(process.cwd(), 'greenwood.config.js')))).default;
const { workspace, devServer, title, markdown, meta, mode, optimization, plugins, prerender, pagesDirectory, templatesDirectory } = userCfgFile;
const { workspace, devServer, title, markdown, meta, optimization, plugins, prerender, staticRouter, pagesDirectory, templatesDirectory } = userCfgFile;

// workspace validation
if (workspace) {
Expand Down Expand Up @@ -99,12 +96,6 @@ const readAndMergeConfig = async() => {
customConfig.meta = meta;
}

if (typeof mode === 'string' && modes.indexOf(mode.toLowerCase()) >= 0) {
customConfig.mode = mode;
} else if (mode) {
reject(`Error: provided mode "${mode}" is not supported. Please use one of: ${modes.join(', ')}.`);
}

if (typeof optimization === 'string' && optimizations.indexOf(optimization.toLowerCase()) >= 0) {
customConfig.optimization = optimization;
} else if (optimization) {
Expand Down Expand Up @@ -177,19 +168,6 @@ const readAndMergeConfig = async() => {
customConfig.markdown.settings = markdown.settings ? markdown.settings : {};
}

if (prerender !== undefined) {
if (typeof prerender === 'boolean') {
customConfig.prerender = prerender;
} else {
reject(`Error: greenwood.config.js prerender must be a boolean; true or false. Passed value was typeof: ${typeof prerender}`);
}
}

// SPA should _not_ prerender if user has specified prerender should be true
if (prerender === undefined && mode === 'spa') {
customConfig.prerender = false;
}

if (pagesDirectory && typeof pagesDirectory === 'string') {
customConfig.pagesDirectory = pagesDirectory;
} else if (pagesDirectory) {
Expand All @@ -201,6 +179,27 @@ const readAndMergeConfig = async() => {
} else if (templatesDirectory) {
reject(`Error: provided templatesDirectory "${templatesDirectory}" is not supported. Please make sure to pass something like 'layouts/'`);
}

if (prerender !== undefined) {
if (typeof prerender === 'boolean') {
customConfig.prerender = prerender;
} else {
reject(`Error: greenwood.config.js prerender must be a boolean; true or false. Passed value was typeof: ${typeof prerender}`);
}
}

// SPA should _not_ prerender unless if user has specified prerender should be true
if (prerender === undefined && fs.existsSync(path.join(customConfig.workspace, 'index.html'))) {
customConfig.prerender = false;
}

if (staticRouter !== undefined) {
if (typeof staticRouter === 'boolean') {
customConfig.staticRouter = staticRouter;
} else {
reject(`Error: greenwood.config.js staticRouter must be a boolean; true or false. Passed value was typeof: ${typeof staticRouter}`);
}
}
}

resolve({ ...defaultConfig, ...customConfig });
Expand Down
4 changes: 1 addition & 3 deletions packages/cli/src/lifecycles/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const initContext = async({ config }) => {
const userWorkspace = path.join(config.workspace);
const pagesDir = path.join(userWorkspace, `${config.pagesDirectory}/`);
const userTemplatesDir = path.join(userWorkspace, `${config.templatesDirectory}/`);
const routesDir = path.join(userWorkspace, `${config.routesDirectory}/`);

const context = {
dataDir,
Expand All @@ -22,8 +21,7 @@ const initContext = async({ config }) => {
pagesDir,
userTemplatesDir,
scratchDir,
projectDirectory,
routesDir
projectDirectory
};

if (!fs.existsSync(scratchDir)) {
Expand Down
Loading

0 comments on commit 907a0a6

Please sign in to comment.