-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
rollup.config.base.mjs
121 lines (116 loc) · 2.82 KB
/
rollup.config.base.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
// @ts-check
import fs from 'node:fs'
import { basename, dirname, join } from 'node:path'
import alias from '@rollup/plugin-alias'
import commonjs from '@rollup/plugin-commonjs'
import resolve from '@rollup/plugin-node-resolve'
import fg from 'fast-glob'
import { defineConfig } from 'rollup'
import dts from 'rollup-plugin-dts'
import esbuild from 'rollup-plugin-esbuild'
import { aliasVirtual } from './alias.mjs'
export function aliasPlugin() {
return alias({
customResolver: (source) => {
if (!source.endsWith('.ts'))
return `${source}.ts`
},
entries: aliasVirtual,
})
}
/**
* @param {string} cwd
*/
export function createConfig(cwd) {
const pkg = JSON.parse(fs.readFileSync(join(cwd, 'package.json'), 'utf-8'))
const rulesEntry = fg.sync(
'rules/**/*.ts',
{
cwd,
onlyFiles: true,
ignore: [
'rules/index.ts',
'**/*._*_.ts',
'**/*.d.ts',
'**/*.test.ts',
],
},
)
return defineConfig([
{
input: {
index: 'src/index.ts',
...Object.fromEntries(rulesEntry.map(file => [`rules/${basename(dirname(file))}`, file])),
},
output: [
{
dir: 'dist',
format: 'cjs',
manualChunks(id) {
if (id.includes('node_modules')) {
console.log('vendor', id)
return 'vendor'
}
if (id.includes('util'))
return 'utils'
if (id.includes('configs'))
return 'configs'
if (id.includes('rules')) {
const name = basename(dirname(id)).replace(/\._\w+_$/, '')
if (name !== 'rules')
return name
}
},
chunkFileNames: '[name].js',
},
],
plugins: [
commonjs(),
esbuild(),
resolve(),
aliasPlugin(),
],
external: [
...[
'eslint',
...Object.keys(pkg.dependencies || []),
...Object.keys(pkg.peerDependencies || []),
].flatMap(dep => [
dep,
new RegExp(`^${dep}/`),
]),
'node:process',
],
},
{
input: [
'./dts/index.d.ts',
'./dts/define-config-support.d.ts',
'./dts/rule-options.d.ts',
],
output: {
dir: 'dist',
format: 'es',
},
plugins: [
dts({
respectExternal: true,
tsconfig: 'tsconfig.dts.json',
}),
{
name: 'strip-comments',
renderChunk(code) {
return code
.replace(/\/\* .*? \*\//g, '')
.replace(/\n{3,}/g, '\n\n')
},
},
],
external: [
...Object.keys(pkg.dependencies || []),
...Object.keys(pkg.peerDependencies || []),
'eslint',
],
},
])
}