-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
184 lines (184 loc) · 5.04 KB
/
rollup.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import {execSync} from 'child_process';
import fs from 'fs';
import path from 'path';
// 1st party Rollup plugins
import {createFilter} from '@rollup/pluginutils';
import {babel } from '@rollup/plugin-babel';
import {nodeResolve } from '@rollup/plugin-node-resolve';
import terser from '@rollup/plugin-terser';
// 3rd party Rollup plugins
import dts from 'rollup-plugin-dts';
import {visualizer} from 'rollup-plugin-visualizer';
import json from '@rollup/plugin-json';
//import {addTypeChecks} from "./src-transpiler/typeInserter.js";
//const {addTypeChecks} = await import("runtime-type-inspector/src-transpiler/typeInserter.js");
import commonjs from '@rollup/plugin-commonjs';
/** @typedef {import('rollup').RollupOptions} RollupOptions */
/** @typedef {import('rollup').Plugin} Plugin */
/** @typedef {import('rollup').OutputOptions} OutputOptions */
/** @typedef {import('@rollup/plugin-babel').RollupBabelInputPluginOptions} RollupBabelInputPluginOptions */
/**
* The ES5 options for babel(...) plugin.
* @returns {RollupBabelInputPluginOptions} The babel options.
*/
const es5Options = () => ({
babelHelpers: 'bundled',
babelrc: false,
comments: true,
compact: false,
minified: false,
presets: [
[
'@babel/preset-env', {
loose: !true,
modules: false,
targets: {
ie: '11'
}
}
]
]
});
/**
* The ES6 options for babel(...) plugin.
* @returns {RollupBabelInputPluginOptions} The babel options.
*/
const moduleOptions = () => ({
babelHelpers: 'bundled',
babelrc: false,
comments: true,
compact: false,
minified: false,
presets: [
[
'@babel/preset-env', {
bugfixes: true,
loose: true,
modules: false,
targets: {
esmodules: true
}
}
]
]
});
/**
* Build a target that rollup is supposed to build.
*
* @param {'debug'|'release'|'profiler'|'min'} buildType - The build type.
* @param {'es5'|'es6'} moduleFormat - The module format.
* @returns {RollupOptions} One rollup target.
*/
function buildTarget(name, rootFile, path, buildType, moduleFormat) {
const banner = {
debug: ' (DEBUG)',
release: ' (RELEASE)',
profiler: ' (PROFILE)',
min: ' (RELEASE)'
};
const outputPlugins = {
release: [],
min: [
terser()
]
};
if (process.env.treemap) {
outputPlugins.min.push(visualizer({
filename: 'treemap.html',
brotliSize: true,
gzipSize: true
}));
}
if (process.env.treenet) {
outputPlugins.min.push(visualizer({
filename: 'treenet.html',
template: 'network'
}));
}
if (process.env.treesun) {
outputPlugins.min.push(visualizer({
filename: 'treesun.html',
template: 'sunburst'
}));
}
const outputFile = {
release: `${path}`,
debug: `${path}.dbg`,
profiler: `${path}.prf`,
min: `${path}.min`,
rti: `${path}.rti`,
};
const outputExtension = {
es5: '.cjs',
es6: '.mjs'
};
/** @type {Record<string, 'umd'|'es'>} */
const outputFormat = {
es5: 'umd',
es6: 'es'
};
/** @type {OutputOptions} */
const outputOptions = {
plugins: outputPlugins[buildType || outputPlugins.release],
format: outputFormat[moduleFormat],
indent: '\t',
sourcemap: false,
name,
preserveModules: false,
file: `${outputFile[buildType]}${outputExtension[moduleFormat]}`
};
const babelOptions = {
es5: es5Options(buildType),
es6: moduleOptions()
};
return {
input: rootFile,
output: outputOptions,
plugins: [
//runtimeTypeInspector(buildType === 'debug'),
babel(babelOptions[moduleFormat]),
//nodeResolve({
// browser: true
//}),
//commonjs(),
//json(),
],
///external: ['fs', 'path', 'url', 'sharp', 'onnxruntime-node', 'onnxruntime-web', 'stream/web'],
};
}
/** @type {RollupOptions} */
const target_types = {
input: 'types/transpiler/index.d.mts',
output: [{
file: '@runtime-type-inspector/transpiler/types.d.ts',
//footer: 'export as namespace rtiTranspiler;',
format: 'es'
}],
plugins: [
dts()
]
};
export default (args) => {
/** @type {RollupOptions[]} */
let targets = [];
const envTarget = process.env.target ? process.env.target.toLowerCase() : null;
if ((envTarget === null) && fs.existsSync('build')) {
// no targets specified, clean build directory
fs.rmSync('build', {recursive: true});
}
if (envTarget === 'types') {
targets.push(target_types);
} else if (envTarget === 'extras') {
targets = targets.concat(target_extras);
} else {
['release', /*'debug', 'profiler', 'min', 'rti'*/].forEach((t) => {
['es5', 'es6'].forEach((m) => {
if (envTarget === null || envTarget === t || envTarget === m || envTarget === `${t}_${m}`) {
targets.push(buildTarget('rtiTranspiler', 'src-transpiler/index.js', '@runtime-type-inspector/transpiler/index', t, m));
targets.push(buildTarget('rtiRuntime' , 'src-runtime/index.js' , '@runtime-type-inspector/runtime/index' , t, m));
}
});
});
}
return targets;
};