Skip to content

Commit 5b0b4e2

Browse files
committed
add function for merging plugin arrays
1 parent 148479a commit 5b0b4e2

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

rollup/bundleHelpers.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
* Rollup config docs: https://rollupjs.org/guide/en/#big-list-of-options
33
*/
44

5-
import assert from 'assert';
6-
75
import deepMerge from 'deepmerge';
86

97
import {
@@ -17,7 +15,7 @@ import {
1715
makeTerserPlugin,
1816
makeTSPlugin,
1917
} from './plugins/index.js';
20-
import { getLastElement, insertAt } from './utils.js';
18+
import { mergePlugins } from './utils';
2119

2220
export function makeBaseBundleConfig(options) {
2321
const { input, isAddOn, jsVersion, licenseTitle, outputFileBase } = options;
@@ -95,7 +93,10 @@ export function makeBaseBundleConfig(options) {
9593
treeshake: 'smallest',
9694
};
9795

98-
return deepMerge(sharedBundleConfig, isAddOn ? addOnBundleConfig : standAloneBundleConfig);
96+
return deepMerge(sharedBundleConfig, isAddOn ? addOnBundleConfig : standAloneBundleConfig, {
97+
// Plugins have to be in the correct order or everything breaks, so when merging we have to manually re-order them
98+
customMerge: key => (key === 'plugins' ? mergePlugins : undefined),
99+
});
99100
}
100101

101102
/**
@@ -108,52 +109,45 @@ export function makeBaseBundleConfig(options) {
108109
* @returns An array of versions of that config
109110
*/
110111
export function makeBundleConfigVariants(baseConfig) {
111-
const { plugins: baseConfigPlugins } = baseConfig;
112112
const includeDebuggingPlugin = makeIsDebugBuildPlugin(true);
113113
const stripDebuggingPlugin = makeIsDebugBuildPlugin(false);
114114
const terserPlugin = makeTerserPlugin();
115115

116-
// The license plugin has to be last, so it ends up after terser. Otherwise, terser will remove the license banner.
117-
assert(
118-
getLastElement(baseConfigPlugins).name === 'rollup-plugin-license',
119-
`Last plugin in given options should be \`rollup-plugin-license\`. Found ${getLastElement(baseConfigPlugins).name}`,
120-
);
121-
122116
// The additional options to use for each variant we're going to create
123117
const variantSpecificConfigs = [
124118
{
125119
output: {
126120
file: `${baseConfig.output.file}.js`,
127121
},
128-
plugins: insertAt(baseConfigPlugins, -2, includeDebuggingPlugin),
122+
plugins: [includeDebuggingPlugin],
129123
},
130124
// This variant isn't particularly helpful for an SDK user, as it strips logging while making no other minification
131125
// changes, so by default we don't create it. It is however very useful when debugging rollup's treeshaking, so it's
132126
// left here for that purpose.
133127
// {
134128
// output: { file: `${baseConfig.output.file}.no-debug.js`,
135129
// },
136-
// plugins: insertAt(plugins, -2, stripDebuggingPlugin),
130+
// plugins: [stripDebuggingPlugin],
137131
// },
138132
{
139133
output: {
140134
file: `${baseConfig.output.file}.min.js`,
141135
},
142-
plugins: insertAt(baseConfigPlugins, -2, stripDebuggingPlugin, terserPlugin),
136+
plugins: [stripDebuggingPlugin, terserPlugin],
143137
},
144138
{
145139
output: {
146140
file: `${baseConfig.output.file}.debug.min.js`,
147141
},
148-
plugins: insertAt(baseConfigPlugins, -2, includeDebuggingPlugin, terserPlugin),
142+
plugins: [terserPlugin],
149143
},
150144
];
151145

152146
return variantSpecificConfigs.map(variant =>
153147
deepMerge(baseConfig, variant, {
154-
// this makes it so that instead of concatenating the `plugin` properties of the two objects, the first value is
155-
// just overwritten by the second value
156-
arrayMerge: (first, second) => second,
148+
// Merge the plugin arrays and make sure the end result is in the correct order. Everything else can use the
149+
// default merge strategy.
150+
customMerge: key => (key === 'plugins' ? mergePlugins : undefined),
157151
}),
158152
);
159153
}

rollup/utils.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
/**
2-
* Helper functions to compensate for the fact that JS can't handle negative array indices very well
2+
* Helper function to compensate for the fact that JS can't handle negative array indices very well
33
*/
4-
5-
export const getLastElement = array => {
6-
return array[array.length - 1];
7-
};
8-
94
export const insertAt = (arr, index, ...insertees) => {
105
const newArr = [...arr];
116
// Add 1 to the array length so that the inserted element ends up in the right spot with respect to the length of the
@@ -14,3 +9,22 @@ export const insertAt = (arr, index, ...insertees) => {
149
newArr.splice(destinationIndex, 0, ...insertees);
1510
return newArr;
1611
};
12+
13+
/**
14+
* Merge two arrays of plugins, making sure they're sorted in the correct order.
15+
*/
16+
export function mergePlugins(pluginsA, pluginsB) {
17+
const plugins = [...pluginsA, ...pluginsB];
18+
plugins.sort((a, b) => {
19+
// Hacky way to make sure the ones we care about end up where they belong in the order. (Really the TS and sucrase
20+
// plugins are tied - both should come first - but they're mutually exclusive, so they can come in arbitrary order
21+
// here.)
22+
const order = ['typescript', 'sucrase', '...', 'terser', 'license'];
23+
const sortKeyA = order.includes(a.name) ? a.name : '...';
24+
const sortKeyB = order.includes(b.name) ? b.name : '...';
25+
26+
return order.indexOf(sortKeyA) - order.indexOf(sortKeyB);
27+
});
28+
29+
return plugins;
30+
}

0 commit comments

Comments
 (0)