Skip to content

Commit

Permalink
auto spread arrays of plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
thescientist13 committed Aug 2, 2022
1 parent de40548 commit 0a1283b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
10 changes: 5 additions & 5 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,7 +34,7 @@ export default {
];
}
},
...greenwoodPluginIncludeHTML(),
greenwoodPluginIncludeHTML(),
...greenwoodPluginRendererPuppeteer()
],
markdown: {
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
39 changes: 23 additions & 16 deletions packages/cli/src/lifecycles/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,31 +100,38 @@ const readAndMergeConfig = async() => {

if (plugins && plugins.length > 0) {
plugins.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.`);
}
const flattened = (Array.isArray(plugin) ? plugin : [plugin]).flat();

if (!plugin.provider || typeof plugin.provider !== 'function') {
const providerTypeof = typeof plugin.provider;
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.`);
}

reject(`Error: greenwood.config.js plugins provider must be a function. got ${providerTypeof} instead.`);
}
if (!plugin.provider || typeof plugin.provider !== 'function') {
const providerTypeof = typeof plugin.provider;

if (!plugin.name || typeof plugin.name !== 'string') {
const nameTypeof = typeof plugin.name;
reject(`Error: greenwood.config.js plugins provider must be a function. got ${providerTypeof} instead.`);
}

reject(`Error: greenwood.config.js plugins must have a name. got ${nameTypeof} instead.`);
}
if (!plugin.name || typeof plugin.name !== 'string') {
const nameTypeof = typeof plugin.name;

reject(`Error: greenwood.config.js plugins must have a name. got ${nameTypeof} instead.`);
}
});

customConfig.plugins = [
...customConfig.plugins,
...flattened
];
});

// 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
if (customConfig.plugins.filter(plugin => plugin.type === 'renderer').length > 1) {
customConfig.plugins = customConfig.plugins.filter((plugin) => {
return plugin.type !== 'renderer';
return plugin.type !== 'renderer' || plugin.type === 'renderer' && !plugin.isGreenwoodDefaultPlugin;
});
}

customConfig.plugins = customConfig.plugins.concat(plugins);
}

if (devServer && Object.keys(devServer).length > 0) {
Expand Down

0 comments on commit 0a1283b

Please sign in to comment.