forked from decs/typeschema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsup.config.ts
66 lines (61 loc) · 1.56 KB
/
tsup.config.ts
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
import type {Plugin} from 'esbuild';
import fs from 'node:fs';
import {
anyOf,
char,
createRegExp,
exactly,
global,
maybe,
multiline,
oneOrMore,
whitespace,
wordChar,
} from 'magic-regexp';
import {defineConfig} from 'tsup';
const regex = createRegExp(
maybe(oneOrMore(whitespace)).groupedAs('indentation'),
anyOf('const', 'let', 'var').groupedAs('declarationType'),
oneOrMore(whitespace),
oneOrMore(wordChar).or('{', oneOrMore(char), '}').groupedAs('variableName'),
oneOrMore(whitespace),
'=',
oneOrMore(whitespace),
maybe('await', oneOrMore(whitespace)).groupedAs('await'),
'import(',
maybe(oneOrMore(whitespace)),
exactly("'", oneOrMore(char), "'")
.or('"', oneOrMore(char), '"')
.groupedAs('modulePath'),
maybe(oneOrMore(whitespace)),
')',
maybe(';').groupedAs('terminator'),
[global, multiline],
);
const template = `
try {
var dynamicallyImportedModule = $<await>import(
/* webpackIgnore: true */
$<modulePath>
)$<terminator>
} catch (moduleImportError) {
throw moduleImportError$<terminator>
}
$<declarationType> $<variableName> = dynamicallyImportedModule$<terminator>
`.trim();
const optionalPeerDependenciesPlugin: Plugin = {
name: 'optionalPeerDependencies',
setup(build) {
build.onLoad({filter: /\.ts$/}, async args => ({
contents: fs.readFileSync(args.path, 'utf-8').replace(regex, template),
loader: 'ts',
}));
},
};
export default defineConfig({
clean: true,
dts: true,
entry: ['src/index.ts'],
esbuildPlugins: [optionalPeerDependenciesPlugin],
format: ['esm', 'cjs'],
});