-
Notifications
You must be signed in to change notification settings - Fork 1
/
rollup.config.js
305 lines (286 loc) · 11.5 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import nodeResolve from '@rollup/plugin-node-resolve';
import typescript from "@rollup/plugin-typescript";
import replace from '@rollup/plugin-replace';
import size from 'rollup-plugin-size';
import eslint from '@rollup/plugin-eslint';
import alias from "@rollup/plugin-alias";
import globals from 'rollup-plugin-node-globals';
import inject from "@rollup/plugin-inject";
import { visualizer } from 'rollup-plugin-visualizer';
import { terser } from 'rollup-plugin-terser';
import pkg from './package.json';
export function emitModulePackageFile() {
return {
name: 'emit-module-package-file',
generateBundle() {
this.emitFile({ type: 'asset', fileName: 'package.json', source: `{"type":"module"}` });
}
};
}
const commitHash = (function () {
try {
return fs.readFileSync('.commithash', 'utf-8');
} catch (err) {
return 'unknown';
}
})();
const prodBuild = process.env.prodbuild || false;
console.log("Prod build: ", prodBuild);
const now = new Date(
process.env.SOURCE_DATE_EPOCH ? process.env.SOURCE_DATE_EPOCH * 1000 : new Date().getTime()
).toUTCString();
const banner = `/*
@license
FeedsSDK.js v${pkg.version}
${now} - commit ${commitHash}
Released under the MIT License.
*/`;
const onwarn = warning => {
// eslint-disable-next-line no-console
if (warning.code && warning.code === "CIRCULAR_DEPENDENCY" && warning.importer.indexOf('node_modules') < 0 && warning.importer.indexOf("internals.ts") >= 0)
return; // TMP: don't get flooded by our "internals" circular dependencies for now
if (warning.code && warning.code === "THIS_IS_UNDEFINED")
return; // TMP: don't get flooded by this for now
if (warning.code && warning.code === "EVAL")
return; // TMP: don't get flooded by this for now
console.warn("Rollup build warning:", warning);
};
const treeshake = {
moduleSideEffects: false,
propertyReadSideEffects: false,
tryCatchDeoptimization: false
};
const nodePlugins = [
nodeResolve({
preferBuiltins: true
}),
json({}),
replace({
delimiters: ['', ''],
preventAssignment: true,
exclude: [
'/node_modules/rollup-plugin-node-polyfills/**/*.js',
'/node_modules/rollup-plugin-polyfill-node/**/*.js',
],
values: {
// Replace readable-stream with stream (polyfilled) because it uses dynamic requires and this doesn't work well at runtime
// even if trying to add "readable-stream" to "dynamicRequireTargets" in commonJs().
// https://github.com/rollup/rollup/issues/1507#issuecomment-340550539
'require(\'readable-stream\')': 'require(\'stream\')',
'require("readable-stream")': 'require("stream")',
'require(\'readable-stream/writable\')': 'require(\'stream\').Writable',
'require("readable-stream/writable")': 'require("stream").Writable',
'require(\'readable-stream/readable\')': 'require(\'stream\').Readable',
'require("readable-stream/readable")': 'require("stream").Readable',
'LegacyTransportStream = require(\'./legacy\')': 'LegacyTransportStream = null',
'LegacyTransportStream = require(\'winston-transport/legacy\')': 'LegacyTransportStream = null'
}
}),
commonjs({}),
typescript({
sourceMap: !prodBuild,
exclude: "*.browser.ts"
}),
...prodBuild ? [
terser()
] : [],
size()
];
/**
* main building routine here
*/
const rollupSourceFile = 'src/index.ts';
export default command => {
//const { collectLicenses, writeLicense } = getLicenseHandler();
const commonJSBuild = {
input: {
'feeds-sdk.js': rollupSourceFile
},
onwarn,
plugins: [
...nodePlugins,
//!command.configTest && collectLicenses()
eslint()
],
// fsevents is a dependency of chokidar that cannot be bundled as it contains binary code
external: [
'spark-md5',
'@elastosfoundation/did-js-sdk',
'@elastosfoundation/hive-js-sdk'
],
treeshake,
strictDeprecations: true,
output: {
//banner,
chunkFileNames: 'shared/[name].js',
dir: 'dist',
banner,
entryFileNames: '[name]',
externalLiveBindings: true,
format: 'cjs',
freeze: false,
// TODO: delete occurences of fsevents - not used in did sdk
interop: id => {
if (id === 'fsevents') {
return 'defaultOnly';
}
return 'default';
},
manualChunks: { did: [rollupSourceFile] },
sourcemap: !prodBuild
}
};
if (command.configTest) {
return commonJSBuild;
}
const esmBuild = {
...commonJSBuild,
input: { 'feeds-sdk.js': rollupSourceFile },
plugins: [
...nodePlugins,
emitModulePackageFile(),
//collectLicenses()
],
external: [
'spark-md5',
'@elastosfoundation/did-js-sdk',
'@elastosfoundation/hive-js-sdk'
],
output: {
...commonJSBuild.output,
dir: 'dist/es',
format: 'es',
banner,
sourcemap: !prodBuild,
minifyInternalExports: false
}
};
const browserBuilds = {
input: rollupSourceFile,
onwarn,
external: [
'spark-md5',
'@elastosfoundation/did-js-sdk',
'@elastosfoundation/hive-js-sdk'
],
plugins: [
// IMPORTANT: DON'T CHANGE THE ORDER OF THINGS BELOW TOO MUCH! OTHERWISE YOU'LL GET
// GOOD HEADACHES WITH RESOLVE ERROR, UNEXPORTED CLASSES AND SO ON...
json(),
// Replace fs with browser implementation.
replace({
delimiters: ['', ''],
preventAssignment: true,
include: [
'src/utils/storage/file.ts'
],
values: {
'fs from "./fs"' : 'fs from "./fs.browser.ts"'
}
}),
// Dirty circular dependency removal atttempt
replace({
delimiters: ['', ''],
preventAssignment: true,
include: [
'node_modules/assert/build/internal/errors.js'
],
values: {
'require(\'../assert\')': 'null',
}
}),
// Dirty hack to remove circular deps between brorand and crypto-browserify as in browser,
// brorand doesn't use 'crypto' even if its source code includes it.
replace({
delimiters: ['', ''],
preventAssignment: true,
include: [
'node_modules/brorand/**/*.js'
],
values: {
'require(\'crypto\')': 'null',
}
}),
// Circular dependencies tips: https://github.com/rollup/rollup/issues/3816
replace({
delimiters: ['', ''],
preventAssignment: true,
values: {
// Replace readable-stream with stream (polyfilled) because it uses dynamic requires and this doesn't work well at runtime
// even if trying to add "readable-stream" to "dynamicRequireTargets" in commonJs().
// https://github.com/rollup/rollup/issues/1507#issuecomment-340550539
'require(\'readable-stream\')': 'require(\'stream\')',
'require("readable-stream")': 'require("stream")',
'require(\'readable-stream/writable\')': 'require(\'stream\').Writable',
'require("readable-stream/writable")': 'require("stream").Writable',
'require(\'readable-stream/readable\')': 'require(\'stream\').Readable',
'require("readable-stream/readable")': 'require("stream").Readable',
'LegacyTransportStream = require(\'./legacy\')': 'LegacyTransportStream = null',
'LegacyTransportStream = require(\'winston-transport/legacy\')': 'LegacyTransportStream = null'
}
}),
alias({
"entries": [
{ "find": "buffer", "replacement": "buffer-es6" },
{ "find": "process", "replacement": "process-es6" },
//{ "find": "fs", "replacement": "browserfs/dist/shims/fs" },
{ "find": "path", "replacement": "path-browserify" },
{ "find": "crypto", "replacement": "crypto-browserify" },
{ "find": "util/", "replacement": "node_modules/util/util.js" },
{ "find": "util", "replacement": "node_modules/util/util.js" },
{ "find": "stream", "replacement": "./src/utils/browser/stream.js" },
{ "find": "string_decoder/", "replacement": "node_modules/string_decoder/lib/string_decoder.js" },
{ "find": "string_decoder", "replacement": "node_modules/string_decoder/lib/string_decoder.js" },
{ "find": "events", "replacement": "node_modules/events/events.js" },
{ "find": "assert", "replacement": "node_modules/assert/build/assert.js" }
]
}),
nodeResolve({
mainFields: ['browser', 'module', 'jsnext:main', 'main'],
browser: true,
preferBuiltins: true,
dedupe: ['bn.js', 'browserfs', 'buffer-es6', 'process-es6', 'crypto-browserify', 'assert', 'events', 'browserify-sign']
}),
// Polyfills needed to replace readable-stream with stream (circular dep)
commonjs({
esmExternals: true,
//requireReturnsDefault: "true", // "true" will generate build error: TypeError: Cannot read property 'deoptimizePath' of undefined
//requireReturnsDefault: "auto", // namespace, true, false, auto, preferred
transformMixedEsModules: true, // TMP trying to solve commonjs "circular dependency" errors at runtime
dynamicRequireTargets: [],
}),
globals(), // Defines process, Buffer, etc
typescript({
exclude: "*.node.ts"
}),
/* nodePolyfills({
stream: true
// crypto:true // Broken, the polyfill just doesn't work. We have to use crypto-browserify directly in our TS code instead.
}), */ // To let some modules bundle NodeJS stream, util, fs ... in browser
inject({
"BrowserFS": "browserfs"
}),
size(),
...prodBuild ? [
terser()
] : [],
visualizer({
filename: "./browser-bundle-stats.html"
}) // To visualize bundle dependencies sizes on a UI.
// LATER terser({ module: true, output: { comments: 'some' } })
],
treeshake,
strictDeprecations: true,
output: [
{
file: 'dist/es/feeds-sdk.browser.js',
format: 'es',
banner,
sourcemap: !prodBuild,
},
]
};
return [ commonJSBuild, esmBuild, browserBuilds];
};