@@ -6,12 +6,14 @@ const babel = require('rollup-plugin-babel');
66const flow = require ( 'rollup-plugin-flow' ) ;
77const commonjs = require ( 'rollup-plugin-commonjs' ) ;
88const resolve = require ( 'rollup-plugin-node-resolve' ) ;
9+ const uglify = require ( 'rollup-plugin-uglify' ) ;
910const replace = require ( 'rollup-plugin-replace' ) ;
11+ const optimizeJs = require ( 'rollup-plugin-optimize-js' ) ;
1012const chalk = require ( 'chalk' ) ;
1113
1214const REACT_TV_VERSION = require ( '../../package.json' ) . version ;
1315
14- const Header = require ( './header' ) ;
16+ let tasks = [ ] ;
1517
1618function stripEnvVariables ( production ) {
1719 return {
@@ -20,70 +22,79 @@ function stripEnvVariables(production) {
2022 } ;
2123}
2224
23- function getBanner ( filename , moduleType ) {
24- return Header . getHeader ( filename , REACT_TV_VERSION ) ;
25- }
25+ function createBundle ( { entryPath, bundleType, destName } ) {
26+ entryPath = path . resolve ( entryPath ) ;
27+ const logKey = chalk . white . bold ( entryPath ) + chalk . dim ( ` (${ REACT_TV_VERSION } )` ) ;
28+ console . log ( `${ chalk . blue ( bundleType ) } ${ logKey } -> dist/${ destName } ` ) ;
2629
27- function runWaterfall ( promiseFactories ) {
28- if ( promiseFactories . length === 0 ) {
29- return Promise . resolve ( ) ;
30- }
30+ let plugins = [
31+ flow ( ) ,
32+ babel ( {
33+ babelrc : false ,
34+ exclude : 'node_modules/**' ,
35+ externalHelpers : true ,
36+ presets : [
37+ [ 'env' , { 'modules' : false } ] ,
38+ 'react' ,
39+ 'stage-2'
40+ ] ,
41+ plugins : [
42+ 'transform-flow-strip-types' ,
43+ 'external-helpers'
44+ ]
45+ } )
46+ ]
3147
32- const head = promiseFactories [ 0 ] ;
33- const tail = promiseFactories . slice ( 1 ) ;
48+ if ( bundleType . indexOf ( 'PROD' ) >= 0 )
49+ plugins = plugins . concat ( [
50+ uglify ( ) ,
51+ optimizeJs ( ) ,
52+ replace ( stripEnvVariables ( ) )
53+ ] )
3454
35- const nextPromiseFactory = head ;
36- const nextPromise = nextPromiseFactory ( ) ;
37- if ( ! nextPromise || typeof nextPromise . then !== 'function' ) {
38- throw new Error ( 'runWaterfall() received something that is not a Promise.' ) ;
39- }
55+ plugins = plugins . concat ( [
56+ commonjs ( ) ,
57+ resolve ( {
58+ jsnext : true ,
59+ main : true ,
60+ browser : true ,
61+ } )
62+ ] ) ;
4063
41- return nextPromise . then ( ( ) => {
42- return runWaterfall ( tail ) ;
43- } ) ;
64+ rollup ( {
65+ input : entryPath ,
66+ plugins : plugins ,
67+ external : [ 'react' ] ,
68+ sourcemap : false ,
69+ } ) . then ( bundle => {
70+ tasks . push (
71+ bundle . write ( {
72+ format : ( bundleType === 'PROD-UMD' ) ? 'umd' : 'iife' ,
73+ name : 'ReactTV' ,
74+ file : `dist/${ destName } ` ,
75+ } )
76+ )
77+ } )
4478}
4579
46- function createBundle ( { entryPath, bundleType } ) {
47- console . log ( `${ chalk . bgGreen . white ( REACT_TV_VERSION ) } ` ) ;
48-
49- entryPath = path . resolve ( entryPath ) ;
50- const logKey = chalk . white . bold ( entryPath ) + chalk . dim ( ` (${ bundleType . toLowerCase ( ) } )` ) ;
51- console . log ( `${ chalk . bgYellow . black ( ' BUILDING ' ) } ${ logKey } ` ) ;
80+ createBundle ( {
81+ entryPath : 'src/ReactTVEntry.js' ,
82+ bundleType : 'DEV' ,
83+ destName : 'react-tv.js' ,
84+ } ) ;
5285
53- rollup ( {
54- entry : entryPath ,
55- plugins : [
56- flow ( ) ,
57- babel ( {
58- babelrc : false ,
59- exclude : 'node_modules/**' ,
60- presets : [
61- [ 'env' , { 'modules' : false } ] ,
62- 'react' ,
63- 'stage-2'
64- ] ,
65- plugins : [
66- 'transform-flow-strip-types'
67- ]
68- } ) ,
69- commonjs ( ) ,
70- resolve ( {
71- jsnext : true ,
72- } ) ,
73- replace ( stripEnvVariables ( ) )
74- ]
75- } ) . then ( bundle => Promise . all ( [
76- bundle . write ( {
77- banner : getBanner ( 'react-tv.js' ) ,
78- format : 'iife' ,
79- moduleName : 'ReactTV' ,
80- sourceMap : 'inline' ,
81- dest : 'build/react-tv.js' ,
82- } )
83- ] ) ) . catch ( error => console . log ( error ) ) ;
84- }
86+ createBundle ( {
87+ entryPath : 'src/ReactTVEntry.js' ,
88+ bundleType : 'PROD' ,
89+ destName : 'react-tv.min.js' ,
90+ } ) ;
8591
8692createBundle ( {
8793 entryPath : 'src/ReactTVEntry.js' ,
88- bundleType : 'DEV'
94+ bundleType : 'PROD-UMD' ,
95+ destName : 'react-tv.umd.js' ,
96+ } ) ;
97+
98+ Promise . all ( tasks ) . catch ( error => {
99+ Promise . reject ( error ) ;
89100} ) ;
0 commit comments