@@ -38,25 +38,30 @@ const PREAMBLE = `/**
38
38
* =============================================================================
39
39
*/` ;
40
40
41
- function config ( { plugins = [ ] , output = { } , external = [ ] , visualize = false } ) {
41
+ function config ( {
42
+ plugins = [ ] ,
43
+ output = { } ,
44
+ external = [ ] ,
45
+ visualize = false ,
46
+ tsCompilerOptions = { }
47
+ } ) {
42
48
if ( visualize ) {
43
49
const filename = output . file + '.html' ;
44
- plugins . push ( visualizer ( {
45
- sourcemap : true ,
46
- filename,
47
- template : 'sunburst' ,
48
- gzipSize : true ,
49
- } ) ) ;
50
+ plugins . push ( visualizer (
51
+ { sourcemap : true , filename, template : 'sunburst' , gzipSize : true } ) ) ;
50
52
console . log ( `Will output a bundle visualization in ${ filename } ` ) ;
51
53
}
54
+
55
+ const defaultTsOptions = {
56
+ include : [ 'src/**/*.ts' ] ,
57
+ module : 'ES2015' ,
58
+ } ;
59
+ const tsoptions = Object . assign ( { } , defaultTsOptions , tsCompilerOptions ) ;
60
+
52
61
return {
53
62
input : 'src/index.ts' ,
54
63
plugins : [
55
- typescript ( {
56
- include : [ 'src/**/*.ts' ] ,
57
- module : 'ES2015' ,
58
- } ) ,
59
- resolve ( ) ,
64
+ typescript ( tsoptions ) , resolve ( ) ,
60
65
// Polyfill require() from dependencies.
61
66
commonjs ( {
62
67
ignore : [ 'crypto' ] ,
@@ -91,39 +96,76 @@ function config({plugins = [], output = {}, external = [], visualize = false}) {
91
96
module . exports = cmdOptions => {
92
97
const bundles = [ ] ;
93
98
94
- if ( ! cmdOptions . ci ) {
95
- // tf-backend-cpu.js
96
- bundles . push ( config ( {
97
- output : {
98
- format : 'umd' ,
99
- name : 'tf' ,
100
- extend : true ,
101
- file : 'dist/tf-backend-cpu.js' ,
102
- }
103
- } ) ) ;
104
- }
99
+ const terserPlugin = terser ( { output : { preamble : PREAMBLE , comments : false } } ) ;
100
+ const name = 'tf' ;
101
+ const extend = true ;
102
+ const browserFormat = 'umd' ;
103
+ const fileName = 'tf-backend-cpu' ;
105
104
106
- // tf-backend-cpu.min.js
105
+ // Node
107
106
bundles . push ( config ( {
108
- plugins : [ terser ( { output : { preamble : PREAMBLE , comments : false } } ) ] ,
109
107
output : {
110
- format : 'umd' ,
111
- name : 'tf' ,
112
- extend : true ,
113
- file : 'dist/tf-backend-cpu.min.js' ,
108
+ format : 'cjs' ,
109
+ name,
110
+ extend,
111
+ file : `dist/${ fileName } .node.js` ,
112
+ freeze : false
114
113
} ,
115
- visualize : cmdOptions . visualize
114
+ tsCompilerOptions : { target : 'es5' }
116
115
} ) ) ;
117
116
118
- if ( ! cmdOptions . ci ) {
119
- // tf-backend-cpu.esm.js
117
+ if ( cmdOptions . ci || cmdOptions . npm ) {
118
+ // Browser default minified (ES5)
120
119
bundles . push ( config ( {
121
- plugins : [ terser ( { output : { preamble : PREAMBLE , comments : false } } ) ] ,
120
+ plugins : [ terserPlugin ] ,
122
121
output : {
123
- format : 'es' ,
124
- file : 'dist/tf-backend-cpu.esm.js' ,
125
- }
122
+ format : browserFormat ,
123
+ name,
124
+ extend,
125
+ file : `dist/${ fileName } .min.js` ,
126
+ freeze : false
127
+ } ,
128
+ tsCompilerOptions : { target : 'es5' } ,
129
+ visualize : cmdOptions . visualize
130
+ } ) ) ;
131
+ }
132
+
133
+ if ( cmdOptions . npm ) {
134
+ // Browser default unminified (ES5)
135
+ bundles . push ( config ( {
136
+ output : {
137
+ format : browserFormat ,
138
+ name,
139
+ extend,
140
+ file : `dist/${ fileName } .js` ,
141
+ freeze : false
142
+ } ,
143
+ tsCompilerOptions : { target : 'es5' }
144
+ } ) ) ;
145
+
146
+ // Browser ES2017
147
+ bundles . push ( config ( {
148
+ output : {
149
+ format : browserFormat ,
150
+ name,
151
+ extend,
152
+ file : `dist/${ fileName } .es2017.js`
153
+ } ,
154
+ tsCompilerOptions : { target : 'es2017' }
155
+ } ) ) ;
156
+
157
+ // Browser ES2017 minified
158
+ bundles . push ( config ( {
159
+ plugins : [ terserPlugin ] ,
160
+ output : {
161
+ format : browserFormat ,
162
+ name,
163
+ extend,
164
+ file : `dist/${ fileName } .es2017.min.js`
165
+ } ,
166
+ tsCompilerOptions : { target : 'es2017' }
126
167
} ) ) ;
127
168
}
169
+
128
170
return bundles ;
129
171
} ;
0 commit comments