-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtsup.config.mjs
More file actions
137 lines (121 loc) · 3.72 KB
/
tsup.config.mjs
File metadata and controls
137 lines (121 loc) · 3.72 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
import { defineConfig } from "tsup";
import { createStringInjectPlugin } from "./tools/stringInjectPlugin.mjs";
import { createUmdWrapperPlugin } from "./tools/umdWrapperPlugin.mjs";
import pkg from "./package.json" with { type: "json" };
import coreUtilsPkg from "./node_modules/@sv443-network/coreutils/package.json" with { type: "json" };
// #region types, consts, header
/** @typedef {import("tsup").Options} TsupOpts */
const libraryName = "UserUtils";
const externalDependencies = Object.keys(pkg.dependencies);
const isDevelopmentMode = process.env.NODE_ENV === "development";
const userLibraryHeader = `\
// ==UserScript==
// @namespace ${pkg.homepage}
// @exclude *
// @author ${pkg.author.name}
// @supportURL ${pkg.bugs.url}
// @homepageURL ${pkg.homepage}
// ==UserLibrary==
// @name ${pkg.libName}
// @description ${pkg.description}
// @version ${pkg.version}
// @license ${pkg.license}
// @copyright ${pkg.author.name} (${pkg.author.url})
// ==/UserScript==
// ==/UserLibrary==
// ==OpenUserJS==
// @author ${pkg.author.name}
// ==/OpenUserJS==
`;
// #region constant injection
/** @type {() => TsupOpts["plugins"][number]} */
const getStringInjectPlugin = () => {
const coreutilsVersion = coreUtilsPkg.version?.replace(/^[^0-9]*/, "") ?? "ERR:unknown";
const userutilsVersion = pkg.version;
const isSemverBasic = (version) => /^\d+\.\d+\.\d+(-.+)?$/.test(version);
if(!isSemverBasic(coreutilsVersion))
throw new Error(`Invalid CoreUtils version: "${coreutilsVersion}"`);
if(!isSemverBasic(userutilsVersion))
throw new Error(`Invalid UserUtils version: "${userutilsVersion}"`);
return createStringInjectPlugin([
{ pattern: /#\{\{COREUTILS_VERSION\}\}/, replacement: coreutilsVersion },
{ pattern: /#\{\{USERUTILS_VERSION\}\}/, replacement: userutilsVersion },
]);
};
// #region base config
/** @type {(cliOpts: TsupOpts) => Promise<TsupOpts | TsupOpts[]> | TsupOpts | TsupOpts[]} */
const getBaseConfig = (cliOpts) => {
/** @type {TsupOpts} */
const opts = {
entry: {
[libraryName]: "lib/index.ts",
},
outDir: "dist",
outExtension: ({ format, options }) => ({
js: `.${options.minify ? "min." : ""}${({
cjs: "cjs",
esm: "mjs",
umd: "umd.js",
})[format] ?? "js"}`,
}),
plugins: [
getStringInjectPlugin(),
],
platform: "browser",
format: ["cjs", "esm"],
noExternal: externalDependencies,
target: ["firefox100", "chrome100", "safari15", "es2018"],
name: "@sv443-network/userutils",
globalName: libraryName,
legacyOutput: false,
bundle: true,
esbuildPlugins: [],
minify: false,
splitting: false,
sourcemap: false,
dts: false,
clean: true,
watch: cliOpts.watch,
metafile: cliOpts.watch || isDevelopmentMode,
};
return opts;
};
// #region bundle configs
export default defineConfig((cliOpts) => ([
{
// base CJS and ESM bundles
...getBaseConfig(cliOpts),
splitting: false,
},
{
// regular UMD bundle
...getBaseConfig(cliOpts),
format: ["umd"],
target: "es6",
plugins: [
createUmdWrapperPlugin({
libraryName,
external: [],
}),
getStringInjectPlugin(),
],
},
{
// UMD bundle for userscript usage
...getBaseConfig(cliOpts),
format: ["umd"],
target: "es6",
plugins: [
createUmdWrapperPlugin({
libraryName,
external: [],
banner: userLibraryHeader,
}),
getStringInjectPlugin(),
],
outExtension: () => ({ js: ".user.js" }),
clean: false,
// generate declaration files after successful build
onSuccess: "tsc --emitDeclarationOnly --declaration --outDir dist && node --import tsx ./tools/fix-dts.mts",
},
]));