diff --git a/packages/optimizely-sdk/package.json b/packages/optimizely-sdk/package.json index f0751fea2..4d3c48259 100644 --- a/packages/optimizely-sdk/package.json +++ b/packages/optimizely-sdk/package.json @@ -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", diff --git a/packages/optimizely-sdk/rollup.config.js b/packages/optimizely-sdk/rollup.config.js index 93851f949..129bdbc51 100644 --- a/packages/optimizely-sdk/rollup.config.js +++ b/packages/optimizely-sdk/rollup.config.js @@ -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({ @@ -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', @@ -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); +};