-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.config.mts
More file actions
111 lines (102 loc) · 2.6 KB
/
rollup.config.mts
File metadata and controls
111 lines (102 loc) · 2.6 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
/**
* @file rollup
* @module config/rollup
*/
import { EXPORT_AGGREGATE_REGEX } from '@flex-development/export-regex'
import resolve from '@rollup/plugin-node-resolve'
import { ok } from 'devlop'
import type {
NormalizedOutputOptions,
OutputBundle,
Plugin,
PluginContext,
RollupOptions
} from 'rollup'
import cleanup from 'rollup-plugin-cleanup'
import { dts as dtsBundle } from 'rollup-plugin-dts'
import pkg from './package.json' with { type: 'json' }
/**
* The list of target files.
*
* @const {ReadonlyArray<string>} files
*/
const files: readonly string[] = ['./dist/index.d.mts', './dist/index.mjs']
/**
* The rollup configuration.
*
* @see {@linkcode RollupOptions}
*
* @type {RollupOptions[]}
*/
export default files.map(input => {
/**
* The list of plugins.
*
* @const {(Plugin | Plugin[])[]} plugins
*/
const plugins: (Plugin | Plugin[])[] = []
if (input.endsWith('.mjs')) {
plugins.push(resolve(), cleanup({ comments: 'none' }))
} else {
plugins.push(resolve({ extensions: ['.d.mts', '.mts'] }), dts())
}
return {
external: Object.keys(pkg.dependencies),
input,
output: [{ file: input, format: 'esm' }],
plugins
}
})
/**
* Create a plugin pack to bundle declaration files and fix `type` modifiers.
*
* @this {void}
*
* @return {Plugin[]}
* The plugin pack
*/
function dts(this: void): Plugin[] {
return [
dtsBundle(),
{
/**
* 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`')
return type ? match : match.replace('export {', 'export type {')
})
output.code = output.code.replaceAll('import {', 'import type {')
}
}
return void this
},
/**
* The plugin name.
*/
name: 'dts:fix-type-modifiers'
}
]
}