Skip to content

Commit 764135b

Browse files
authored
Leverage rollup's --config* feature for choosing bundles (#477)
Summary: - Use rollup --config feature for choosing which bundles to build instead of environment variables - Add unminified ESM bundle to build output - Use clean and prebuild npm scripts instead of doing everything in build - Apply prettier formatting to rollup config file
1 parent 990245f commit 764135b

File tree

2 files changed

+68
-55
lines changed

2 files changed

+68
-55
lines changed

packages/optimizely-sdk/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
"react-native": "dist/optimizely.react_native.min.js",
99
"typings": "lib/index.d.ts",
1010
"scripts": {
11+
"clean": "rm -rf dist",
1112
"test": "mocha ./lib/*.tests.js ./lib/**/*.tests.js ./lib/**/**/*tests.js --recursive --exit --require esm --require lib/tests/exit_on_unhandled_rejection.js",
1213
"test-xbrowser": "karma start karma.bs.conf.js --single-run",
1314
"test-umdbrowser": "npm run build-browser-umd && karma start karma.umd.conf.js --single-run",
14-
"build-browser-umd": "rollup -c --environment BUILD_UMD_BUNDLE",
15-
"build": "rm -rf dist && rollup -c --environment BUILD_ALL",
15+
"prebuild": "npm run clean",
16+
"build": "rollup -c",
17+
"build-browser-umd": "rollup -c --config-umd",
1618
"test-ci": "npm run test-xbrowser && npm run test-umdbrowser",
1719
"lint": "eslint 'lib/**/*.js'",
1820
"cover": "istanbul cover _mocha ./lib/*.tests.js ./lib/**/*.tests.js ./lib/**/**/*tests.js",

packages/optimizely-sdk/rollup.config.js

Lines changed: 64 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,43 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { dependencies } from './package.json';
16+
1717
import commonjs from '@rollup/plugin-commonjs';
18-
import { terser } from 'rollup-plugin-terser';
18+
import { terser } from 'rollup-plugin-terser';
1919
import resolve from '@rollup/plugin-node-resolve';
20+
import { dependencies } from './package.json';
2021

21-
const BUILD_ALL = process.env.BUILD_ALL ? true : false;
22-
const BUILD_UMD_BUNDLE = process.env.BUILD_UMD_BUNDLE ? true : false;
23-
24-
const getCjsConfigForPlatform = (platform) => {
25-
return {
26-
plugins: [
27-
resolve(),
28-
commonjs(),
29-
],
30-
external: ['https', 'http', 'url'].concat(Object.keys(dependencies || {})),
31-
input: `lib/index.${platform}.js`,
32-
output: {
33-
exports: 'named',
34-
format: 'cjs',
35-
file: `dist/optimizely.${platform}.min.js`,
36-
plugins: [ terser() ],
37-
sourcemap: true,
38-
}
39-
};
40-
};
41-
42-
const esModuleConfig = {
43-
... getCjsConfigForPlatform('browser'),
22+
const cjsBundleFor = (platform) => ({
23+
plugins: [resolve(), commonjs()],
24+
external: ['https', 'http', 'url'].concat(Object.keys(dependencies || {})),
25+
input: `lib/index.${platform}.js`,
4426
output: {
4527
exports: 'named',
46-
format: 'es',
47-
file: 'dist/optimizely.browser.es.min.js',
48-
plugins: [ terser() ],
28+
format: 'cjs',
29+
file: `dist/optimizely.${platform}.min.js`,
30+
plugins: [terser()],
4931
sourcemap: true,
50-
}
51-
}
32+
},
33+
});
34+
35+
const esmBundle = {
36+
...cjsBundleFor('browser'),
37+
output: [
38+
{
39+
format: 'es',
40+
file: 'dist/optimizely.browser.es.js',
41+
sourcemap: true,
42+
},
43+
{
44+
format: 'es',
45+
file: 'dist/optimizely.browser.es.min.js',
46+
plugins: [terser()],
47+
sourcemap: true,
48+
},
49+
],
50+
};
5251

53-
const umdconfig = {
52+
const umdBundle = {
5453
plugins: [
5554
resolve({ browser: true }),
5655
commonjs({
@@ -62,13 +61,10 @@ const umdconfig = {
6261
'LogLevel',
6362
'setLogHandler',
6463
'setErrorHandler',
65-
'getErrorHandler'
64+
'getErrorHandler',
6665
],
67-
'@optimizely/js-sdk-event-processor': [
68-
'LogTierV1EventProcessor',
69-
'LocalStoragePendingEventsDispatcher'
70-
]
71-
}
66+
'@optimizely/js-sdk-event-processor': ['LogTierV1EventProcessor', 'LocalStoragePendingEventsDispatcher'],
67+
},
7268
}),
7369
],
7470
input: 'lib/index.browser.js',
@@ -84,34 +80,49 @@ const umdconfig = {
8480
format: 'umd',
8581
file: 'dist/optimizely.browser.umd.min.js',
8682
exports: 'named',
87-
plugins: [ terser() ],
83+
plugins: [terser()],
8884
sourcemap: true,
8985
},
9086
],
9187
};
9288

9389
// A separate bundle for json schema validator.
94-
const jsonSchemaValidatorConfig = {
95-
plugins: [
96-
resolve(),
97-
commonjs(),
98-
],
90+
const jsonSchemaBundle = {
91+
plugins: [resolve(), commonjs()],
9992
external: ['json-schema', '@optimizely/js-sdk-utils'],
10093
input: 'lib/utils/json_schema_validator/index.js',
10194
output: {
10295
exports: 'named',
10396
format: 'cjs',
10497
file: 'dist/optimizely.json_schema_validator.min.js',
105-
plugins: [ terser() ],
98+
plugins: [terser()],
10699
sourcemap: true,
107-
}
100+
},
101+
};
102+
103+
const bundles = {
104+
'cjs-node': cjsBundleFor('node'),
105+
'cjs-browser': cjsBundleFor('browser'),
106+
'cjs-react-native': cjsBundleFor('react_native'),
107+
esm: esmBundle,
108+
'json-schema': jsonSchemaBundle,
109+
umd: umdBundle,
108110
};
109111

110-
export default [
111-
BUILD_ALL && getCjsConfigForPlatform('node'),
112-
BUILD_ALL && getCjsConfigForPlatform('browser'),
113-
BUILD_ALL && getCjsConfigForPlatform('react_native'),
114-
BUILD_ALL && esModuleConfig,
115-
BUILD_ALL && jsonSchemaValidatorConfig,
116-
(BUILD_ALL || BUILD_UMD_BUNDLE) && umdconfig,
117-
].filter(config => config);
112+
// Collect all --config-* options and return the matching bundle configs
113+
// Builds all bundles if no --config-* option given
114+
// --config-cjs will build all three cjs-* bundles
115+
// --config-umd will build only the umd bundle
116+
// --config-umd --config-json will build both umd and the json-schema bundles
117+
export default (args) => {
118+
const patterns = Object.keys(args)
119+
.filter((arg) => arg.startsWith('config-'))
120+
.map((arg) => arg.replace(/config-/, ''));
121+
122+
// default to matching all bundles
123+
if (!patterns.length) patterns.push(/.*/);
124+
125+
return Object.entries(bundles)
126+
.filter(([name, config]) => patterns.some((pattern) => name.match(pattern)))
127+
.map(([name, config]) => config);
128+
};

0 commit comments

Comments
 (0)