|
| 1 | +/** |
| 2 | + * Copyright 2020-2022 Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import commonjs from '@rollup/plugin-commonjs'; |
| 18 | +import { terser } from 'rollup-plugin-terser'; |
| 19 | +import resolve from '@rollup/plugin-node-resolve'; |
| 20 | +import alias from '@rollup/plugin-alias'; |
| 21 | +import { dependencies, peerDependencies } from './package.json'; |
| 22 | + |
| 23 | +const aliasPlugin = alias({ |
| 24 | + entries: [ |
| 25 | + { find: 'error_message', replacement: './.build/message/error_message.gen.js' }, |
| 26 | + { find: 'log_message', replacement: './.build/message/log_message.gen.js' }, |
| 27 | + ] |
| 28 | +}); |
| 29 | + |
| 30 | +const cjsBundleFor = (platform, opt = {}) => { |
| 31 | + const { minify, ext } = { |
| 32 | + minify: true, |
| 33 | + ext: '.js', |
| 34 | + ...opt, |
| 35 | + }; |
| 36 | + |
| 37 | + const min = minify ? '.min' : ''; |
| 38 | + |
| 39 | + return { |
| 40 | + plugins: [aliasPlugin, resolve(), commonjs()], |
| 41 | + external: ['https', 'http', 'url'].concat(Object.keys({ ...dependencies, ...peerDependencies } || {})), |
| 42 | + input: `.build/index.${platform}.js`, |
| 43 | + output: { |
| 44 | + exports: 'named', |
| 45 | + format: 'cjs', |
| 46 | + file: `dist/index.${platform}${min}${ext}`, |
| 47 | + plugins: minify ? [terser()] : undefined, |
| 48 | + sourcemap: true, |
| 49 | + }, |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +const esmBundleFor = (platform, opt) => { |
| 54 | + const { minify, ext } = { |
| 55 | + minify: true, |
| 56 | + ext: '.js', |
| 57 | + ...opt, |
| 58 | + }; |
| 59 | + |
| 60 | + const min = minify ? '.min' : ''; |
| 61 | + |
| 62 | + return { |
| 63 | + ...cjsBundleFor(platform), |
| 64 | + output: [ |
| 65 | + { |
| 66 | + format: 'es', |
| 67 | + file: `dist/index.${platform}.es${min}${ext}`, |
| 68 | + plugins: minify ? [terser()] : undefined, |
| 69 | + sourcemap: true, |
| 70 | + }, |
| 71 | + ], |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +const cjsBundleForUAParser = (opt = {}) => { |
| 76 | + const { minify, ext } = { |
| 77 | + minify: true, |
| 78 | + ext: '.js', |
| 79 | + ...opt, |
| 80 | + }; |
| 81 | + |
| 82 | + const min = minify ? '.min' : ''; |
| 83 | + |
| 84 | + return { |
| 85 | + plugins: [aliasPlugin, resolve(), commonjs()], |
| 86 | + external: ['https', 'http', 'url'].concat(Object.keys({ ...dependencies, ...peerDependencies } || {})), |
| 87 | + input: `.build/odp/ua_parser/ua_parser.js`, |
| 88 | + output: { |
| 89 | + exports: 'named', |
| 90 | + format: 'cjs', |
| 91 | + file: `dist/ua_parser${min}${ext}`, |
| 92 | + plugins: minify ? [terser()] : undefined, |
| 93 | + sourcemap: true, |
| 94 | + }, |
| 95 | + }; |
| 96 | +}; |
| 97 | + |
| 98 | +const esmBundleForUAParser = (opt = {}) => { |
| 99 | + const { minify, ext } = { |
| 100 | + minify: true, |
| 101 | + ext: '.js', |
| 102 | + ...opt, |
| 103 | + }; |
| 104 | + |
| 105 | + const min = minify ? '.min' : ''; |
| 106 | + |
| 107 | + return { |
| 108 | + ...cjsBundleForUAParser(), |
| 109 | + output: [ |
| 110 | + { |
| 111 | + format: 'es', |
| 112 | + file: `dist/ua_parser.es${min}${ext}`, |
| 113 | + plugins: minify ? [terser()] : undefined, |
| 114 | + sourcemap: true, |
| 115 | + }, |
| 116 | + ], |
| 117 | + }; |
| 118 | +}; |
| 119 | + |
| 120 | +const umdBundle = { |
| 121 | + plugins: [ |
| 122 | + aliasPlugin, |
| 123 | + resolve({ browser: true }), |
| 124 | + commonjs({ |
| 125 | + namedExports: { |
| 126 | + '@optimizely/js-sdk-event-processor': ['LogTierV1EventProcessor', 'LocalStoragePendingEventsDispatcher'], |
| 127 | + 'json-schema': ['validate'], |
| 128 | + }, |
| 129 | + }), |
| 130 | + ], |
| 131 | + input: '.build/index.browser.js', |
| 132 | + output: [ |
| 133 | + { |
| 134 | + name: 'optimizelySdk', |
| 135 | + format: 'umd', |
| 136 | + file: 'dist/optimizely.browser.umd.js', |
| 137 | + exports: 'named', |
| 138 | + }, |
| 139 | + { |
| 140 | + name: 'optimizelySdk', |
| 141 | + format: 'umd', |
| 142 | + file: 'dist/optimizely.browser.umd.min.js', |
| 143 | + exports: 'named', |
| 144 | + plugins: [terser()], |
| 145 | + sourcemap: true, |
| 146 | + }, |
| 147 | + ], |
| 148 | +}; |
| 149 | + |
| 150 | +// A separate bundle for json schema validator. |
| 151 | +const jsonSchemaBundle = { |
| 152 | + plugins: [aliasPlugin, resolve(), commonjs()], |
| 153 | + external: ['json-schema', 'uuid'], |
| 154 | + input: '.build/utils/json_schema_validator/index.js', |
| 155 | + output: { |
| 156 | + exports: 'named', |
| 157 | + format: 'cjs', |
| 158 | + file: 'dist/optimizely.json_schema_validator.min.js', |
| 159 | + plugins: [terser()], |
| 160 | + sourcemap: true, |
| 161 | + }, |
| 162 | +}; |
| 163 | + |
| 164 | +const bundles = { |
| 165 | + 'cjs-node-min': cjsBundleFor('node'), |
| 166 | + 'cjs-browser-min': cjsBundleFor('browser'), |
| 167 | + 'cjs-react-native-min': cjsBundleFor('react_native'), |
| 168 | + 'cjs-universal': cjsBundleFor('universal'), |
| 169 | + 'esm-browser-min': esmBundleFor('browser'), |
| 170 | + 'esm-node-min': esmBundleFor('node', { ext: '.mjs' }), |
| 171 | + 'esm-react-native-min': esmBundleFor('react_native'), |
| 172 | + 'esm-universal': esmBundleFor('universal'), |
| 173 | + 'json-schema': jsonSchemaBundle, |
| 174 | + 'cjs-ua-parser-min': cjsBundleForUAParser(), |
| 175 | + 'esm-ua-parser-min': esmBundleForUAParser(), |
| 176 | + umd: umdBundle, |
| 177 | +}; |
| 178 | + |
| 179 | +// Collect all --config-* options and return the matching bundle configs |
| 180 | +// Builds all bundles if no --config-* option given |
| 181 | +// --config-cjs will build all three cjs-* bundles |
| 182 | +// --config-umd will build only the umd bundle |
| 183 | +// --config-umd --config-json will build both umd and the json-schema bundles |
| 184 | +export default args => { |
| 185 | + const patterns = Object.keys(args) |
| 186 | + .filter(arg => arg.startsWith('config-')) |
| 187 | + .map(arg => arg.replace(/config-/, '')); |
| 188 | + |
| 189 | + // default to matching all bundles |
| 190 | + if (!patterns.length) patterns.push(/.*/); |
| 191 | + |
| 192 | + return Object.entries(bundles) |
| 193 | + .filter(([name, config]) => patterns.some(pattern => name.match(pattern))) |
| 194 | + .map(([name, config]) => config); |
| 195 | +}; |
0 commit comments