13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
-
17
- import path from 'path' ;
18
- import alias from '@rollup/plugin-alias' ;
16
+ import { dependencies } from './package.json' ;
19
17
import commonjs from '@rollup/plugin-commonjs' ;
20
18
import { terser } from 'rollup-plugin-terser' ;
21
19
import resolve from '@rollup/plugin-node-resolve' ;
22
- import visualizer from 'rollup-plugin-visualizer' ;
23
- import { dependencies } from './package.json' ;
24
20
25
- const cjsBuildFor = ( platform ) => ( {
26
- plugins : [
27
- resolve ( ) ,
28
- commonjs ( ) ,
29
- ] ,
30
- external : [ 'https' , 'http' , 'url' ] . concat ( Object . keys ( dependencies || { } ) ) ,
31
- input : `lib/index.${ platform } .js` ,
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' ) ,
32
44
output : {
33
45
exports : 'named' ,
34
- format : 'cjs' ,
35
- file : `dist/optimizely.${ platform } .min.js` ,
36
- plugins : [ terser ( ) ] ,
37
- sourcemap : true ,
38
- }
39
- } ) ;
40
-
41
- const esmBundle = {
42
- ...cjsBuildFor ( 'browser' ) ,
43
- output : [ {
44
- format : 'es' ,
45
- file : 'dist/optimizely.browser.es.js' ,
46
- sourcemap : true ,
47
- } , {
48
46
format : 'es' ,
49
47
file : 'dist/optimizely.browser.es.min.js' ,
50
- sourcemap : true ,
51
48
plugins : [ terser ( ) ] ,
52
49
sourcemap : true ,
53
- } ]
54
- }
55
-
56
- // ESM Bundle for browsers with all optimizely deps included
57
- const esmFullBundle = {
58
- input : 'lib/index.browser.js' ,
59
- external : [ 'https' , 'http' , 'url' ] ,
60
- plugins : [
61
- resolve ( { browser : true } ) ,
62
- commonjs ( {
63
- namedExports : {
64
- '@optimizely/js-sdk-logging' : [
65
- 'ConsoleLogHandler' ,
66
- 'getLogger' ,
67
- 'setLogLevel' ,
68
- 'LogLevel' ,
69
- 'setLogHandler' ,
70
- 'setErrorHandler' ,
71
- 'getErrorHandler' ,
72
- ] ,
73
- '@optimizely/js-sdk-event-processor' : [
74
- 'LogTierV1EventProcessor' ,
75
- 'LocalStoragePendingEventsDispatcher' ,
76
- ]
77
- }
78
- } ) ,
79
- ] ,
80
- output : {
81
- format : 'es' ,
82
- file : 'dist/optimizely.browser.es.full.js' ,
83
- sourcemap : true ,
84
- } ,
85
- }
86
-
87
- const esmSlimBundle = {
88
- ...esmFullBundle ,
89
- plugins : [
90
- alias ( { entries : [
91
- { find : './project_config_schema' ,
92
- replacement : path . resolve ( __dirname , 'ext/project_config_schema.json' ) } ,
93
- { find : path . resolve ( __dirname , 'lib/utils/config_validator/index.js' ) ,
94
- replacement : path . resolve ( __dirname , 'ext/config_validator.js' ) } ,
95
- { find : / .* \/ e n u m s $ / ,
96
- replacement : path . resolve ( __dirname , 'ext/enums.js' ) }
97
- ] } ) ,
98
- ...esmFullBundle . plugins ,
99
- visualizer ( ) ,
100
- ] ,
101
- output : {
102
- format : 'es' ,
103
- file : 'dist/optimizely.browser.es.slim.js' ,
104
- sourcemap : true ,
105
- } ,
50
+ }
106
51
}
107
52
108
- const umdBundle = {
53
+ const umdconfig = {
109
54
plugins : [
110
55
resolve ( { browser : true } ) ,
111
56
commonjs ( {
@@ -146,7 +91,7 @@ const umdBundle = {
146
91
} ;
147
92
148
93
// A separate bundle for json schema validator.
149
- const jsonSchemaBundle = {
94
+ const jsonSchemaValidatorConfig = {
150
95
plugins : [
151
96
resolve ( ) ,
152
97
commonjs ( ) ,
@@ -162,35 +107,11 @@ const jsonSchemaBundle = {
162
107
}
163
108
} ;
164
109
165
- const bundles = {
166
- 'cjs-node' : cjsBuildFor ( 'node' ) ,
167
- 'cjs-browser' : cjsBuildFor ( 'browser' ) ,
168
- 'cjs-react-native' : cjsBuildFor ( 'react_native' ) ,
169
- 'esm' : esmBundle ,
170
- 'esm-full' : esmFullBundle ,
171
- 'esm-slim' : esmSlimBundle ,
172
- 'json-schema' : jsonSchemaBundle ,
173
- 'umd' : umdBundle ,
174
- }
175
-
176
- // Returns the bundle config matching the given pattern
177
- const bundlesMatching = pattern => Object . entries ( bundles )
178
- . reduce ( ( bundles , [ name , config ] ) => {
179
- if ( name . match ( pattern ) ) bundles . push ( config )
180
- return bundles
181
- } , [ ] )
182
-
183
- // Collect all --config-* options and return the matching bundle configs
184
- // Builds all bundles if no --config-* option given
185
- // --config-cjs will build all three cjs-* bundles
186
- // --config-umd will build only the umd bundle
187
- // --config-umd --config-json will build both umd and the json-schema bundles
188
- export default args => {
189
- const patterns = Object . keys ( args )
190
- . filter ( arg => arg . startsWith ( 'config-' ) )
191
- . map ( arg => arg . replace ( / c o n f i g - / , '' ) )
192
-
193
- if ( ! patterns . length ) patterns . push ( / .* / )
194
-
195
- return patterns . flatMap ( bundlesMatching )
196
- }
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 ) ;
0 commit comments