forked from nenjotsu/technicalindicators
-
Notifications
You must be signed in to change notification settings - Fork 3
/
rollup.js
190 lines (176 loc) · 6.25 KB
/
rollup.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
185
186
187
188
189
190
import babel from "@rollup/plugin-babel";
import minify from "@rollup/plugin-terser";
import { rollup } from "rollup";
import commonjs from "rollup-plugin-commonjs";
import builtins from "rollup-plugin-node-builtins";
import resolve from "rollup-plugin-node-resolve";
import replace from "rollup-plugin-replace";
var declarations = "";
var fs = require("fs");
async function doBuild() {
try {
let bundle = await rollup({
input: "index.js",
plugins: [
replace({
"process.env.NODE_ENV": JSON.stringify("production"),
}),
builtins(),
resolve({
browser: true,
mainFields: ["main", "jsnext:main", "browser"],
}),
commonjs({}),
babel({
babelrc: false,
presets: [
[
"@babel/preset-env",
{
modules: false,
},
],
],
plugins: ["@babel/plugin-external-helpers"],
babelHelpers: "external",
}),
minify({}),
],
external: ["@babel/polyfill"],
strictDeprecations: true,
});
await bundle.write({
banner: "/* APP */",
file: "dist/browser.js",
name: "window",
format: "iife",
globals: {
"@babel/polyfill": "window",
},
});
function definitionGenerator() {
return {
name: "definition-generator", // this name will show up in warnings and errors
resolveId(importee) {
if (importee === "definition-generator") {
return importee; // this signals that rollup should not ask other plugins or check the file system to find this id
}
return null; // other ids should be handled as usually
},
load(id) {
if (id.indexOf("lib") != -1) {
id = id.split("lib").join("declarations");
id = id.split(".js").join(".d.ts");
}
if (fs.existsSync(id)) {
var declaration = fs.readFileSync(id).toString();
var lines = declaration.split("\n");
lines = lines.map((line) => {
var replaced = line.replace(
"export declare",
"declare"
);
replaced = replaced.replace("export default", "");
return replaced;
});
lines = lines.filter((line) => {
return (
line.indexOf("import") != 0 &&
line.indexOf("export") != 0
);
});
declarations = declarations + lines.join("\n");
}
return null; // other ids should be handled as usually
},
};
}
let customBundle = await rollup({
input: "custom.js",
plugins: [
definitionGenerator(),
replace({
"process.env.NODE_ENV": JSON.stringify("production"),
}),
builtins(),
resolve({
browser: true,
mainFields: ["main", "jsnext:main", "browser"],
}),
commonjs({}),
minify({}),
],
external: ["@babel/polyfill"],
strictDeprecations: true,
});
await customBundle.write({
banner: "/* APP */",
file: "dist/custom.js",
name: "window",
format: "iife",
globals: {
"@babel/polyfill": "window",
},
});
fs.writeFileSync("dist/custom.d.ts", declarations);
let bundleES6 = await rollup({
input: "index.js",
plugins: [
replace({
"process.env.NODE_ENV": JSON.stringify("production"),
}),
builtins(),
resolve({
browser: true,
mainFields: ["main", "jsnext:main", "browser"],
}),
commonjs({}),
minify({}),
],
external: ["@babel/polyfill"],
strictDeprecations: true,
});
await bundleES6.write({
banner: "/* APP */",
file: "dist/browser.es6.js",
name: "window",
format: "iife",
globals: {
"@babel/polyfill": "window",
},
});
let bundleNode = await rollup({
input: "index.js",
external: ["@babel/polyfill"],
strictDeprecations: true,
});
await bundleNode.write({
banner: "/* APP */",
file: "dist/index.js",
format: "cjs",
sourcemap: true,
});
} catch (e) {
console.error(e);
console.log(e.message);
}
}
doBuild().then(() => {
// var data = fs.readFileSync('dist/browser.js')
// var fd = fs.openSync('dist/browser.js', 'w+')
// var insert = Buffer.from("var tf = window.tf || {};")
// fs.writeSync(fd, insert, 0, insert.length, 0)
// fs.writeSync(fd, data, 0, data.length, insert.length)
// fs.close(fd, (err) => {
// if (err) throw err;
// });
// var data = fs.readFileSync('dist/browser.es6.js')
// var fd = fs.openSync('dist/browser.es6.js', 'w+')
// var insert = Buffer.from("var tf = window.tf || {};")
// fs.writeSync(fd, insert, 0, insert.length, 0)
// fs.writeSync(fd, data, 0, data.length, insert.length)
// fs.close(fd, (err) => {
// if (err) throw err;
// });
console.log("Completed build for node and browser");
});