Skip to content

Commit

Permalink
Enhancement/issue 925 auto spread arrays of plugins (#969)
Browse files Browse the repository at this point in the history
* auto spread arrays of plugins

* flatten refactoring

* update docs and website content
  • Loading branch information
thescientist13 committed Nov 12, 2022
1 parent 3d0ae4b commit dc4b53b
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 40 deletions.
12 changes: 6 additions & 6 deletions greenwood.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -34,8 +34,8 @@ export default {
];
}
},
...greenwoodPluginIncludeHTML(),
...greenwoodPluginRendererPuppeteer()
greenwoodPluginIncludeHTML(),
greenwoodPluginRendererPuppeteer()
],
markdown: {
plugins: [
Expand Down
17 changes: 5 additions & 12 deletions packages/cli/src/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,15 @@ 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)
: {};

if (!fs.existsSync(outputDir)) {
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';
Expand All @@ -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);
Expand Down
25 changes: 18 additions & 7 deletions packages/cli/src/lifecycles/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -14,16 +15,16 @@ 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]];

return Array.isArray(plugin)
? plugin
: [plugin];
}))).flat();
}).flat())).flat().map((plugin) => {
}));
}))).flat(PLUGINS_FLATTENED_DEPTH).map((plugin) => {
return {
isGreenwoodDefaultPlugin: true,
...plugin
Expand Down Expand Up @@ -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.`);
}
Expand All @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions packages/plugin-babel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default {
...

plugins: [
...greenwoodPluginBabel() // notice the spread ... !
greenwoodPluginBabel()
]
}
```
Expand Down Expand Up @@ -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
})
]
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export default {
...

plugins: [
...greenwoodPluginGraphQL(), // notice the spread ... !
...greenwoodPluginRendererPuppeteer() // notice the spread ... !
greenwoodPluginGraphQL(),
greenwoodPluginRendererPuppeteer()
]
}
```
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-import-commonjs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default {
...

plugins: [
...greenwoodPluginImportCommonJs() // notice the spread ... !
greenwoodPluginImportCommonJs()
]
}
```
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-import-css/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default {
...

plugins: [
...greenwoodPluginImportCss() // notice the spread ... !
greenwoodPluginImportCss()
]
}
```
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-import-json/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default {
...

plugins: [
...greenwoodPluginImportJson() // notice the spread ... !
greenwoodPluginImportJson()
]
}
```
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-include-html/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default {
...

plugins: [
...greenwoodPluginIncludeHtml() // notice the spread ... !
greenwoodPluginIncludeHtml()
]
}
```
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-polyfills/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default {
...

plugins: [
...greenwoodPluginPolyfills() // notice the spread ... !
greenwoodPluginPolyfills()
]
}
```
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-renderer-puppeteer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default {
...

plugins: [
...greenwoodPluginRendererPuppeteer() // notice the spread!
greenwoodPluginRendererPuppeteer()
]
}
```
Expand Down
5 changes: 2 additions & 3 deletions packages/plugin-typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default {
...

plugins: [
...greenwoodPluginTypeScript() // notice the spread ... !
greenwoodPluginTypeScript()
]
}
```
Expand Down Expand Up @@ -90,8 +90,7 @@ If you would like to extend / override these options:
...

plugins: [
// notice the spread ... !
...greenwoodPluginTypeScript({
greenwoodPluginTypeScript({
extendConfig: true
})
]
Expand Down
2 changes: 1 addition & 1 deletion www/pages/blog/release/v0-26-0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
]
}
```
Expand Down

0 comments on commit dc4b53b

Please sign in to comment.