-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
173 lines (157 loc) · 4.61 KB
/
rollup.config.mjs
File metadata and controls
173 lines (157 loc) · 4.61 KB
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
import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import typescript from '@rollup/plugin-typescript'
import fs from 'node:fs/promises'
import { builtinModules } from 'node:module'
import path from 'node:path'
import { dts } from 'rollup-plugin-dts'
const shouldSourcemap = process.env.ROLLUP_SHOULD_SOURCEMAP === 'true'
const removeComments = process.env.NODE_ENV === 'production'
const { default: manifest } = await import(path.resolve('package.json'), {
with: { type: 'json' },
})
const shouldCopySchemaForPackage = manifest.name === '@guanghechen/commander'
const deps = new Set([
...builtinModules,
...builtinModules.map(m => `node:${m}`),
...Object.keys(manifest.dependencies ?? {}),
...Object.keys(manifest.peerDependencies ?? {}),
...Object.keys(manifest.optionalDependencies ?? {}),
])
const external = id => {
if (id.startsWith('.') || path.isAbsolute(id)) return false
const match = /^(@[^/]+\/[^/]+|[^/]+)/.exec(id)
return match ? deps.has(match[1]) : false
}
function resolveBuildEntries() {
const exportsField = manifest.exports
if (
exportsField &&
typeof exportsField === 'object' &&
!('source' in exportsField) &&
!('import' in exportsField) &&
!('require' in exportsField) &&
!('types' in exportsField)
) {
const entries = []
for (const [subpath, entry] of Object.entries(exportsField)) {
if (entry === null || typeof entry !== 'object') {
continue
}
entries.push({
subpath,
input: entry.source,
esm: entry.import,
cjs: entry.require,
types: entry.types,
})
}
return entries
}
const entry = exportsField?.['.'] ?? exportsField ?? {}
return [
{
subpath: '.',
input: entry.source ?? manifest.source ?? './src/index.ts',
esm: entry.import ?? manifest.module,
cjs: entry.require ?? manifest.main,
types: entry.types ?? manifest.types,
},
]
}
function createTsPlugins() {
return [
nodeResolve({ preferBuiltins: true, extensions: ['.ts', '.tsx', '.js', '.jsx', '.json'] }),
json(),
typescript({
tsconfig: 'tsconfig.lib.json',
compilerOptions: {
declaration: false,
declarationMap: false,
sourceMap: shouldSourcemap,
removeComments,
},
}),
commonjs(),
]
}
function createCopySchemaPlugin() {
let copied = false
return {
name: 'copy-schema-assets',
async writeBundle() {
if (copied) {
return
}
copied = true
const sourceDir = path.resolve('schema')
const targetDir = path.resolve('lib/schema')
let entries
try {
entries = await fs.readdir(sourceDir, { withFileTypes: true })
} catch (error) {
const ioError = error
if (
typeof ioError === 'object' &&
ioError &&
'code' in ioError &&
ioError.code === 'ENOENT'
) {
return
}
throw error
}
const schemaFiles = entries.filter(
entry => entry.isFile() && entry.name.endsWith('.schema.json'),
)
if (schemaFiles.length === 0) {
return
}
await fs.mkdir(targetDir, { recursive: true })
await Promise.all(
schemaFiles.map(entry =>
fs.copyFile(path.join(sourceDir, entry.name), path.join(targetDir, entry.name)),
),
)
},
}
}
const configs = []
let copySchemaPluginInstalled = false
for (const entry of resolveBuildEntries()) {
const input =
entry.input ?? (entry.subpath === '.' ? (manifest.source ?? './src/index.ts') : undefined)
if (!input) {
continue
}
if (entry.esm || entry.cjs) {
const output = []
if (entry.esm) {
output.push({ file: entry.esm, format: 'esm', exports: 'named', sourcemap: shouldSourcemap })
}
if (entry.cjs) {
output.push({ file: entry.cjs, format: 'cjs', exports: 'named', sourcemap: shouldSourcemap })
}
const plugins = createTsPlugins()
if (shouldCopySchemaForPackage && !copySchemaPluginInstalled) {
plugins.push(createCopySchemaPlugin())
copySchemaPluginInstalled = true
}
configs.push({ input, output, external, plugins })
}
if (entry.types) {
const plugins = [dts({ tsconfig: 'tsconfig.lib.json', respectExternal: true })]
if (shouldCopySchemaForPackage && !copySchemaPluginInstalled) {
plugins.push(createCopySchemaPlugin())
copySchemaPluginInstalled = true
}
configs.push({
input,
output: { file: entry.types, format: 'esm' },
external,
plugins,
})
}
}
export default configs