Skip to content

Commit

Permalink
Leverage rollup's --config* feature for choosing bundles (#477)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jasonkarns committed May 15, 2020
1 parent 990245f commit 764135b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 55 deletions.
6 changes: 4 additions & 2 deletions packages/optimizely-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
"react-native": "dist/optimizely.react_native.min.js",
"typings": "lib/index.d.ts",
"scripts": {
"clean": "rm -rf dist",
"test": "mocha ./lib/*.tests.js ./lib/**/*.tests.js ./lib/**/**/*tests.js --recursive --exit --require esm --require lib/tests/exit_on_unhandled_rejection.js",
"test-xbrowser": "karma start karma.bs.conf.js --single-run",
"test-umdbrowser": "npm run build-browser-umd && karma start karma.umd.conf.js --single-run",
"build-browser-umd": "rollup -c --environment BUILD_UMD_BUNDLE",
"build": "rm -rf dist && rollup -c --environment BUILD_ALL",
"prebuild": "npm run clean",
"build": "rollup -c",
"build-browser-umd": "rollup -c --config-umd",
"test-ci": "npm run test-xbrowser && npm run test-umdbrowser",
"lint": "eslint 'lib/**/*.js'",
"cover": "istanbul cover _mocha ./lib/*.tests.js ./lib/**/*.tests.js ./lib/**/**/*tests.js",
Expand Down
117 changes: 64 additions & 53 deletions packages/optimizely-sdk/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,43 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { dependencies } from './package.json';

import commonjs from '@rollup/plugin-commonjs';
import { terser } from 'rollup-plugin-terser';
import { terser } from 'rollup-plugin-terser';
import resolve from '@rollup/plugin-node-resolve';
import { dependencies } from './package.json';

const BUILD_ALL = process.env.BUILD_ALL ? true : false;
const BUILD_UMD_BUNDLE = process.env.BUILD_UMD_BUNDLE ? true : false;

const getCjsConfigForPlatform = (platform) => {
return {
plugins: [
resolve(),
commonjs(),
],
external: ['https', 'http', 'url'].concat(Object.keys(dependencies || {})),
input: `lib/index.${platform}.js`,
output: {
exports: 'named',
format: 'cjs',
file: `dist/optimizely.${platform}.min.js`,
plugins: [ terser() ],
sourcemap: true,
}
};
};

const esModuleConfig = {
... getCjsConfigForPlatform('browser'),
const cjsBundleFor = (platform) => ({
plugins: [resolve(), commonjs()],
external: ['https', 'http', 'url'].concat(Object.keys(dependencies || {})),
input: `lib/index.${platform}.js`,
output: {
exports: 'named',
format: 'es',
file: 'dist/optimizely.browser.es.min.js',
plugins: [ terser() ],
format: 'cjs',
file: `dist/optimizely.${platform}.min.js`,
plugins: [terser()],
sourcemap: true,
}
}
},
});

const esmBundle = {
...cjsBundleFor('browser'),
output: [
{
format: 'es',
file: 'dist/optimizely.browser.es.js',
sourcemap: true,
},
{
format: 'es',
file: 'dist/optimizely.browser.es.min.js',
plugins: [terser()],
sourcemap: true,
},
],
};

const umdconfig = {
const umdBundle = {
plugins: [
resolve({ browser: true }),
commonjs({
Expand All @@ -62,13 +61,10 @@ const umdconfig = {
'LogLevel',
'setLogHandler',
'setErrorHandler',
'getErrorHandler'
'getErrorHandler',
],
'@optimizely/js-sdk-event-processor': [
'LogTierV1EventProcessor',
'LocalStoragePendingEventsDispatcher'
]
}
'@optimizely/js-sdk-event-processor': ['LogTierV1EventProcessor', 'LocalStoragePendingEventsDispatcher'],
},
}),
],
input: 'lib/index.browser.js',
Expand All @@ -84,34 +80,49 @@ const umdconfig = {
format: 'umd',
file: 'dist/optimizely.browser.umd.min.js',
exports: 'named',
plugins: [ terser() ],
plugins: [terser()],
sourcemap: true,
},
],
};

// A separate bundle for json schema validator.
const jsonSchemaValidatorConfig = {
plugins: [
resolve(),
commonjs(),
],
const jsonSchemaBundle = {
plugins: [resolve(), commonjs()],
external: ['json-schema', '@optimizely/js-sdk-utils'],
input: 'lib/utils/json_schema_validator/index.js',
output: {
exports: 'named',
format: 'cjs',
file: 'dist/optimizely.json_schema_validator.min.js',
plugins: [ terser() ],
plugins: [terser()],
sourcemap: true,
}
},
};

const bundles = {
'cjs-node': cjsBundleFor('node'),
'cjs-browser': cjsBundleFor('browser'),
'cjs-react-native': cjsBundleFor('react_native'),
esm: esmBundle,
'json-schema': jsonSchemaBundle,
umd: umdBundle,
};

export default [
BUILD_ALL && getCjsConfigForPlatform('node'),
BUILD_ALL && getCjsConfigForPlatform('browser'),
BUILD_ALL && getCjsConfigForPlatform('react_native'),
BUILD_ALL && esModuleConfig,
BUILD_ALL && jsonSchemaValidatorConfig,
(BUILD_ALL || BUILD_UMD_BUNDLE) && umdconfig,
].filter(config => config);
// Collect all --config-* options and return the matching bundle configs
// Builds all bundles if no --config-* option given
// --config-cjs will build all three cjs-* bundles
// --config-umd will build only the umd bundle
// --config-umd --config-json will build both umd and the json-schema bundles
export default (args) => {
const patterns = Object.keys(args)
.filter((arg) => arg.startsWith('config-'))
.map((arg) => arg.replace(/config-/, ''));

// default to matching all bundles
if (!patterns.length) patterns.push(/.*/);

return Object.entries(bundles)
.filter(([name, config]) => patterns.some((pattern) => name.match(pattern)))
.map(([name, config]) => config);
};

0 comments on commit 764135b

Please sign in to comment.