-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtsdown.config.mts
More file actions
127 lines (110 loc) · 3.82 KB
/
tsdown.config.mts
File metadata and controls
127 lines (110 loc) · 3.82 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
import { defineConfig } from 'tsdown';
import fs from 'node:fs';
import path from 'node:path';
import {parseAst} from 'rolldown/parseAst';
type AstNode = {
type?: string;
start?: number;
end?: number;
name?: string;
value?: unknown;
callee?: AstNode;
arguments?: AstNode[];
[key: string]: unknown;
};
type Replacement = {
start: number;
end: number;
value: string;
};
function isNode(value: unknown): value is AstNode {
return typeof value === 'object' && value !== null && typeof (value as AstNode).type === 'string';
}
function isNodeArray(value: unknown): value is AstNode[] {
return Array.isArray(value) && value.every(isNode);
}
function isBundledDependency(id: string): boolean {
return id.includes('/node_modules/') || id.includes('\\node_modules\\');
}
function isPackageJsonSpecifier(value: unknown): value is string {
return typeof value === 'string' && value.startsWith('.') && path.basename(value) === 'package.json';
}
function findPackageJsonRequires(node: AstNode, visitor: (node: AstNode, specifier: string) => void): void {
if (
node.type === 'CallExpression'
&& node.callee?.type === 'Identifier'
&& (node.callee.name === 'cjsRequire' || node.callee.name === 'require')
&& node.arguments?.length === 1
&& node.arguments[0]?.type === 'Literal'
&& isPackageJsonSpecifier(node.arguments[0].value)
) {
visitor(node, node.arguments[0].value);
}
for (const value of Object.values(node)) {
if (isNode(value)) {
findPackageJsonRequires(value, visitor);
} else if (isNodeArray(value)) {
for (const child of value) {
findPackageJsonRequires(child, visitor);
}
}
}
}
function applyReplacements(code: string, replacements: Replacement[]): string {
return replacements
.sort((first, second) => second.start - first.start)
.reduce(
(result, replacement) => (
result.slice(0, replacement.start) + replacement.value + result.slice(replacement.end)
),
code,
);
}
function inlineBundledPackageJson() {
return {
name: 'inline-bundled-package-json',
transform(code: string, id: string, meta: {ast?: AstNode} = {}) {
if (!isBundledDependency(id) || !code.includes('package.json')) {
return null;
}
const ast = meta.ast ?? parseAst(code, null, id) as AstNode;
const replacements: Replacement[] = [];
findPackageJsonRequires(ast, (node, specifier) => {
if (node.start === undefined || node.end === undefined) {
return;
}
const packageJsonPath = path.resolve(path.dirname(id), specifier);
if (!fs.existsSync(packageJsonPath)) {
return;
}
replacements.push({
start: node.start,
end: node.end,
value: JSON.stringify(JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))),
});
});
return replacements.length > 0 ? applyReplacements(code, replacements) : null;
},
};
}
export default defineConfig({
entry: ['src/index.ts'],
format: 'cjs',
dts: true,
platform: 'node',
plugins: [inlineBundledPackageJson()],
alias: {
// Redirect to a shim that statically resolves rules
// (the original uses `requireindex` which breaks in bundles)
'eslint-plugin-jest-dom': path.resolve('src/shims/eslint-plugin-jest-dom.ts'),
},
deps: {
// Native bindings can't be bundled
neverBundle: [
/^@unrs\//,
/^@eslint\//,
'eslint',
'unrs-resolver',
],
},
});