forked from fullcalendar/fullcalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
269 lines (224 loc) · 7.43 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
const path = require('path')
const { readFileSync } = require('fs')
const glob = require('glob')
const nodeResolve = require('rollup-plugin-node-resolve')
const commonjs = require('rollup-plugin-commonjs')
const multiEntry = require('rollup-plugin-multi-entry')
const sourcemaps = require('rollup-plugin-sourcemaps')
const alias = require('rollup-plugin-alias')
const replace = require('rollup-plugin-replace')
const cleanup = require('rollup-plugin-cleanup')
const handleBars = require('handlebars')
const { pkgStructs } = require('./scripts/lib/pkg-struct')
const rootPkgJsonData = require('./package.json')
const EXTERNAL_BROWSER_GLOBALS = {
luxon: 'luxon',
rrule: 'rrule',
moment: 'moment'
}
const EXTERNAL_BROWSER_GLOBAL_HACKS = {
'moment-timezone/builds/moment-timezone-with-data': 'moment' // see moment-timezone/src/main.ts
}
const WATCH_OPTIONS = {
chokidar: { // better than default watch util. doesn't fire change events on stat changes (like last opened)
awaitWriteFinish: { // because tsc/rollup sometimes takes a long time to write and triggers two recompiles
stabilityThreshold: 500,
pollInterval: 100
}
},
clearScreen: false // let tsc do the screan clearing
}
const renderBanner = handleBars.compile(
readFileSync('packages/banner.tpl', { encoding: 'utf8' })
)
module.exports = buildConfigs()
function buildConfigs() {
let isDev = detectIsDev()
let ownBrowserGlobals = {}
for (let pkgStruct of pkgStructs) {
ownBrowserGlobals[pkgStruct.name] = pkgStruct.browserGlobal
}
return [
...buildPkgConfigs(ownBrowserGlobals, isDev),
...buildLocaleConfigs(ownBrowserGlobals),
buildTestConfig() // must be last b/c depends on built pkgs+locales
]
}
// FullCalendar Packages
// ----------------------------------------------------------------------------------------------------
function buildPkgConfigs(ownBrowserGlobals, isDev) {
return pkgStructs.map((pkgStruct) => buildPkgConfig(pkgStruct, ownBrowserGlobals, isDev))
}
function buildPkgConfig(pkgStruct, ownBrowserGlobals, isDev) {
let banner = renderBanner(pkgStruct.jsonObj)
let external = Object.keys(Object.assign(
{},
EXTERNAL_BROWSER_GLOBAL_HACKS, // apply to all because didn't have per-pkg data from package.json's
ownBrowserGlobals,
pkgStruct.jsonObj.dependencies || {},
pkgStruct.jsonObj.peerDependencies || {}
))
let plugins = [
nodeResolve({
only: [ 'tslib' ] // the only external module we want to bundle
}),
replace({
delimiters: [ '<%= ', ' %>' ],
values: {
version: rootPkgJsonData.version,
releaseDate: new Date().toISOString().replace(/T.*/, '')
// ^TODO: store this in package.json for easier old-release recreation
}
})
]
if (isDev) {
plugins.push(sourcemaps())
// HACK: there's a bug with sourcemap reading and watching: the first rebuild includes the intermediate
// sourceMappingURL comments, confusing consumers of the generated sourcemap. Forcefully remove these comments.
plugins.push(cleanup({ comments: 'none' }))
}
return {
onwarn,
watch: WATCH_OPTIONS,
input: path.join('tmp/tsc-output', pkgStruct.srcDir, 'main.js'),
external,
output: [
{
file: path.join(pkgStruct.distDir, 'main.js'),
format: 'umd',
name: pkgStruct.browserGlobal,
globals: {
...ownBrowserGlobals,
...EXTERNAL_BROWSER_GLOBALS,
...EXTERNAL_BROWSER_GLOBAL_HACKS
},
exports: 'named',
sourcemap: isDev,
banner
},
{
file: path.join(pkgStruct.distDir, 'main.esm.js'),
format: 'esm',
sourcemap: isDev,
banner
}
],
plugins
}
}
// Locales
// ----------------------------------------------------------------------------------------------------
function buildLocaleConfigs(ownBrowserGlobals) {
let corePkgStruct = getCorePkgStruct()
let coreTmpDir = path.join('tmp/tsc-output', corePkgStruct.srcDir)
let localePaths = glob.sync('locales/*.js', { cwd: coreTmpDir })
let external = Object.keys(ownBrowserGlobals)
let configs = []
for (let localePath of localePaths) {
let localeName = path.basename(localePath).replace(/\..*$/, '')
configs.push({
onwarn,
watch: WATCH_OPTIONS,
input: path.join(coreTmpDir, localePath),
external,
output: {
file: path.join(corePkgStruct.distDir, localePath),
globals: ownBrowserGlobals,
name: 'FullCalendarLocales.' + localeName,
format: 'umd'
}
})
}
// ALL locales in one file
configs.push({
onwarn,
watch: WATCH_OPTIONS,
input: localePaths.map(localePath => path.join(coreTmpDir, localePath)),
external,
output: {
file: path.join(corePkgStruct.distDir, 'locales-all.js'),
globals: ownBrowserGlobals,
name: 'FullCalendarLocalesAll',
format: 'umd'
},
plugins: [
multiEntry({ exports: 'array' })
]
})
return configs
}
// Tests
// ----------------------------------------------------------------------------------------------------
function buildTestConfig() {
let plugins = [
multiEntry({
exports: false // don't combine all the exports. no need, and would collide
}),
nodeResolve({
customResolveOptions: {
// tests can access all the dependencies they declared. these deps will be bundled.
// apparently this setting is inefficient.
// IMPORTANT: our internal package.jsons need to be written first
paths: [
'packages/__tests__/node_modules',
'packages-premium/__tests__/node_modules'
]
}
}),
alias({
// the alias to the non-premium tests. must be absolute
'package-tests': path.join(process.cwd(), 'tmp/tsc-output/packages/__tests__/src')
}),
commonjs(), // for fast-deep-equal import
sourcemaps()
]
return {
onwarn,
watch: WATCH_OPTIONS,
input: [
'tmp/tsc-output/packages?(-premium)/__tests__/src/globals.js',
'tmp/tsc-output/packages?(-premium)/__tests__/src/**/*.js'
],
external: [
// HACK: because hoisting is no yet implemented for the monorepo-tool, when we require our packages,
// *their* dependencies are not deduped, we we get multiple instances of the below libraries in the bundle.
// Until hoisting is implemented, make these external and include them manually from karma.config.js.
'luxon',
'rrule',
'moment',
'moment/locale/es',
'moment-timezone/builds/moment-timezone-with-data'
],
output: {
file: 'tmp/tests.js',
globals: Object.assign({}, EXTERNAL_BROWSER_GLOBALS, EXTERNAL_BROWSER_GLOBAL_HACKS), // HACK (continued)
format: 'iife',
sourcemap: true
},
plugins
}
}
// Utils
// ----------------------------------------------------------------------------------------------------
function getCorePkgStruct() {
for (let pkgStruct of pkgStructs) {
if (pkgStruct.name === '@fullcalendar/core') {
return pkgStruct
}
}
throw new Error('No core package')
}
function onwarn(warning, warn) {
// ignore circ dep warnings. too numerous and hard to fix right now
if (warning.code !== 'CIRCULAR_DEPENDENCY') {
warn(warning)
}
}
function detectIsDev() {
if (!/^(development|production)$/.test(process.env.BUILD)) {
console.warn('BUILD environment not specified. Assuming \'development\'')
return true
} else {
return process.env.BUILD === 'development'
}
}