-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathbob-esbuild.config.ts
More file actions
87 lines (74 loc) · 2.44 KB
/
Copy pathbob-esbuild.config.ts
File metadata and controls
87 lines (74 loc) · 2.44 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
import { readFileSync } from 'fs';
import { sep } from 'path';
const isCLIPackage = process.cwd().endsWith(sep + 'cli');
export const config: import('bob-esbuild').BobConfig = {
tsc: {
dirs: ['packages/*'],
},
verbose: true,
manualRewritePackageJson: {
// Keep the expo/metro-friendly condition order when the package.json is
// rewritten for publication. Otherwise the generated manifest puts the ESM
// entry first and native apps pull the web bundle.
'@gqty/react': (pkg) => {
const normalizePath = (value?: string) =>
value?.replace('./dist/', './');
const deriveNativePath = (value?: string) =>
normalizePath(value)?.replace(/(\.m?js)$/u, '.native$1');
const preserveOrder = (
entry: Record<string, unknown> | string | undefined
) => {
if (entry == null || typeof entry === 'string') return entry;
const candidate =
(entry.require as string | undefined) ??
(entry.import as string | undefined);
const nativePath = deriveNativePath(candidate);
const orderedEntries: [string, unknown][] = [];
if (nativePath) orderedEntries.push(['react-native', nativePath]);
for (const [key, value] of Object.entries(entry)) {
if (key === 'react-native') continue;
orderedEntries.push([key, value]);
}
return Object.fromEntries(orderedEntries);
};
const rewrittenExports =
pkg.exports &&
Object.fromEntries(
Object.entries(pkg.exports).map(([key, value]) => [
key,
preserveOrder(value as any),
])
);
const nativeMain = deriveNativePath(pkg.main);
return nativeMain || rewrittenExports
? {
...pkg,
...(nativeMain ? { 'react-native': nativeMain } : null),
...(rewrittenExports ? { exports: rewrittenExports } : null),
}
: pkg;
},
},
esbuildPluginOptions: isCLIPackage
? {
// Check https://github.com/evanw/esbuild/issues/1146
target: 'node13.2',
define: {
__VERSION__: JSON.stringify(
JSON.parse(readFileSync('./package.json', 'utf-8')).version
),
},
}
: undefined,
outputOptions: {
interop(module) {
switch (module) {
case 'react-ssr-prepass':
case 'react':
return 'esModule';
default:
return 'auto';
}
},
},
};