-
Notifications
You must be signed in to change notification settings - Fork 6
/
rollup.config.mjs
161 lines (146 loc) · 4.18 KB
/
rollup.config.mjs
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
import strip from '@rollup/plugin-strip'
import terser from '@rollup/plugin-terser'
import stripCode from 'rollup-plugin-strip-code'
import { readFile } from 'node:fs/promises'
const packagemeta = JSON.parse(await readFile('package.json'))
export default (args) => {
const plugins = []
const stripfunctions = []
if (!args.dev) {
stripfunctions.push('console.log', 'assert.*', 'panic', 'log')
}
plugins.push(
strip({
functions: stripfunctions,
sourceMap: true,
})
)
if (!args.dev) {
plugins.push(
stripCode({
start_comment: 'DEV.START',
end_comment: 'DEV.END',
})
)
}
const features = {
css: true,
jsxLiterals: false,
usestring: false,
stores: false,
}
const polyfills = {
scope: true,
}
for (const arg in args) {
if (arg.startsWith('disable-')) {
const feature = arg.slice(8)
features[feature] = false
}
if (arg.startsWith('enable-')) {
const feature = arg.slice(7)
features[feature] = true
}
if (arg.startsWith('polyfill-')) {
const feature = arg.slice(9)
polyfills[feature] = true
}
if (arg.startsWith('dont-polyfill-')) {
const feature = arg.slice(14)
polyfills[feature] = false
}
}
for (const [feature, enabled] of Object.entries(features)) {
if (!enabled) {
plugins.push(
stripCode({
start_comment: `FEATURE.${feature.toUpperCase()}.START`,
end_comment: `FEATURE.${feature.toUpperCase()}.END`,
})
)
}
}
for (const [fill, enabled] of Object.entries(polyfills)) {
if (enabled) {
plugins.push(
stripCode({
start_comment: `NOT.POLYFILL.${fill.toUpperCase()}.START`,
end_comment: `NOT.POLYFILL.${fill.toUpperCase()}.END`,
})
)
} else {
plugins.push(
stripCode({
start_comment: `POLYFILL.${fill.toUpperCase()}.START`,
end_comment: `POLYFILL.${fill.toUpperCase()}.END`,
})
)
}
}
const dlbanner = `// dreamland.js, MIT license\nconst DLFEATURES = [${Object.entries(
features
)
.filter(([_, enabled]) => enabled)
.map(([feature, _]) => `'${feature}'`)
.join(', ')}]; const DLVERSION = '${packagemeta.version}';`
if (args.dev || args.nominify) {
plugins.push({
banner() {
return dlbanner
},
})
} else {
plugins.push(
terser({
mangle: {
toplevel: true,
},
compress: {
unused: true,
collapse_vars: true,
toplevel: true,
},
output: {
comments: false,
preamble: dlbanner,
},
})
)
}
const sharedOutput = {
format: 'iife',
name: 'window',
extend: true,
strict: false,
plugins: [
{
name: 'iife-plus',
renderChunk(code) {
// iife output doesn't support globals, and the name:"window" hack they told me to use on github doesn't work with a bundler
// regex is good enough
return code.replace(
/\(this\.window.?=.?this\.window.?\|\|.?\{\}\);/,
'(window)'
)
},
},
],
}
const iifeOutput = {
sourcemap: true,
...sharedOutput,
}
const devOutput = {
sourcemap: true,
...sharedOutput,
}
const output = args.dev ? devOutput : iifeOutput
return [
{
input: 'src/main.js',
external: ['window'],
output,
plugins: plugins,
},
]
}