-
Notifications
You must be signed in to change notification settings - Fork 566
/
rollup.js
197 lines (180 loc) · 5 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
191
192
193
194
195
196
197
import { rollup } from 'rollup';
import babel from 'rollup-plugin-babel';
import uglify from 'rollup-plugin-uglify';
import minify from 'rollup-plugin-babel-minify';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import builtins from 'rollup-plugin-node-builtins';
import replace from 'rollup-plugin-replace';
var declarations = '';
var fs = require('fs');
async function doBuild() {
try {
let bundle = await rollup({
entry: 'index.js',
plugins: [
replace({
'process.env.NODE_ENV': JSON.stringify( 'production' )
}),
builtins(),
resolve({
jsnext: true,
main: true,
browser: true
}),
commonjs({
}),
babel({
babelrc: false,
"presets": [
[
"es2015",
{
"modules": false
}
]
],
"plugins": [
"external-helpers"
]
}),
minify({
comments : false
})
],
external: ["@babel/polyfill"]
});
await bundle.write({
'banner': '/* APP */',
dest: 'dist/browser.js',
format: 'iife',
moduleName: 'window',
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({
entry: 'custom.js',
plugins: [
definitionGenerator(),
replace({
'process.env.NODE_ENV': JSON.stringify( 'production' )
}),
builtins(),
resolve({
jsnext: true,
main: true,
browser: true
}),
commonjs({
}),
minify({
comments : false
})
],
external: ["@babel/polyfill"]
});
await customBundle.write({
'banner': '/* APP */',
dest: 'dist/custom.js',
format: 'iife',
moduleName: 'window',
globals : {
"@babel/polyfill" : 'window'
}
})
fs.writeFileSync('dist/custom.d.ts', declarations);
let bundleES6 = await rollup({
entry: 'index.js',
plugins: [
replace({
'process.env.NODE_ENV': JSON.stringify( 'production' )
}),
builtins(),
resolve({
jsnext: true,
main: true,
browser: true
}),
commonjs({
}),
minify({
comments : false
})
],
external: ["@babel/polyfill"],
});
await bundleES6.write({
'banner': '/* APP */',
dest: 'dist/browser.es6.js',
format: 'iife',
moduleName: 'window',
globals : {
"@babel/polyfill" : 'window'
}
})
let bundleNode = await rollup({
entry: 'index.js',
external: ["@babel/polyfill"],
});
await bundleNode.write({
'banner': '/* APP */',
dest: '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');
});