diff --git a/greenwood.config.js b/greenwood.config.js index e913c0891..332980001 100644 --- a/greenwood.config.js +++ b/greenwood.config.js @@ -15,11 +15,11 @@ export default { staticRouter: true, interpolateFrontmatter: true, plugins: [ - ...greenwoodPluginGraphQL(), - ...greenwoodPluginPolyfills(), + greenwoodPluginGraphQL(), + greenwoodPluginPolyfills(), greenwoodPluginPostCss(), - ...greenwoodPluginImportJson(), - ...greenwoodPluginImportCss(), + greenwoodPluginImportJson(), + greenwoodPluginImportCss(), { type: 'rollup', name: 'rollup-plugin-analyzer', @@ -34,8 +34,8 @@ export default { ]; } }, - ...greenwoodPluginIncludeHTML(), - ...greenwoodPluginRendererPuppeteer() + greenwoodPluginIncludeHTML(), + greenwoodPluginRendererPuppeteer() ], markdown: { plugins: [ diff --git a/packages/cli/src/commands/build.js b/packages/cli/src/commands/build.js index 27f78eaf5..e5ce43348 100644 --- a/packages/cli/src/commands/build.js +++ b/packages/cli/src/commands/build.js @@ -11,10 +11,7 @@ const runProductionBuild = async (compilation) => { try { const { prerender } = compilation.config; const outputDir = compilation.context.outputDir; - const defaultPrerender = (compilation.config.plugins.filter(plugin => plugin.type === 'renderer' && plugin.isGreenwoodDefaultPlugin) || []).length === 1 - ? compilation.config.plugins.filter(plugin => plugin.type === 'renderer')[0].provider(compilation) - : {}; - const customPrerender = (compilation.config.plugins.filter(plugin => plugin.type === 'renderer' && !plugin.isGreenwoodDefaultPlugin) || []).length === 1 + const prerenderPlugin = (compilation.config.plugins.filter(plugin => plugin.type === 'renderer') || []).length === 1 ? compilation.config.plugins.filter(plugin => plugin.type === 'renderer')[0].provider(compilation) : {}; @@ -22,7 +19,7 @@ const runProductionBuild = async (compilation) => { fs.mkdirSync(outputDir); } - if (prerender || customPrerender.prerender) { + if (prerender || prerenderPlugin.prerender) { // start any servers if needed const servers = [...compilation.config.plugins.filter((plugin) => { return plugin.type === 'server'; @@ -42,14 +39,10 @@ const runProductionBuild = async (compilation) => { return Promise.resolve(server); })); - if (customPrerender.workerUrl) { - await preRenderCompilationWorker(compilation, customPrerender); - } else if (customPrerender.customUrl) { - await preRenderCompilationCustom(compilation, customPrerender); - } else if (defaultPrerender && prerender) { - await preRenderCompilationWorker(compilation, defaultPrerender); + if (prerenderPlugin.workerUrl) { + await preRenderCompilationWorker(compilation, prerenderPlugin); } else { - reject('This is an unhandled pre-rendering case! Please report.'); + await preRenderCompilationCustom(compilation, prerenderPlugin); } } else { await staticRenderCompilation(compilation); diff --git a/packages/cli/src/lifecycles/config.js b/packages/cli/src/lifecycles/config.js index 53ccbb06b..1e4fbdaf8 100644 --- a/packages/cli/src/lifecycles/config.js +++ b/packages/cli/src/lifecycles/config.js @@ -5,6 +5,7 @@ import { fileURLToPath, pathToFileURL, URL } from 'url'; // get and "tag" all plugins provided / maintained by the @greenwood/cli // and include as the default set, with all user plugins getting appended const greenwoodPluginsBasePath = fileURLToPath(new URL('../plugins', import.meta.url)); +const PLUGINS_FLATTENED_DEPTH = 2; const greenwoodPlugins = (await Promise.all([ path.join(greenwoodPluginsBasePath, 'copy'), @@ -14,7 +15,7 @@ const greenwoodPlugins = (await Promise.all([ ].map(async (pluginDirectory) => { const files = await fs.promises.readdir(pluginDirectory); - return (await Promise.all(files.map(async(file) => { + return await Promise.all(files.map(async(file) => { const importPaTh = pathToFileURL(`${pluginDirectory}${path.sep}${file}`); const pluginImport = await import(importPaTh); const plugin = pluginImport[Object.keys(pluginImport)[0]]; @@ -22,8 +23,8 @@ const greenwoodPlugins = (await Promise.all([ return Array.isArray(plugin) ? plugin : [plugin]; - }))).flat(); -}).flat())).flat().map((plugin) => { + })); +}))).flat(PLUGINS_FLATTENED_DEPTH).map((plugin) => { return { isGreenwoodDefaultPlugin: true, ...plugin @@ -99,7 +100,9 @@ const readAndMergeConfig = async() => { } if (plugins && plugins.length > 0) { - plugins.forEach(plugin => { + const flattened = plugins.flat(PLUGINS_FLATTENED_DEPTH); + + flattened.forEach(plugin => { if (!plugin.type || pluginTypes.indexOf(plugin.type) < 0) { reject(`Error: greenwood.config.js plugins must be one of type "${pluginTypes.join(', ')}". got "${plugin.type}" instead.`); } @@ -117,14 +120,22 @@ const readAndMergeConfig = async() => { } }); - // if user provides a custom renderer, replace ours with theirs - if (plugins.filter(plugin => plugin.type === 'renderer').length === 1) { + // if user provided a custom renderer, filter out Greenwood's default renderer + const customRendererPlugins = flattened.filter(plugin => plugin.type === 'renderer').length; + + if (customRendererPlugins === 1) { customConfig.plugins = customConfig.plugins.filter((plugin) => { return plugin.type !== 'renderer'; }); + } else if (customRendererPlugins > 1) { + console.warn('More than one custom renderer plugin detected. Please make sure you are only loading one.'); + console.debug(plugins.filter(plugin => plugin.type === 'renderer')); } - customConfig.plugins = customConfig.plugins.concat(plugins); + customConfig.plugins = [ + ...customConfig.plugins, + ...flattened + ]; } if (devServer && Object.keys(devServer).length > 0) { diff --git a/packages/plugin-babel/README.md b/packages/plugin-babel/README.md index d032c4e66..eaa2e8946 100644 --- a/packages/plugin-babel/README.md +++ b/packages/plugin-babel/README.md @@ -27,7 +27,7 @@ export default { ... plugins: [ - ...greenwoodPluginBabel() // notice the spread ... ! + greenwoodPluginBabel() ] } ``` @@ -68,8 +68,7 @@ If you would like to use it, either standalone or with your own custom _babel.co ... plugins: [ - // notice the spread ... ! - ...greenwoodPluginBabel({ + greenwoodPluginBabel({ extendConfig: true }) ] diff --git a/packages/plugin-graphql/README.md b/packages/plugin-graphql/README.md index 140a39597..56e2fbe93 100644 --- a/packages/plugin-graphql/README.md +++ b/packages/plugin-graphql/README.md @@ -42,8 +42,8 @@ export default { ... plugins: [ - ...greenwoodPluginGraphQL(), // notice the spread ... ! - ...greenwoodPluginRendererPuppeteer() // notice the spread ... ! + greenwoodPluginGraphQL(), + greenwoodPluginRendererPuppeteer() ] } ``` diff --git a/packages/plugin-import-commonjs/README.md b/packages/plugin-import-commonjs/README.md index e1020e127..216f7c83c 100644 --- a/packages/plugin-import-commonjs/README.md +++ b/packages/plugin-import-commonjs/README.md @@ -27,7 +27,7 @@ export default { ... plugins: [ - ...greenwoodPluginImportCommonJs() // notice the spread ... ! + greenwoodPluginImportCommonJs() ] } ``` diff --git a/packages/plugin-import-css/README.md b/packages/plugin-import-css/README.md index 4f973933c..7a9d8737d 100644 --- a/packages/plugin-import-css/README.md +++ b/packages/plugin-import-css/README.md @@ -31,7 +31,7 @@ export default { ... plugins: [ - ...greenwoodPluginImportCss() // notice the spread ... ! + greenwoodPluginImportCss() ] } ``` diff --git a/packages/plugin-import-json/README.md b/packages/plugin-import-json/README.md index c12064cc7..744630209 100644 --- a/packages/plugin-import-json/README.md +++ b/packages/plugin-import-json/README.md @@ -31,7 +31,7 @@ export default { ... plugins: [ - ...greenwoodPluginImportJson() // notice the spread ... ! + greenwoodPluginImportJson() ] } ``` diff --git a/packages/plugin-include-html/README.md b/packages/plugin-include-html/README.md index 699714505..424fdd5af 100644 --- a/packages/plugin-include-html/README.md +++ b/packages/plugin-include-html/README.md @@ -15,7 +15,7 @@ export default { ... plugins: [ - ...greenwoodPluginIncludeHtml() // notice the spread ... ! + greenwoodPluginIncludeHtml() ] } ``` diff --git a/packages/plugin-polyfills/README.md b/packages/plugin-polyfills/README.md index 9b9ccd9f3..89b79527a 100644 --- a/packages/plugin-polyfills/README.md +++ b/packages/plugin-polyfills/README.md @@ -35,7 +35,7 @@ export default { ... plugins: [ - ...greenwoodPluginPolyfills() // notice the spread ... ! + greenwoodPluginPolyfills() ] } ``` diff --git a/packages/plugin-renderer-puppeteer/README.md b/packages/plugin-renderer-puppeteer/README.md index c859628a8..6b28e7645 100644 --- a/packages/plugin-renderer-puppeteer/README.md +++ b/packages/plugin-renderer-puppeteer/README.md @@ -75,7 +75,7 @@ export default { ... plugins: [ - ...greenwoodPluginRendererPuppeteer() // notice the spread! + greenwoodPluginRendererPuppeteer() ] } ``` diff --git a/packages/plugin-typescript/README.md b/packages/plugin-typescript/README.md index 301b95693..e0c33d6a2 100644 --- a/packages/plugin-typescript/README.md +++ b/packages/plugin-typescript/README.md @@ -31,7 +31,7 @@ export default { ... plugins: [ - ...greenwoodPluginTypeScript() // notice the spread ... ! + greenwoodPluginTypeScript() ] } ``` @@ -90,8 +90,7 @@ If you would like to extend / override these options: ... plugins: [ - // notice the spread ... ! - ...greenwoodPluginTypeScript({ + greenwoodPluginTypeScript({ extendConfig: true }) ] diff --git a/www/pages/blog/release/v0-26-0.md b/www/pages/blog/release/v0-26-0.md index bd6ec3480..3f3c26aec 100644 --- a/www/pages/blog/release/v0-26-0.md +++ b/www/pages/blog/release/v0-26-0.md @@ -187,7 +187,7 @@ So although WCC is now the default for SSR, Puppeteer is still available as a pl export default { plugins: [ - ...greenwoodPluginRendererPuppeteer() + greenwoodPluginRendererPuppeteer() ] } ```