Skip to content

Commit 440ad95

Browse files
fix: adjust testing bundle creation
1 parent d8cf9b9 commit 440ad95

File tree

1 file changed

+58
-35
lines changed

1 file changed

+58
-35
lines changed

tools/build/config/rollup.config.mjs

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { getUMDGlobals } from './utils/getUMDGlobals.mjs';
1414
import { defaultsDeep } from 'lodash-es';
1515

1616
const extensions = ['.ts', '.tsx'];
17-
const testUtilsFilename = 'src/testing/index.ts';
17+
const testBundleGlob = 'src/testing/index.ts';
1818
const storyGlob = 'src/*.stor{y,ies}.tsx';
1919

2020
const babelConfigPath = fileURLToPath(
@@ -33,22 +33,27 @@ const moduleFormatToDirectory = {
3333
umd: 'dist/umd',
3434
};
3535

36-
const doTestUtilsExist = glob.sync(testUtilsFilename).length > 0;
37-
3836
/**
39-
* @param {{ format: import('rollup').OutputOptions['format'], useTerser?: boolean, outputNameSuffix?: string }} options
37+
* @param {{ format: import('rollup').OutputOptions['format'], useTerser?: boolean, outputFile?: string, outputName?: string, outputDir?: string }} options
4038
* @returns {import('rollup').OutputOptions}
4139
*/
42-
const createOutput = ({ format, useTerser = false, outputNameSuffix = '' }) => {
40+
const createOutput = ({
41+
format,
42+
useTerser = false,
43+
outputFile = undefined,
44+
outputName = '[name].js',
45+
outputDir = moduleFormatToDirectory[format],
46+
}) => {
4347
return {
44-
dir: moduleFormatToDirectory[format],
48+
dir: outputDir,
49+
file: outputFile,
4550
name,
4651
format,
4752
sourcemap: true,
4853
globals: format === 'umd' ? getUMDGlobals() : {},
4954
validate: true,
5055
interop: 'compat', // https://rollupjs.org/configuration-options/#output-interop
51-
entryFileNames: `[name]${outputNameSuffix}.js`,
56+
entryFileNames: outputName,
5257
plugins: useTerser ? [terser()] : [],
5358
};
5459
};
@@ -92,6 +97,7 @@ const createConfigForFormat = (output, overrides = {}) => {
9297
return finalConfig;
9398
};
9499

100+
// 1. Create the default esm/umd bundles configs
95101
const esmConfig = createConfigForFormat(
96102
createOutput({ format: 'esm', useTerser: true }),
97103
);
@@ -101,57 +107,74 @@ const umdConfig = createConfigForFormat(
101107

102108
const defaultConfig = [esmConfig, umdConfig];
103109

104-
// configurations for modern dev/prod bundle publishing
105-
// to be used by packages that are included in the experiment
110+
// 1.1. Create the modern dev/prod bundle configs
106111
const modernDevProdConfig = createConfigForFormat([
107112
createOutput({ format: 'esm' }),
108113
createOutput({ format: 'umd' }),
109114
createOutput({
110115
format: 'esm',
111116
useTerser: true,
112-
outputNameSuffix: '-min',
117+
outputName: '[name]-min.js',
113118
}),
114119
createOutput({
115120
format: 'umd',
116121
useTerser: true,
117-
outputNameSuffix: '-min',
122+
outputName: '[name]-min.js',
118123
}),
119124
]);
120125

121-
// Add additional entry point to UMD build for test-utils if they exist
122-
doTestUtilsExist &&
126+
// 2. Create testing bundles (if applicable)
127+
const testingBundleEntryPoints = glob.sync(testBundleGlob);
128+
129+
if (testingBundleEntryPoints.length > 0) {
123130
defaultConfig.push(
124-
createConfigForFormat('esm', {
125-
input: testUtilsFilename,
126-
output: {
127-
dir: `${moduleFormatToDirectory['esm']}/testing`,
131+
createConfigForFormat(
132+
createOutput({
133+
format: 'esm',
134+
useTerser: true,
135+
outputDir: `${moduleFormatToDirectory['esm']}/testing`,
136+
}),
137+
{
138+
input: testingBundleEntryPoints,
128139
},
129-
}),
130-
createConfigForFormat('umd', {
131-
input: testUtilsFilename,
132-
output: {
133-
dir: `${moduleFormatToDirectory['umd']}/testing`,
140+
),
141+
createConfigForFormat(
142+
createOutput({
143+
format: 'umd',
144+
useTerser: true,
145+
outputDir: `${moduleFormatToDirectory['umd']}/testing`,
146+
}),
147+
{
148+
input: testingBundleEntryPoints,
134149
},
135-
}),
150+
),
136151
);
152+
}
153+
154+
// 3. Create stories bundles (if applicable)
155+
156+
const storiesEntryPoints = glob.sync(storyGlob);
137157

138158
// FIXME: Figure out a way to get rid of this.
139159
// Creates a super-hacky `stories` bundle
140-
const storiesExist = glob.sync(storyGlob).length > 0;
141-
142-
/** @type {import('rollup').RollupOptions} */
143-
const storiesConfig = {
144-
...esmConfig,
145-
input: glob.sync(storyGlob)[0],
146-
output: {
147-
format: 'esm',
148-
file: 'stories.js',
160+
const storiesConfig = createConfigForFormat(
161+
{
162+
...createOutput({
163+
format: 'esm',
164+
useTerser: true,
165+
outputDir: null,
166+
outputFile: 'stories.js',
167+
}),
149168
sourcemap: false,
150-
globals: esmConfig.output.globals,
151169
},
152-
};
170+
{
171+
input: storiesEntryPoints[0],
172+
},
173+
);
153174

154-
storiesExist && defaultConfig.push(storiesConfig);
175+
if (storiesEntryPoints.length > 0) {
176+
defaultConfig.push(storiesConfig);
177+
}
155178

156179
export { modernDevProdConfig, esmConfig, storiesConfig, umdConfig };
157180

0 commit comments

Comments
 (0)