Skip to content

Commit 161d302

Browse files
authored
feat(build): Add ability to combine package-specific rollup config with standard options (#5144)
Currently, the functions which we use to generate rollup configs accept a small handful of pre-defined options for customizing the output. For greater flexibility, this adds the option of passing arbitrary rollup options, which will get merged into what is currently being generated.
1 parent 7c71013 commit 161d302

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

packages/nextjs/rollup.npm.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ export default makeNPMConfigVariants(
77
entrypoints: ['src/index.server.ts', 'src/index.client.ts', 'src/utils/instrumentServer.ts'],
88
// prevent this nextjs code from ending up in our built package (this doesn't happen automatially because the name
99
// doesn't match an SDK dependency)
10-
externals: ['next/router'],
10+
packageSpecificConfig: { external: ['next/router'] },
1111
}),
1212
);

rollup/bundleHelpers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
import { mergePlugins } from './utils';
2222

2323
export function makeBaseBundleConfig(options) {
24-
const { bundleType, entrypoints, jsVersion, licenseTitle, outputFileBase } = options;
24+
const { bundleType, entrypoints, jsVersion, licenseTitle, outputFileBase, packageSpecificConfig } = options;
2525

2626
const nodeResolvePlugin = makeNodeResolvePlugin();
2727
const sucrasePlugin = makeSucrasePlugin();
@@ -113,7 +113,7 @@ export function makeBaseBundleConfig(options) {
113113
node: nodeBundleConfig,
114114
};
115115

116-
return deepMerge(sharedBundleConfig, bundleTypeConfigMap[bundleType], {
116+
return deepMerge.all([sharedBundleConfig, bundleTypeConfigMap[bundleType], packageSpecificConfig || {}], {
117117
// Plugins have to be in the correct order or everything breaks, so when merging we have to manually re-order them
118118
customMerge: key => (key === 'plugins' ? mergePlugins : undefined),
119119
});

rollup/npmHelpers.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ import {
1515
makeRemoveESLintCommentsPlugin,
1616
makeSucrasePlugin,
1717
} from './plugins/index.js';
18+
import { mergePlugins } from './utils';
1819

1920
const packageDotJSON = require(path.resolve(process.cwd(), './package.json'));
2021

2122
export function makeBaseNPMConfig(options = {}) {
2223
const {
2324
entrypoints = ['src/index.ts'],
2425
esModuleInterop = false,
25-
externals: packageSpecificExternals = [],
2626
hasBundles = false,
27+
packageSpecificConfig = {},
2728
} = options;
2829

2930
const nodeResolvePlugin = makeNodeResolvePlugin();
@@ -33,7 +34,7 @@ export function makeBaseNPMConfig(options = {}) {
3334
const removeBlankLinesPlugin = makeRemoveBlankLinesPlugin();
3435
const extractPolyfillsPlugin = makeExtractPolyfillsPlugin();
3536

36-
return {
37+
const defaultBaseConfig = {
3738
input: entrypoints,
3839

3940
output: {
@@ -91,7 +92,6 @@ export function makeBaseNPMConfig(options = {}) {
9192
...Object.keys(packageDotJSON.dependencies || {}),
9293
...Object.keys(packageDotJSON.devDependencies || {}),
9394
...Object.keys(packageDotJSON.peerDependencies || {}),
94-
...packageSpecificExternals,
9595
],
9696

9797
// TODO `'smallest'` will get rid of `isDebugBuild()` by evaluating it and inlining the result and then treeshaking
@@ -100,6 +100,11 @@ export function makeBaseNPMConfig(options = {}) {
100100
// treeshake: 'smallest',
101101
treeshake: false,
102102
};
103+
104+
return deepMerge(defaultBaseConfig, packageSpecificConfig, {
105+
// Plugins have to be in the correct order or everything breaks, so when merging we have to manually re-order them
106+
customMerge: key => (key === 'plugins' ? mergePlugins : undefined),
107+
});
103108
}
104109

105110
export function makeNPMConfigVariants(baseConfig) {

0 commit comments

Comments
 (0)