|
15 | 15 | * =============================================================================
|
16 | 16 | */
|
17 | 17 |
|
18 |
| -import node from 'rollup-plugin-node-resolve'; |
19 |
| -import typescript from 'rollup-plugin-typescript2'; |
20 |
| -import uglify from 'rollup-plugin-uglify'; |
| 18 | +import commonjs from '@rollup/plugin-commonjs'; |
| 19 | +import resolve from '@rollup/plugin-node-resolve'; |
| 20 | +import typescript from '@rollup/plugin-typescript'; |
| 21 | +import {terser} from 'rollup-plugin-terser'; |
| 22 | +import visualizer from 'rollup-plugin-visualizer'; |
21 | 23 |
|
22 | 24 | const PREAMBLE = `/**
|
23 |
| - * @license |
24 |
| - * Copyright ${(new Date).getFullYear()} Google LLC. All Rights Reserved. |
25 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
26 |
| - * you may not use this file except in compliance with the License. |
27 |
| - * You may obtain a copy of the License at |
28 |
| - * |
29 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
30 |
| - * |
31 |
| - * Unless required by applicable law or agreed to in writing, software |
32 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
33 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
34 |
| - * See the License for the specific language governing permissions and |
35 |
| - * limitations under the License. |
36 |
| - * ============================================================================= |
37 |
| - */`; |
| 25 | + * @license |
| 26 | + * Copyright ${(new Date).getFullYear()} Google LLC. All Rights Reserved. |
| 27 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 28 | + * you may not use this file except in compliance with the License. |
| 29 | + * You may obtain a copy of the License at |
| 30 | + * |
| 31 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 32 | + * |
| 33 | + * Unless required by applicable law or agreed to in writing, software |
| 34 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 35 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 36 | + * See the License for the specific language governing permissions and |
| 37 | + * limitations under the License. |
| 38 | + * ============================================================================= |
| 39 | + */`; |
38 | 40 |
|
39 |
| -function minify() { |
40 |
| - return uglify({ |
41 |
| - output: { |
42 |
| - preamble: PREAMBLE, |
43 |
| - } |
44 |
| - }); |
45 |
| -} |
| 41 | +function config({ |
| 42 | + plugins = [], |
| 43 | + output = {}, |
| 44 | + external = [], |
| 45 | + visualize = false, |
| 46 | + tsCompilerOptions = {} |
| 47 | +}) { |
| 48 | + if (visualize) { |
| 49 | + const filename = output.file + '.html'; |
| 50 | + plugins.push(visualizer({sourcemap: true, filename})); |
| 51 | + console.log(`Will output a bundle visualization in ${filename}`); |
| 52 | + } |
| 53 | + |
| 54 | + const defaultTsOptions = { |
| 55 | + include: ['src/**/*.ts'], |
| 56 | + module: 'ES2015', |
| 57 | + }; |
| 58 | + const tsoptions = Object.assign({}, defaultTsOptions, tsCompilerOptions); |
46 | 59 |
|
47 |
| -function config({plugins = [], output = {}}) { |
48 | 60 | return {
|
49 | 61 | input: 'src/index.ts',
|
50 | 62 | plugins: [
|
51 |
| - typescript({ |
52 |
| - tsconfigOverride: { |
53 |
| - compilerOptions: {module: 'ES2015'}, |
| 63 | + typescript(tsoptions), resolve(), |
| 64 | + // Polyfill require() from dependencies. |
| 65 | + commonjs({ |
| 66 | + ignore: ['crypto', 'node-fetch', 'util'], |
| 67 | + include: 'node_modules/**', |
| 68 | + namedExports: { |
| 69 | + './node_modules/seedrandom/index.js': ['alea'], |
54 | 70 | },
|
55 | 71 | }),
|
56 |
| - node(), ...plugins |
| 72 | + ...plugins |
57 | 73 | ],
|
58 | 74 | output: {
|
59 | 75 | banner: PREAMBLE,
|
60 |
| - globals: { |
61 |
| - '@tensorflow/tfjs-core': 'tf', |
62 |
| - '@tensorflow/tfjs-converter': 'tf', |
63 |
| - }, |
64 |
| - ...output |
| 76 | + sourcemap: true, |
| 77 | + ...output, |
65 | 78 | },
|
66 |
| - external: [ |
67 |
| - '@tensorflow/tfjs-core', |
68 |
| - '@tensorflow/tfjs-converter', |
69 |
| - ] |
| 79 | + external: |
| 80 | + ['@tensorflow/tfjs-core', '@tensorflow/tfjs-converter', , ...external], |
| 81 | + onwarn: warning => { |
| 82 | + let {code} = warning; |
| 83 | + if (code === 'CIRCULAR_DEPENDENCY' || code === 'CIRCULAR' || |
| 84 | + code === 'THIS_IS_UNDEFINED') { |
| 85 | + return; |
| 86 | + } |
| 87 | + console.warn('WARNING: ', warning.toString()); |
| 88 | + } |
70 | 89 | };
|
71 | 90 | }
|
72 | 91 |
|
73 |
| -export default [ |
74 |
| - config({ |
75 |
| - output: { |
76 |
| - format: 'umd', |
77 |
| - name: 'cocoSsd', |
78 |
| - file: 'dist/coco-ssd.js', |
79 |
| - } |
80 |
| - }), |
81 |
| - config({ |
82 |
| - plugins: [minify()], |
83 |
| - output: { |
84 |
| - format: 'umd', |
85 |
| - name: 'cocoSsd', |
86 |
| - file: 'dist/coco-ssd.min.js', |
87 |
| - } |
88 |
| - }), |
89 |
| - config({ |
90 |
| - plugins: [minify()], |
| 92 | +module.exports = cmdOptions => { |
| 93 | + const bundles = []; |
| 94 | + |
| 95 | + const terserPlugin = terser({output: {preamble: PREAMBLE, comments: false}}); |
| 96 | + const name = 'cocoSsd'; |
| 97 | + const extend = true; |
| 98 | + const browserFormat = 'umd'; |
| 99 | + const fileName = 'coco-ssd'; |
| 100 | + |
| 101 | + // Node |
| 102 | + bundles.push(config({ |
91 | 103 | output: {
|
92 |
| - format: 'es', |
93 |
| - file: 'dist/coco-ssd.esm.js', |
94 |
| - } |
95 |
| - }) |
96 |
| -]; |
| 104 | + format: 'cjs', |
| 105 | + name, |
| 106 | + extend, |
| 107 | + file: `dist/${fileName}.node.js`, |
| 108 | + freeze: false |
| 109 | + }, |
| 110 | + tsCompilerOptions: {target: 'es5'} |
| 111 | + })); |
| 112 | + |
| 113 | + if (cmdOptions.ci || cmdOptions.npm) { |
| 114 | + // Browser default minified (ES5) |
| 115 | + bundles.push(config({ |
| 116 | + plugins: [terserPlugin], |
| 117 | + output: { |
| 118 | + format: browserFormat, |
| 119 | + name, |
| 120 | + extend, |
| 121 | + file: `dist/${fileName}.min.js`, |
| 122 | + freeze: false |
| 123 | + }, |
| 124 | + tsCompilerOptions: {target: 'es5'}, |
| 125 | + visualize: cmdOptions.visualize |
| 126 | + })); |
| 127 | + } |
| 128 | + |
| 129 | + if (cmdOptions.npm) { |
| 130 | + // Browser default unminified (ES5) |
| 131 | + bundles.push(config({ |
| 132 | + output: { |
| 133 | + format: browserFormat, |
| 134 | + name, |
| 135 | + extend, |
| 136 | + file: `dist/${fileName}.js`, |
| 137 | + freeze: false |
| 138 | + }, |
| 139 | + tsCompilerOptions: {target: 'es5'} |
| 140 | + })); |
| 141 | + |
| 142 | + // Browser ES2017 |
| 143 | + bundles.push(config({ |
| 144 | + output: { |
| 145 | + format: browserFormat, |
| 146 | + name, |
| 147 | + extend, |
| 148 | + file: `dist/${fileName}.es2017.js` |
| 149 | + }, |
| 150 | + tsCompilerOptions: {target: 'es2017'} |
| 151 | + })); |
| 152 | + |
| 153 | + // Browser ES2017 minified |
| 154 | + bundles.push(config({ |
| 155 | + plugins: [terserPlugin], |
| 156 | + output: { |
| 157 | + format: browserFormat, |
| 158 | + name, |
| 159 | + extend, |
| 160 | + file: `dist/${fileName}.es2017.min.js` |
| 161 | + }, |
| 162 | + tsCompilerOptions: {target: 'es2017'} |
| 163 | + })); |
| 164 | + } |
| 165 | + |
| 166 | + return bundles; |
| 167 | +}; |
0 commit comments