forked from fullcalendar/fullcalendar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrollup.packages.js
169 lines (144 loc) · 4.36 KB
/
rollup.packages.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
const fs = require('fs')
const path = require('path')
const dts = require('rollup-plugin-dts').default
const sourceMapLoader = require('rollup-plugin-sourcemaps')
const postcss = require('rollup-plugin-postcss')
const { checkNoSymlinks, buildBanner } = require('./scripts/lib/new')
const { externalizeStylesheets, externalizeNonRelative, injectReleaseDate } = require('./scripts/lib/new-rollup')
/*
needs tsc to run first
but needs symlinks killed also
compiles from TSC files
*/
const { publicPackageStructs } = require('./scripts/lib/package-index')
checkNoSymlinks(publicPackageStructs)
module.exports = [
// for JS
...publicPackageStructs.map((struct) => {
return {
input: path.join(struct.dir, struct.mainTscJs),
output: {
format: 'es',
file: path.join(struct.dir, struct.mainDistJs),
sourcemap: true,
banner: buildBanner(struct.isPremium)
},
plugins: [
externalizeVDom(),
externalizeNonRelative(),
sourceMapLoader(), // load from transpiled-via-tsc JS files
postcss({ // will use postcss.config.js
extract: true
}),
transplantCss(struct.mainName),
injectReleaseDate()
]
}
}),
// for DTS
...publicPackageStructs.map((struct) => {
return {
input: path.join(struct.dir, struct.mainTscDts),
output: {
format: 'es',
file: path.join(struct.dir, struct.mainDistDts),
},
plugins: [
fixDtsCodeIn(),
ensurePremiumCommonAmbient(),
externalizeVDom(),
externalizeStylesheets(),
externalizeNonRelative(),
dts(),
fixDtsCodeOut()
]
}
})
]
function transplantCss(fileName) { // fileName w/o extension
let hasCss = false
return {
resolveId(id, importer) {
if (/^\./.test(id) && /\.css$/.test(id)) { // relative stylesheet
hasCss = true
let filePath = path.join(importer, '..', id)
filePath = filePath.replace('/tsc/', '/src/') // not very robust!!!
return filePath
}
},
intro() {
if (hasCss) {
return `import './${fileName}.css';`
} else {
return ''
}
}
}
}
function externalizeVDom() {
return {
resolveId(id, importer) {
if (/\/vdom$/.test(id) || id.match(/^(preact|react|react-dom)$/)) {
if (
importer.match('packages/common') ||
importer.match('packages/core')
) {
return { id: './vdom', external: true, moduleSideEffects: true }
} else {
return { id: '@fullcalendar/common', external: true, moduleSideEffects: true }
}
}
}
}
}
function ensurePremiumCommonAmbient() {
return {
resolveId(id) {
if (id === '@fullcalendar/premium-common') {
return { id, external: true, moduleSideEffects: true }
}
}
}
}
function fixDtsCodeIn() {
return {
/*
fix problem with tsc outputting random imports like this:
import("../../../packages/common/src/plugin-system-struct")
import("../../../packages/common/src/main")
*/
resolveId(id, importer) {
if (importer && id.match('packages/common')) {
return { id: '@fullcalendar/common', external: true }
}
}
}
}
function fixDtsCodeOut() {
return {
renderChunk(code) {
// remove sourcemap comments and ///<reference>
code = code.replace(/\/\/.*/g, '')
/*
tsc, for classes that have superclasses with getter methods, sometimes reference the return type like this:
import("@fullcalendar/common/tsc/whatever").Something
and this is from WITHIN the @fullcalendar/common package
*/
// BUG: playing weird with TS triple-slash references
code = code.replace(/(['"]@fullcalendar\/[^'"]+)\/[^'"]+(['"])/g, function(m0, m1, m2) {
return m1 + m2
})
// sometimes tsc fully resolved generic vdom declarations to preact, which makes the VNode<any> a thing (which it shouldn't be)
code = code.replace(/VNode<any>/g, 'VNode')
/*
rollup-plugin-dts sometimes does not correctly reduce nested type declarations, leaving something like this:
import("../toolbar-struct").ToolbarInput
*/
code = code.replace(/import\(([^)]*)\)\./g, '')
if (/\b(p?react)\b/.test(code)) {
throw new Error('BAD reference to preact/react in DTS')
}
return code
}
}
}