From a51d5502e223fc1bbeb7043a3d065138ed654bc5 Mon Sep 17 00:00:00 2001 From: Paul Barry Date: Mon, 4 Dec 2023 11:07:22 -0500 Subject: [PATCH] Issue-1118: Refactor rollup config generation for APIs --- packages/cli/src/config/rollup.config.js | 36 +++++++++++------------- packages/cli/src/lifecycles/bundle.js | 10 ++++--- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/packages/cli/src/config/rollup.config.js b/packages/cli/src/config/rollup.config.js index 8aa6f56c2..823aef2cf 100644 --- a/packages/cli/src/config/rollup.config.js +++ b/packages/cli/src/config/rollup.config.js @@ -355,31 +355,29 @@ const getRollupConfigForScriptResources = async (compilation) => { const getRollupConfigForApis = async (compilation) => { const { outputDir, userWorkspace } = compilation.context; - const input = [...compilation.manifest.apis.values()] - .map(api => normalizePathnameForWindows(new URL(`.${api.path}`, userWorkspace))); // why is this needed? await fs.promises.mkdir(new URL('./api/assets/', outputDir), { recursive: true }); - // TODO should routes and APIs have chunks? - // https://github.com/ProjectEvergreen/greenwood/issues/1118 - return [{ - input, - output: { - dir: `${normalizePathnameForWindows(outputDir)}/api`, - entryFileNames: '[name].js', - chunkFileNames: '[name].[hash].js' - }, - plugins: [ - greenwoodJsonLoader(), - greenwoodResourceLoader(compilation), - nodeResolve(), - commonjs(), - greenwoodImportMetaUrl(compilation) - ] - }]; + return [...compilation.manifest.apis.values()] + .map(api => normalizePathnameForWindows(new URL(`.${api.path}`, userWorkspace))) + .map(filepath => ({ + input: filepath, + output: { + dir: `${normalizePathnameForWindows(outputDir)}/api`, + entryFileNames: '[name].js', + chunkFileNames: '[name].[hash].js' + }, + plugins: [ + greenwoodJsonLoader(), + greenwoodResourceLoader(compilation), + nodeResolve(), + commonjs(), + greenwoodImportMetaUrl(compilation) + ] + })); }; const getRollupConfigForSsr = async (compilation, input) => { diff --git a/packages/cli/src/lifecycles/bundle.js b/packages/cli/src/lifecycles/bundle.js index e5a28fdd5..1620039dd 100644 --- a/packages/cli/src/lifecycles/bundle.js +++ b/packages/cli/src/lifecycles/bundle.js @@ -163,11 +163,13 @@ async function bundleStyleResources(compilation, resourcePlugins) { async function bundleApiRoutes(compilation) { // https://rollupjs.org/guide/en/#differences-to-the-javascript-api - const [rollupConfig] = await getRollupConfigForApis(compilation); + const apiConfigs = await getRollupConfigForApis(compilation); - if (rollupConfig.input.length !== 0) { - const bundle = await rollup(rollupConfig); - await bundle.write(rollupConfig.output); + if (apiConfigs.length > 0 && apiConfigs[0].input.length !== 0) { + apiConfigs.forEach(async rollupConfig => { + const bundle = await rollup(rollupConfig); + await bundle.write(rollupConfig.output); + }); } }