Skip to content

Leverage rollup's --config* feature for choosing bundles #477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
};