13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- import { dependencies } from './package.json' ;
16
+
17
17
import commonjs from '@rollup/plugin-commonjs' ;
18
- import { terser } from 'rollup-plugin-terser' ;
18
+ import { terser } from 'rollup-plugin-terser' ;
19
19
import resolve from '@rollup/plugin-node-resolve' ;
20
+ import { dependencies } from './package.json' ;
20
21
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` ,
44
26
output : {
45
27
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 ( ) ] ,
49
31
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
+ } ;
52
51
53
- const umdconfig = {
52
+ const umdBundle = {
54
53
plugins : [
55
54
resolve ( { browser : true } ) ,
56
55
commonjs ( {
@@ -62,13 +61,10 @@ const umdconfig = {
62
61
'LogLevel' ,
63
62
'setLogHandler' ,
64
63
'setErrorHandler' ,
65
- 'getErrorHandler'
64
+ 'getErrorHandler' ,
66
65
] ,
67
- '@optimizely/js-sdk-event-processor' : [
68
- 'LogTierV1EventProcessor' ,
69
- 'LocalStoragePendingEventsDispatcher'
70
- ]
71
- }
66
+ '@optimizely/js-sdk-event-processor' : [ 'LogTierV1EventProcessor' , 'LocalStoragePendingEventsDispatcher' ] ,
67
+ } ,
72
68
} ) ,
73
69
] ,
74
70
input : 'lib/index.browser.js' ,
@@ -84,34 +80,49 @@ const umdconfig = {
84
80
format : 'umd' ,
85
81
file : 'dist/optimizely.browser.umd.min.js' ,
86
82
exports : 'named' ,
87
- plugins : [ terser ( ) ] ,
83
+ plugins : [ terser ( ) ] ,
88
84
sourcemap : true ,
89
85
} ,
90
86
] ,
91
87
} ;
92
88
93
89
// A separate bundle for json schema validator.
94
- const jsonSchemaValidatorConfig = {
95
- plugins : [
96
- resolve ( ) ,
97
- commonjs ( ) ,
98
- ] ,
90
+ const jsonSchemaBundle = {
91
+ plugins : [ resolve ( ) , commonjs ( ) ] ,
99
92
external : [ 'json-schema' , '@optimizely/js-sdk-utils' ] ,
100
93
input : 'lib/utils/json_schema_validator/index.js' ,
101
94
output : {
102
95
exports : 'named' ,
103
96
format : 'cjs' ,
104
97
file : 'dist/optimizely.json_schema_validator.min.js' ,
105
- plugins : [ terser ( ) ] ,
98
+ plugins : [ terser ( ) ] ,
106
99
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 ,
108
110
} ;
109
111
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 ( / c o n f i g - / , '' ) ) ;
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