-
Notifications
You must be signed in to change notification settings - Fork 1
/
normalizeOptions.ts
95 lines (87 loc) · 1.87 KB
/
normalizeOptions.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
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
import type { NapiResolveOptions } from 'oxc-resolver'
import fs from 'node:fs'
import path from 'node:path'
import { cwd } from 'node:process'
// @keep-sorted
/**
* some default options copy from
* https://github.com/import-js/eslint-import-resolver-typescript/blob/master/src/index.ts
* https://github.com/rolldown/rolldown/blob/main/crates/rolldown_resolver/src/resolver.rs
*/
const defaultOptions: NapiResolveOptions = {
aliasFields: [
['browser'],
],
conditionNames: [
'default',
'types',
'import',
'require',
'node',
'node-addons',
'browser',
// APF: https://angular.io/guide/angular-package-format
'esm2020',
'es2020',
'es2015',
],
extensionAlias: {
'.js': [
'.ts',
// `.tsx` can also be compiled as `.js`
'.tsx',
'.d.ts',
'.js',
],
'.jsx': ['.tsx', '.d.ts', '.jsx'],
'.cjs': ['.cts', '.d.cts', '.cjs'],
'.mjs': ['.mts', '.d.mts', '.mjs'],
},
extensions: [
'.ts',
'.tsx',
'.d.ts',
'.js',
'.jsx',
'.json',
'.node',
],
mainFields: [
'types',
'typings',
// APF: https://angular.io/guide/angular-package-format
'fesm2020',
'fesm2015',
'esm2020',
'es2020',
'main',
'module',
'browser',
'jsnext:main',
],
roots: [cwd()],
}
function findConfigFile(files: string[]) {
while (files.length) {
const file = files.shift()!
const absPath = path.resolve(cwd(), file)
if (fs.existsSync(absPath)) {
return absPath
}
}
}
export function normalizeOptions(options: NapiResolveOptions | null = {}): NapiResolveOptions {
if (!options?.tsconfig) {
const configFile = findConfigFile(['tsconfig.json', 'jsconfig.json'])
if (configFile) {
defaultOptions.tsconfig = {
configFile,
references: 'auto',
}
}
}
return {
...defaultOptions,
...options,
}
}