Skip to content

Commit

Permalink
Issue-1118: Refactor rollup config generation for APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
DevLab2425 committed Dec 5, 2023
1 parent a819187 commit a51d550
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
36 changes: 17 additions & 19 deletions packages/cli/src/config/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
10 changes: 6 additions & 4 deletions packages/cli/src/lifecycles/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}

Expand Down

0 comments on commit a51d550

Please sign in to comment.