forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.js
189 lines (178 loc) · 5.32 KB
/
rollup.config.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import fs from 'fs'
import path from 'pathe'
import esbuild from 'rollup-plugin-esbuild'
import dts from 'rollup-plugin-dts'
import resolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import alias from '@rollup/plugin-alias'
import license from 'rollup-plugin-license'
import c from 'picocolors'
import fg from 'fast-glob'
import pkg from './package.json'
const entries = [
'src/index.ts',
'src/node/cli.ts',
'src/node.ts',
'src/runtime/worker.ts',
'src/runtime/entry.ts',
]
const dtsEntries = [
'src/index.ts',
'src/node.ts',
]
const external = [
...Object.keys(pkg.dependencies),
...Object.keys(pkg.peerDependencies),
'worker_threads',
'inspector',
'c8',
]
export default ({ watch }) => [
{
input: entries,
output: {
dir: 'dist',
format: 'esm',
sourcemap: 'inline',
},
external,
plugins: [
alias({
entries: [
{ find: /^node:(.+)$/, replacement: '$1' },
{ find: 'vite-node/server', replacement: path.resolve(__dirname, '../vite-node/src/server.ts') },
{ find: 'vite-node/client', replacement: path.resolve(__dirname, '../vite-node/src/client.ts') },
{ find: 'vite-node/utils', replacement: path.resolve(__dirname, '../vite-node/src/utils.ts') },
],
}),
resolve({
preferBuiltins: true,
}),
json(),
commonjs(),
esbuild({
target: 'node14',
}),
!watch && licensePlugin(),
],
onwarn(message) {
if (/Circular dependencies/.test(message))
return
console.error(message)
},
},
...dtsEntries.map(input => ({
input,
output: {
file: input.replace('src/', 'dist/').replace('.ts', '.d.ts'),
format: 'esm',
},
external,
plugins: [
dts({ respectExternal: true }),
],
})),
]
function licensePlugin() {
return license({
thirdParty(dependencies) {
// https://github.com/rollup/rollup/blob/master/build-plugins/generate-license-file.js
// MIT Licensed https://github.com/rollup/rollup/blob/master/LICENSE-CORE.md
const coreLicense = fs.readFileSync(
path.resolve(__dirname, '../../LICENSE'),
)
function sortLicenses(licenses) {
let withParenthesis = []
let noParenthesis = []
licenses.forEach((license) => {
if (/^\(/.test(license))
withParenthesis.push(license)
else
noParenthesis.push(license)
})
withParenthesis = withParenthesis.sort()
noParenthesis = noParenthesis.sort()
return [...noParenthesis, ...withParenthesis]
}
const licenses = new Set()
const dependencyLicenseTexts = dependencies
.sort(({ name: nameA }, { name: nameB }) =>
nameA > nameB ? 1 : nameB > nameA ? -1 : 0,
)
.map(
({
name,
license,
licenseText,
author,
maintainers,
contributors,
repository,
}) => {
let text = `## ${name}\n`
if (license)
text += `License: ${license}\n`
const names = new Set()
if (author && author.name)
names.add(author.name)
for (const person of maintainers.concat(contributors)) {
if (person && person.name)
names.add(person.name)
}
if (names.size > 0)
text += `By: ${Array.from(names).join(', ')}\n`
if (repository)
text += `Repository: ${repository.url || repository}\n`
if (!licenseText) {
try {
const pkgDir = path.dirname(
resolve(path.join(name, 'package.json'), {
preserveSymlinks: false,
}),
)
const licenseFile = fg.sync(`${pkgDir}/LICENSE*`, {
caseSensitiveMatch: false,
})[0]
if (licenseFile)
licenseText = fs.readFileSync(licenseFile, 'utf-8')
}
catch {}
}
if (licenseText) {
text
+= `\n${
licenseText
.trim()
.replace(/(\r\n|\r)/gm, '\n')
.split('\n')
.map(line => `> ${line}`)
.join('\n')
}\n`
}
licenses.add(license)
return text
},
)
.join('\n---------------------------------------\n\n')
const licenseText
= '# Vitest core license\n'
+ `Vitest is released under the MIT license:\n\n${
coreLicense
}\n# Licenses of bundled dependencies\n`
+ 'The published Vitest artifact additionally contains code with the following licenses:\n'
+ `${sortLicenses(licenses).join(', ')}\n\n`
+ `# Bundled dependencies:\n${
dependencyLicenseTexts}`
const existingLicenseText = fs.readFileSync('LICENSE.md', 'utf8')
if (existingLicenseText !== licenseText) {
fs.writeFileSync('LICENSE.md', licenseText)
console.warn(
c.yellow(
'\nLICENSE.md updated. You should commit the updated file.\n',
),
)
}
},
})
}