-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.config.mts
More file actions
152 lines (140 loc) · 4.19 KB
/
Copy pathrollup.config.mts
File metadata and controls
152 lines (140 loc) · 4.19 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
/**
* @file rollup
* @module config/rollup
*/
import { EXPORT_AGGREGATE_REGEX } from '@flex-development/export-regex'
import { STATIC_IMPORT_REGEX } from '@flex-development/import-regex'
import resolve from '@rollup/plugin-node-resolve'
import { ok } from 'devlop'
import type { Dirent } from 'node:fs'
import type {
NormalizedOutputOptions,
OutputBundle,
PluginContext,
RollupOptions
} from 'rollup'
import { dts } from 'rollup-plugin-dts'
import pkg from './package.json' with { type: 'json' }
import listSubdomains from './utils/list-subdomains.mts'
/**
* The list of subdomains.
*
* @const {ReadonlyArray<Dirent>} subdomains
*/
const subdomains: readonly Dirent[] = listSubdomains()
/**
* The list of subpath exports.
*
* @const {string[]} subpaths
*/
const subpaths: string[] = subdomains.map(sub => `${pkg.name}/${sub.name}`)
/**
* The rollup configuration.
*
* @type {RollupOptions[]}
*/
export default subdomains.map((subdomain: Dirent): RollupOptions => {
/**
* The target file.
*
* @const {string} input
*/
const input: string = `./dist/${subdomain.name}/index.d.mts`
/**
* The list of external dependencies.
*
* @const {Set<string>} external
*/
const external: Set<string> = new Set(Object.keys(pkg.dependencies))
// add subpath exports as external dependencies.
for (const subpath of subpaths) {
if (subpath !== `${pkg.name}/${subdomain.name}`) external.add(subpath)
}
return {
/**
* Determine if a module is external.
*
* @see https://rollupjs.org/configuration-options#external
*
* @this {void}
*
* @param {string} id
* The module id
* @param {string | undefined} importer
* The path to the parent module
* @param {boolean} isResolved
* Whether `id` has already been resolved
* @return {boolean | null}
* Whether the module at `id` is external, or `null` to use rollup defaults
*/
external(
this: void,
id: string,
importer: string | undefined,
isResolved: boolean
): boolean | null {
return void importer, void isResolved, external.has(id)
},
input,
output: [{ file: input, format: 'esm' }],
plugins: [
resolve({ extensions: ['.d.mts', '.mts'] }),
dts(),
{
/**
* Re-add lost `type` modifiers.
*
* @see https://github.com/Swatinem/rollup-plugin-dts/issues/354
*
* @this {PluginContext}
*
* @param {NormalizedOutputOptions} options
* The normalized output options
* @param {OutputBundle} bundle
* The output bundle object
* @return {undefined}
*/
generateBundle(
this: PluginContext,
options: NormalizedOutputOptions,
bundle: OutputBundle
): undefined {
for (const output of Object.values(bundle)) {
if (output.type === 'chunk') {
output.code = output.code.replace(EXPORT_AGGREGATE_REGEX, (
match: string,
type: string | undefined,
exports: string,
specifier: string | undefined
) => {
ok(specifier, 'expected `specifier`')
// re-exporting `Numeric` from `@flex-development/mark/core`
// only to remove it is annoying, but unfortunately required
// because `rollup-plugin-dts` does not know how to bundle types
// only used in index signatures.
if (
subdomain.name === 'parse' &&
specifier === `${pkg.name}/core`
) {
return match.replace('export {', 'import type {')
}
return type ? match : match.replace('export {', 'export type {')
})
output.code = output.code.replace(STATIC_IMPORT_REGEX, (
match: string,
type: string | undefined
) => {
return type ? match : match.replace('import', 'import type')
})
}
}
return void this
},
/**
* The plugin name.
*/
name: 'dts:fix'
}
]
}
})