/
rollup.ts
225 lines (192 loc) · 7.71 KB
/
rollup.ts
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
import { writeFile, mkdir } from 'fs/promises'
import { promises as fsp } from 'fs'
import { pathToFileURL } from 'url'
import type { RollupOptions, OutputOptions, OutputChunk } from 'rollup'
import { rollup } from 'rollup'
import commonjs from '@rollup/plugin-commonjs'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import alias from '@rollup/plugin-alias'
import _esbuild from 'rollup-plugin-esbuild'
import dts from 'rollup-plugin-dts'
import replace from '@rollup/plugin-replace'
import { relative, resolve, dirname, normalize, extname } from 'pathe'
import { resolvePath, resolveModuleExportNames } from 'mlly'
import { getpkg, tryResolve, warn } from '../utils'
import type { BuildContext } from '../types'
import { JSONPlugin } from './plugins/json'
import { rawPlugin } from './plugins/raw'
import { cjsPlugin } from './plugins/cjs'
import { shebangPlugin, makeExecutable, getShebang } from './plugins/shebang'
// @ts-ignore https://github.com/unjs/unbuild/issues/23
const esbuild = _esbuild.default || _esbuild
const DEFAULT_EXTENSIONS = ['.ts', '.tsx', '.mjs', '.cjs', '.js', '.jsx', '.json']
export async function rollupBuild (ctx: BuildContext) {
if (ctx.options.stub) {
const jitiPath = await resolvePath('jiti', { url: import.meta.url })
for (const entry of ctx.options.entries.filter(entry => entry.builder === 'rollup')) {
const output = resolve(ctx.options.rootDir, ctx.options.outDir, entry.name!)
const resolvedEntry = normalize(tryResolve(entry.input, ctx.options.rootDir) || entry.input)
const resolvedEntryWithoutExt = resolvedEntry.substring(0, resolvedEntry.length - extname(resolvedEntry).length)
const code = await fsp.readFile(resolvedEntry, 'utf8')
const shebang = getShebang(code)
await mkdir(dirname(output), { recursive: true })
// CJS Stub
if (ctx.options.rollup.emitCJS) {
await writeFile(output + '.cjs', `${shebang}module.exports = require(${JSON.stringify(jitiPath)})(null, { interopDefault: true, esmResolve: true })(${JSON.stringify(resolvedEntry)})`)
}
// MJS Stub
// Try to analyze exports
const namedExports: string[] = await resolveModuleExportNames(resolvedEntry, {
extensions: DEFAULT_EXTENSIONS
}).catch((err) => {
warn(ctx, `Cannot analyze ${resolvedEntry} for exports:` + err)
return []
})
const hasDefaultExport = namedExports.includes('default') || !namedExports.length
await writeFile(output + '.mjs', shebang + [
`import jiti from ${JSON.stringify(pathToFileURL(jitiPath).href)};`,
'',
`/** @type {import(${JSON.stringify(resolvedEntryWithoutExt)})} */`,
`const _module = jiti(null, { interopDefault: true, esmResolve: true })(${JSON.stringify(resolvedEntry)});`,
hasDefaultExport ? '\nexport default _module;' : '',
...namedExports.filter(name => name !== 'default').map(name => `export const ${name} = _module.${name};`)
].join('\n'))
// DTS Stub
await writeFile(output + '.d.ts', [
`export * from ${JSON.stringify(resolvedEntryWithoutExt)};`,
hasDefaultExport ? `export { default } from ${JSON.stringify(resolvedEntryWithoutExt)};` : ''
].join('\n'))
if (shebang) {
await makeExecutable(output + '.cjs')
await makeExecutable(output + '.mjs')
}
}
await ctx.hooks.callHook('rollup:done', ctx)
return
}
const rollupOptions = getRollupOptions(ctx)
await ctx.hooks.callHook('rollup:options', ctx, rollupOptions)
if (!Object.keys(rollupOptions.input as any).length) {
return
}
const buildResult = await rollup(rollupOptions)
await ctx.hooks.callHook('rollup:build', ctx, buildResult)
const allOutputOptions = rollupOptions.output! as OutputOptions[]
for (const outputOptions of allOutputOptions) {
const { output } = await buildResult.write(outputOptions)
for (const entry of output.filter(e => e.type === 'chunk') as OutputChunk[]) {
for (const id of entry.imports) {
ctx.usedImports.add(id)
}
if (entry.isEntry) {
ctx.buildEntries.push({
path: relative(ctx.options.rootDir, resolve(outputOptions.dir!, entry.fileName)),
bytes: Buffer.byteLength(entry.code, 'utf8'),
exports: entry.exports
})
}
}
}
// Types
if (ctx.options.declaration) {
rollupOptions.plugins = rollupOptions.plugins || []
// TODO: Use fresh rollup options
const shebangPlugin: any = rollupOptions.plugins.find(p => p && p.name === 'unbuild-shebang')
shebangPlugin._options.preserve = false
rollupOptions.plugins.push(dts(ctx.options.rollup.dts))
await ctx.hooks.callHook('rollup:dts:options', ctx, rollupOptions)
const typesBuild = await rollup(rollupOptions)
await ctx.hooks.callHook('rollup:dts:build', ctx, typesBuild)
await typesBuild.write({
dir: resolve(ctx.options.rootDir, ctx.options.outDir),
format: 'esm'
})
}
await ctx.hooks.callHook('rollup:done', ctx)
}
export function getRollupOptions (ctx: BuildContext): RollupOptions {
return {
context: ctx.options.rootDir,
input: Object.fromEntries(ctx.options.entries
.filter(entry => entry.builder === 'rollup')
.map(entry => [entry.name, resolve(ctx.options.rootDir, entry.input)])
),
output: [
ctx.options.rollup.emitCJS && {
dir: resolve(ctx.options.rootDir, ctx.options.outDir),
entryFileNames: '[name].cjs',
chunkFileNames: `chunks/${ctx.options.name}.[hash].cjs`,
format: 'cjs',
exports: 'auto',
preferConst: true,
externalLiveBindings: false,
freeze: false
},
{
dir: resolve(ctx.options.rootDir, ctx.options.outDir),
entryFileNames: '[name].mjs',
chunkFileNames: `chunks/${ctx.options.name}.[hash].mjs`,
format: 'esm',
exports: 'auto',
preferConst: true,
externalLiveBindings: false,
freeze: false
}
].filter(Boolean),
external (id) {
const pkg = getpkg(id)
const isExplicitExternal = ctx.options.externals.includes(pkg)
if (isExplicitExternal) {
return true
}
if (ctx.options.rollup.inlineDependencies || id[0] === '.' || id[0] === '/' || id.match(/src[\\/]/) || id.startsWith(ctx.pkg.name!)) {
return false
}
if (!isExplicitExternal) {
warn(ctx, `Inlined implicit external ${id}`)
}
return isExplicitExternal
},
onwarn (warning, rollupWarn) {
if (!warning.code || !['CIRCULAR_DEPENDENCY'].includes(warning.code)) {
rollupWarn(warning)
}
},
plugins: [
ctx.options.rollup.replace && replace({
...ctx.options.rollup.replace,
values: {
...ctx.options.replace,
...ctx.options.rollup.replace.values
}
}),
ctx.options.rollup.alias && alias({
...ctx.options.rollup.alias,
entries: {
[ctx.pkg.name!]: ctx.options.rootDir,
...ctx.options.alias,
...ctx.options.rollup.alias.entries
}
}),
ctx.options.rollup.resolve && nodeResolve({
extensions: DEFAULT_EXTENSIONS,
...ctx.options.rollup.resolve
}),
ctx.options.rollup.json && JSONPlugin({
...ctx.options.rollup.json
}),
shebangPlugin(),
ctx.options.rollup.esbuild && esbuild({
...ctx.options.rollup.esbuild
}),
ctx.options.rollup.commonjs && commonjs({
extensions: DEFAULT_EXTENSIONS,
...ctx.options.rollup.commonjs
}),
// Preserve dynamic imports for CommonJS
{ renderDynamicImport () { return { left: 'import(', right: ')' } } },
ctx.options.rollup.cjsBridge && cjsPlugin({}),
rawPlugin()
].filter(Boolean)
} as RollupOptions
}