UNPKG

5.95 kBJavaScriptView Raw
1/**
2 * @license
3 * Copyright 2018 Google LLC. All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 * =============================================================================
16 */
17
18import commonjs from '@rollup/plugin-commonjs';
19import resolve from '@rollup/plugin-node-resolve';
20import typescript from '@rollup/plugin-typescript';
21import babel from '@rollup/plugin-babel';
22import {terser} from 'rollup-plugin-terser';
23import {visualizer} from 'rollup-plugin-visualizer';
24
25const PREAMBLE = `/**
26 * @license
27 * Copyright ${(new Date).getFullYear()} Google LLC. All Rights Reserved.
28 * Licensed under the Apache License, Version 2.0 (the "License");
29 * you may not use this file except in compliance with the License.
30 * You may obtain a copy of the License at
31 *
32 * http://www.apache.org/licenses/LICENSE-2.0
33 *
34 * Unless required by applicable law or agreed to in writing, software
35 * distributed under the License is distributed on an "AS IS" BASIS,
36 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 * See the License for the specific language governing permissions and
38 * limitations under the License.
39 * =============================================================================
40 */`;
41
42function config({
43 plugins = [],
44 output = {},
45 external = [],
46 visualize = false,
47 tsCompilerOptions = {},
48 entry = 'src/index.ts'
49}) {
50 if (visualize) {
51 const filename = output.file + '.html';
52 plugins.push(visualizer({sourcemap: true, filename}));
53 console.log(`Will output a bundle visualization in ${filename}`);
54 }
55
56 const defaultTsOptions = {
57 include: ['src/**/*.ts'],
58 module: 'ES2015',
59 };
60 const tsoptions = Object.assign({}, defaultTsOptions, tsCompilerOptions);
61
62 return {
63 input: entry,
64 // Do not tree shake union package library
65 treeshake: false,
66 plugins: [
67 typescript(tsoptions),
68 resolve({dedupe: ['seedrandom', 'long']}),
69 commonjs({
70 ignore: ['crypto', 'fs', 'node-fetch', 'string_decoder', 'util'],
71 include: 'node_modules/**',
72 }),
73 ...plugins,
74 ],
75 output: {
76 banner: PREAMBLE,
77 globals: {
78 'node-fetch': 'nodeFetch',
79 },
80 sourcemap: true,
81 ...output
82 },
83 external: [
84 // node-fetch is only used in node. Browsers have native "fetch".
85 'node-fetch', 'crypto', ...external
86 ],
87 onwarn: warning => {
88 let {code} = warning;
89 if (code === 'CIRCULAR_DEPENDENCY' || code === 'CIRCULAR' ||
90 code === 'THIS_IS_UNDEFINED') {
91 return;
92 }
93 console.warn('WARNING: ', warning.toString());
94 }
95 };
96}
97
98export default function(cmdOptions) {
99 const bundles = [];
100
101 const babelPlugin = babel({
102 babelrc: false,
103 babelHelpers: 'bundled',
104 presets: [
105 // ensure we get es5 by adding IE 11 as a target
106 [
107 '@babel/env', {
108 modules: false,
109 useBuiltIns: 'entry',
110 corejs: '3.29.1',
111 targets: {'ie': '11'},
112 }
113 ]
114 ]
115 });
116
117 const terserPlugin = terser({output: {preamble: PREAMBLE, comments: false}});
118 const name = 'tf';
119 const extend = true;
120 const umdFormat = 'umd';
121 const fesmFormat = 'es';
122 const fileName = 'tf';
123
124 // Node
125 bundles.push(config({
126 output: {
127 format: 'cjs',
128 name,
129 extend,
130 file: `dist/${fileName}.node.js`,
131 freeze: false
132 },
133 tsCompilerOptions: {target: 'es5'},
134 external: [
135 'long',
136 '@tensorflow/tfjs-core',
137 '@tensorflow/tfjs-layers',
138 '@tensorflow/tfjs-converter',
139 '@tensorflow/tfjs-data',
140 '@tensorflow/tfjs-backend-cpu',
141 '@tensorflow/tfjs-backend-webgl',
142 ]
143 }));
144
145 if (cmdOptions.ci || cmdOptions.npm) {
146 // UMD ES5
147 bundles.push(config({
148 entry: 'src/index_with_polyfills.ts',
149 plugins: [babelPlugin, terserPlugin],
150 output: {
151 format: umdFormat,
152 name,
153 extend,
154 file: `dist/${fileName}.min.js`,
155 freeze: false
156 },
157 tsCompilerOptions: {target: 'es5'},
158 visualize: cmdOptions.visualize
159 }));
160 }
161
162 if (cmdOptions.npm) {
163 // UMD ES5
164 bundles.push(config({
165 entry: 'src/index_with_polyfills.ts',
166 plugins: [babelPlugin],
167 output: {
168 format: umdFormat,
169 name,
170 extend,
171 file: `dist/${fileName}.js`,
172 freeze: false
173 },
174 tsCompilerOptions: {target: 'es5'}
175 }));
176
177 // UMD ES2017
178 bundles.push(config({
179 output:
180 {format: umdFormat, name, extend, file: `dist/${fileName}.es2017.js`},
181 tsCompilerOptions: {target: 'es2017'}
182 }));
183
184 // UMD ES2017 minified
185 bundles.push(config({
186 plugins: [terserPlugin],
187 output: {
188 format: umdFormat,
189 name,
190 extend,
191 file: `dist/${fileName}.es2017.min.js`
192 },
193 tsCompilerOptions: {target: 'es2017'},
194 visualize: cmdOptions.visualize
195 }));
196
197 // FESM ES2017
198 bundles.push(config({
199 output:
200 {format: fesmFormat, name, extend, file: `dist/${fileName}.fesm.js`},
201 tsCompilerOptions: {target: 'es2017'}
202 }));
203
204 // FESM ES2017 minified
205 bundles.push(config({
206 plugins: [terserPlugin],
207 output: {
208 format: fesmFormat,
209 name,
210 extend,
211 file: `dist/${fileName}.fesm.min.js`
212 },
213 tsCompilerOptions: {target: 'es2017'},
214 visualize: cmdOptions.visualize
215 }));
216 }
217
218 return bundles;
219};