1
1
var path = require ( 'path' ) ;
2
+ var minimist = require ( 'minimist' ) ;
2
3
var runSeries = require ( 'run-series' ) ;
3
4
var prependFile = require ( 'prepend-file' ) ;
4
5
@@ -7,41 +8,78 @@ var common = require('./util/common');
7
8
var _bundle = require ( './util/browserify_wrapper' ) ;
8
9
9
10
var header = constants . licenseDist + '\n' ;
11
+ var allTransforms = constants . allTransforms ;
10
12
var allTraces = constants . allTraces ;
11
13
var mainIndex = constants . mainIndex ;
12
14
13
- var argv = process . argv ;
14
-
15
- if ( argv . length > 2 ) {
16
- // command line
17
-
18
- var traceList = [ 'scatter' ] ; // added by default
19
- var name ;
20
- for ( var i = 2 ; i < argv . length ; i ++ ) {
21
- var a = argv [ i ] ;
22
-
15
+ function createList ( outList , inList , allList , type ) {
16
+ for ( var i = 0 ; i < inList . length ; i ++ ) {
17
+ var t = inList [ i ] ;
23
18
if (
24
- allTraces . indexOf ( a ) !== - 1 && // requested
25
- traceList . indexOf ( a ) === - 1 // not added before
19
+ outList . indexOf ( t ) === - 1 // not added before
26
20
) {
27
- traceList . push ( a ) ;
21
+ if ( allList . indexOf ( t ) === - 1 ) {
22
+ console . error ( t , 'is not a valid ' + type + '!' , 'Valid ' + type + 's are:' , allList ) ;
23
+ } else {
24
+ outList . push ( t ) ;
25
+ }
28
26
}
29
- if ( a . indexOf ( '--name=' ) === 0 ) name = a . replace ( '--name=' , '' ) ;
30
27
}
31
- if ( ! name ) name = 'custom' ;
32
- traceList = traceList . sort ( ) ;
28
+
29
+ return outList . sort ( ) ;
30
+ }
31
+
32
+ function isFalse ( a ) {
33
+ return (
34
+ a === 'none' ||
35
+ a === 'false'
36
+ ) ;
37
+ }
38
+
39
+ function inputBoolean ( a , dflt ) {
40
+ return ! a ? dflt : ! isFalse ( a ) ;
41
+ }
42
+
43
+ function inputArray ( a , dflt ) {
44
+ dflt = dflt . slice ( ) ;
45
+
46
+ return (
47
+ isFalse ( a ) ? [ ] :
48
+ ! a || a === 'all' ? dflt :
49
+ a . split ( ',' )
50
+ ) ;
51
+ }
52
+
53
+ if ( process . argv . length > 2 ) {
54
+ // command line
55
+
56
+ var args = minimist ( process . argv . slice ( 2 ) , { } ) ;
57
+
58
+ // parse arguments
59
+ var unminified = inputBoolean ( args . unminified , false ) ;
60
+ var out = args . out ? args . out : 'custom' ;
61
+ var traces = inputArray ( args . traces , allTraces ) ;
62
+ var transforms = inputArray ( args . transforms , allTransforms ) ;
33
63
34
64
var opts = {
35
- traceList : traceList ,
36
- name : name ,
65
+ traceList : createList ( [ 'scatter' ] , traces , allTraces , 'trace' ) ,
66
+ transformList : createList ( [ ] , transforms , allTransforms , 'transform' ) ,
37
67
38
- index : path . join ( constants . pathToBuild , 'index-' + name + '.js' ) ,
39
- dist : path . join ( constants . pathToDist , 'plotly-' + name + '.js' ) ,
40
- distMin : path . join ( constants . pathToDist , 'plotly-' + name + '.min.js' )
68
+ name : out ,
69
+ index : path . join ( constants . pathToLib , 'index-' + out + '.js' )
41
70
} ;
42
71
72
+ if ( unminified ) {
73
+ opts . dist = path . join ( constants . pathToDist , 'plotly-' + out + '.js' ) ;
74
+ } else {
75
+ opts . distMin = path . join ( constants . pathToDist , 'plotly-' + out + '.min.js' ) ;
76
+ }
77
+
43
78
console . log ( opts ) ;
44
79
80
+ opts . calendars = true ;
81
+ opts . deleteIndex = true ;
82
+
45
83
var tasks = [ ] ;
46
84
47
85
partialBundle ( tasks , opts ) ;
@@ -55,43 +93,55 @@ if(argv.length > 2) {
55
93
function partialBundle ( tasks , opts ) {
56
94
var name = opts . name ;
57
95
var index = opts . index ;
96
+ var deleteIndex = opts . deleteIndex ;
58
97
var dist = opts . dist ;
59
98
var distMin = opts . distMin ;
60
99
var traceList = opts . traceList ;
100
+ var transformList = opts . transformList ;
101
+ var calendars = opts . calendars ;
61
102
62
103
tasks . push ( function ( done ) {
63
104
var partialIndex = mainIndex ;
64
- allTraces . forEach ( function ( trace ) {
65
- if ( traceList . indexOf ( trace ) === - 1 ) {
66
- var WHITESPACE_BEFORE = '\\s*' ;
67
- // remove require
68
- var newCode = partialIndex . replace (
69
- new RegExp (
70
- WHITESPACE_BEFORE +
71
- 'require\\(\'\\./' + trace + '\'\\),' ,
72
- 'g' ) , ''
73
- ) ;
74
-
75
- // test removal
76
- if ( newCode === partialIndex ) throw 'Unable to find and drop require for trace: "' + trace + '"' ;
77
-
78
- partialIndex = newCode ;
105
+
106
+ var all = [ 'calendars' ] . concat ( allTransforms ) . concat ( allTraces ) ;
107
+ var includes = ( calendars ? [ 'calendars' ] : [ ] ) . concat ( transformList ) . concat ( traceList ) ;
108
+ var excludes = all . filter ( function ( e ) { return includes . indexOf ( e ) === - 1 ; } ) ;
109
+
110
+ excludes . forEach ( function ( t ) {
111
+ var WHITESPACE_BEFORE = '\\s*' ;
112
+ // remove require
113
+ var newCode = partialIndex . replace (
114
+ new RegExp (
115
+ WHITESPACE_BEFORE +
116
+ 'require\\(\'\\./' + t + '\'\\),' ,
117
+ 'g' ) , ''
118
+ ) ;
119
+
120
+ // test removal
121
+ if ( newCode === partialIndex ) {
122
+ console . error ( 'Unable to find and drop require for ' + t ) ;
123
+ throw 'Error generating index for partial bundle!' ;
79
124
}
125
+
126
+ partialIndex = newCode ;
80
127
} ) ;
81
128
82
129
common . writeFile ( index , partialIndex , done ) ;
83
130
} ) ;
84
131
85
132
tasks . push ( function ( done ) {
86
- _bundle ( index , dist , {
133
+ var bundleOpts = {
87
134
standalone : 'Plotly' ,
135
+ deleteIndex : deleteIndex ,
88
136
pathToMinBundle : distMin
89
- } , function ( ) {
137
+ } ;
138
+
139
+ _bundle ( index , dist , bundleOpts , function ( ) {
90
140
var headerDist = header . replace ( 'plotly.js' , 'plotly.js (' + name + ')' ) ;
91
141
var headerDistMin = header . replace ( 'plotly.js' , 'plotly.js (' + name + ' - minified)' ) ;
92
142
93
- prependFile ( dist , headerDist , common . throwOnError ) ;
94
- prependFile ( distMin , headerDistMin , common . throwOnError ) ;
143
+ if ( dist ) prependFile ( dist , headerDist , common . throwOnError ) ;
144
+ if ( distMin ) prependFile ( distMin , headerDistMin , common . throwOnError ) ;
95
145
96
146
done ( ) ;
97
147
} ) ;
0 commit comments