-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
rollup.config.mjs
255 lines (236 loc) · 6.87 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
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
import { createRequire } from 'module'
import { fileURLToPath } from 'url'
import fs from 'fs'
import path from 'path'
import jscc from 'rollup-plugin-jscc'
import ts from 'rollup-plugin-typescript2'
import replace from '@rollup/plugin-replace'
import json from '@rollup/plugin-json'
import alias from '@rollup/plugin-alias'
import nodeResolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import { getBabelOutputPlugin } from '@rollup/plugin-babel'
import terser from '@rollup/plugin-terser'
if (!process.env.TARGET) {
throw new Error('TARGET package must be specified via --environment flag.')
}
const require = createRequire(import.meta.url)
const __dirname = fileURLToPath(new URL('.', import.meta.url))
const packagesDir = path.resolve(__dirname, 'packages')
const packageDir = path.resolve(packagesDir, process.env.TARGET)
const resolve = (p) => path.resolve(packageDir, p)
const pkg = require(resolve(`package.json`))
// ensure TS checks only once for each build
let hasTSChecked = false
const configs = []
let buildOptions = require(resolve(`build.json`))
function normalizeOutput(file, output = {}) {
return Object.assign(
{
file,
format: file.includes('.cjs.') ? 'cjs' : 'es',
exports: 'auto',
interop: 'auto',
sourcemap: process.env.ENABLE_SOURCEMAP === 'true',
},
output
)
}
if (!Array.isArray(buildOptions)) {
buildOptions = [buildOptions]
}
buildOptions.forEach((buildOption) => {
Object.keys(buildOption.input).forEach((name) => {
const files = buildOption.input[name]
if (Array.isArray(files)) {
files.forEach((file) => {
configs.push(
createConfig(
name,
normalizeOutput(resolve(file), buildOption.output),
buildOption
)
)
})
} else {
configs.push(
createConfig(
name,
normalizeOutput(resolve(buildOption.input[name]), buildOption.output),
buildOption
)
)
}
})
})
export default configs
function resolveTsconfigJson() {
const tsconfigJsonPath = resolve('tsconfig.json')
if (
fs.existsSync(tsconfigJsonPath)
// &&
// require(tsconfigJsonPath).extends === '../../tsconfig.json'
) {
return tsconfigJsonPath
}
return path.resolve(__dirname, 'tsconfig.json')
}
function parseExternal(external) {
const parsed = external === false
? []
: Array.isArray(external)
? external
: [
'vue',
'@vue/shared',
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
...(external || []),
]
return parsed.map((id) => {
if (typeof id === 'string') {
return id
}
return new RegExp(id.source, id.flags)
})
}
function createConfig(entryFile, output, buildOption) {
const shouldEmitDeclarations = process.env.TYPES != null && !hasTSChecked
const tsOptions = {
check: !process.env.TRANSPILE_ONLY &&
(!process.env.CI && process.env.NODE_ENV === 'production' && !hasTSChecked),
tsconfig: resolveTsconfigJson(),
cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'),
tsconfigOverride: {
compilerOptions: {
sourceMap: output.sourcemap,
declaration: shouldEmitDeclarations,
declarationMap: false,
skipLibCheck: true,
...(buildOption.compilerOptions || {}),
},
exclude: ['**/__tests__', 'test-dts'],
},
useTsconfigDeclarationDir: true,
clean: false,
verbosity: 0
}
const tsPlugin = ts(tsOptions)
// we only need to check TS and generate declarations once for each build.
// it also seems to run into weird issues when checking multiple times
// during a single build.
hasTSChecked = true
const external = parseExternal(buildOption.external)
let isX = process.env.UNI_APP_X === 'true'
if (!isX && buildOption.replacements) {
if (buildOption.replacements.__X__ === 'true') {
isX = true
}
}
const plugins = [
createAliasPlugin(buildOption),
nodeResolve(),
commonjs(),
json({
// namedExports: false,
}),
tsPlugin,
createReplacePlugin(buildOption, output.format),
]
if (buildOption.output?.compact) {
plugins.push(terser())
}
if (process.env.TARGET !== 'uni-push' && process.env.TARGET !== 'uni-stat') {
plugins.unshift(jscc({
values: {
// 该插件限制了不能以__开头
_NODE_JS_: 0,
_X_: isX ? 1 : 0,
},
exclude: ['**/node_modules/**']
}))
}
if (buildOption.babel) {
// TODO weex 使用了 buble 编译,暂时先通过 babel 编译一遍,避免 buble 编译失败
plugins.push(
getBabelOutputPlugin({
allowAllFormats: true,
sourceType: 'module',
presets: [['@babel/preset-env', { targets: ['iOS 10'] }]],
})
)
}
if (buildOption.replaceAfterBundled) {
const replacements = buildOption.replaceAfterBundled
plugins.push({
name: 'replace-after-bundled',
generateBundle(_options, bundles) {
Object.keys(bundles).forEach((name) => {
const bundle = bundles[name]
if (!bundle.code) {
return
}
Object.keys(replacements).forEach((replacement) => {
bundle.code = bundle.code.replace(
new RegExp(replacement, 'g'),
replacements[replacement]
)
})
})
},
})
}
return {
input: resolve(entryFile),
external,
plugins,
output,
onwarn: (msg, warn) => {
// if (!/Circular/.test(msg)) {
warn(msg)
// }
},
treeshake:
buildOption.treeshake === false
? false
: {
moduleSideEffects(id) {
if (id.endsWith('polyfill.ts')) {
console.log('[WARN]:sideEffects[' + id + ']')
return true
}
return false
},
},
}
}
function createAliasPlugin(buildOption) {
return alias(buildOption.alias || {})
}
function createReplacePlugin(buildOption, format) {
const replacements = {
__DEV__: `(process.env.NODE_ENV !== 'production')`,
__TEST__: false,
__NODE_JS__: format === 'cjs',
}
// if (process.env.TARGET === 'uni-h5') {
// replacements.global = format === 'cjs' ? 'global' : 'window'
// }
if (buildOption.replacements) {
Object.assign(replacements, buildOption.replacements)
}
// 注入 __UNI_CONSOLE_WEBVIEW_EVAL_JS_CODE__
if ('process.env.UNI_CONSOLE_WEBVIEW_EVAL_JS_CODE' in replacements) {
replacements['process.env.UNI_CONSOLE_WEBVIEW_EVAL_JS_CODE'] = () => {
return JSON.stringify(
fs.readFileSync(resolve('dist/__uniwebview.js'), 'utf-8')
)
}
}
Object.keys(replacements).forEach((key) => {
if (key in process.env) {
replacements[key] = process.env[key]
}
})
return replace({ delimiters: ['', ''], values: replacements, preventAssignment: true })
}