rc-config-loader 4.0.0 → 4.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -2
- package/lib/rc-config-loader.d.ts +2 -12
- package/lib/rc-config-loader.js +11 -12
- package/lib/rc-config-loader.js.map +1 -1
- package/lib/types.d.ts +21 -0
- package/lib/types.js +3 -0
- package/lib/types.js.map +1 -0
- package/package.json +19 -19
- package/src/rc-config-loader.ts +29 -41
- package/src/types.ts +32 -0
package/README.md
CHANGED
|
@@ -103,10 +103,18 @@ const config = loadRcFile("your-application");
|
|
|
103
103
|
console.log(config); // => rcfile content
|
|
104
104
|
```
|
|
105
105
|
|
|
106
|
+
It will check these files and return config file if found it.
|
|
107
|
+
|
|
108
|
+
- `.your-applicationrc.json`
|
|
109
|
+
- `.your-applicationrc.yml`
|
|
110
|
+
- `.your-applicationrc.yaml`
|
|
111
|
+
- `.your-applicationrc.js`
|
|
112
|
+
- [optional] `package.json`
|
|
113
|
+
- if `packageJSON` option is enabled
|
|
114
|
+
|
|
106
115
|
### Example
|
|
107
116
|
|
|
108
117
|
```js
|
|
109
|
-
"use strict";
|
|
110
118
|
import { rcFile } from "rc-config-loader"
|
|
111
119
|
// load .eslintrc from current dir
|
|
112
120
|
console.log(rcFile("eslint"));
|
|
@@ -123,7 +131,7 @@ config: { extends: 'standard',
|
|
|
123
131
|
filePath: ${__dirname}/test/fixtures/.eslintrc
|
|
124
132
|
*/
|
|
125
133
|
|
|
126
|
-
// load property from
|
|
134
|
+
// load property from package.json
|
|
127
135
|
console.log(rcFile("rc-config-loader", {
|
|
128
136
|
packageJSON: {
|
|
129
137
|
fieldName: "directories"
|
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
packageJSON?: boolean | {
|
|
3
|
-
fieldName: string;
|
|
4
|
-
};
|
|
5
|
-
configFileName?: string;
|
|
6
|
-
defaultExtension?: string | string[];
|
|
7
|
-
cwd?: string;
|
|
8
|
-
}
|
|
1
|
+
import type { PossibleUndefined, rcConfigLoaderOption, rcConfigResult } from "./types";
|
|
9
2
|
/**
|
|
10
3
|
* Find and load rcfile, return { config, filePath }
|
|
11
4
|
* If not found any rcfile, throw an Error.
|
|
@@ -13,7 +6,4 @@ export interface rcConfigLoaderOption {
|
|
|
13
6
|
* @param {rcConfigLoaderOption} [opts]
|
|
14
7
|
* @returns {{ config: Object, filePath:string } | undefined}
|
|
15
8
|
*/
|
|
16
|
-
export declare function rcFile<R extends {}>(pkgName: string, opts?: rcConfigLoaderOption):
|
|
17
|
-
config: R;
|
|
18
|
-
filePath: string;
|
|
19
|
-
} | undefined;
|
|
9
|
+
export declare function rcFile<R extends {}>(pkgName: string, opts?: rcConfigLoaderOption): PossibleUndefined<rcConfigResult<R>>;
|
package/lib/rc-config-loader.js
CHANGED
|
@@ -13,20 +13,19 @@ const require_from_string_1 = __importDefault(require("require-from-string"));
|
|
|
13
13
|
const json5_1 = __importDefault(require("json5"));
|
|
14
14
|
const debug = require("debug")("rc-config-loader");
|
|
15
15
|
const defaultLoaderByExt = {
|
|
16
|
+
".cjs": loadJSConfigFile,
|
|
16
17
|
".js": loadJSConfigFile,
|
|
17
18
|
".json": loadJSONConfigFile,
|
|
18
19
|
".yaml": loadYAMLConfigFile,
|
|
19
|
-
".yml": loadYAMLConfigFile
|
|
20
|
+
".yml": loadYAMLConfigFile
|
|
20
21
|
};
|
|
21
22
|
const defaultOptions = {
|
|
22
|
-
// does look for `package.json`
|
|
23
23
|
packageJSON: false,
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
cwd: process.cwd(),
|
|
24
|
+
defaultExtension: [".json", ".yaml", ".yml", ".js", ".cjs"],
|
|
25
|
+
cwd: process.cwd()
|
|
27
26
|
};
|
|
28
27
|
const selectLoader = (defaultLoaderByExt, extension) => {
|
|
29
|
-
if (!
|
|
28
|
+
if (!defaultOptions.defaultExtension.includes(extension)) {
|
|
30
29
|
throw new Error(`${extension} is not supported.`);
|
|
31
30
|
}
|
|
32
31
|
return defaultLoaderByExt[extension];
|
|
@@ -56,7 +55,7 @@ function rcFile(pkgName, opts = {}) {
|
|
|
56
55
|
loadersByOrder,
|
|
57
56
|
configFileName,
|
|
58
57
|
packageJSON,
|
|
59
|
-
packageJSONFieldName
|
|
58
|
+
packageJSONFieldName
|
|
60
59
|
});
|
|
61
60
|
}
|
|
62
61
|
exports.rcFile = rcFile;
|
|
@@ -67,7 +66,7 @@ exports.rcFile = rcFile;
|
|
|
67
66
|
* filePath: string
|
|
68
67
|
* }}
|
|
69
68
|
*/
|
|
70
|
-
function findConfig({ parts, loaderByExt, loadersByOrder, configFileName, packageJSON, packageJSONFieldName
|
|
69
|
+
function findConfig({ parts, loaderByExt, loadersByOrder, configFileName, packageJSON, packageJSONFieldName }) {
|
|
71
70
|
const extensions = Object.keys(loaderByExt);
|
|
72
71
|
while (extensions.length) {
|
|
73
72
|
const ext = extensions.shift();
|
|
@@ -86,7 +85,7 @@ function findConfig({ parts, loaderByExt, loadersByOrder, configFileName, packag
|
|
|
86
85
|
}
|
|
87
86
|
return {
|
|
88
87
|
config: result,
|
|
89
|
-
filePath: configLocation
|
|
88
|
+
filePath: configLocation
|
|
90
89
|
};
|
|
91
90
|
}
|
|
92
91
|
for (let i = 0; i < loaders.length; i++) {
|
|
@@ -97,7 +96,7 @@ function findConfig({ parts, loaderByExt, loadersByOrder, configFileName, packag
|
|
|
97
96
|
}
|
|
98
97
|
return {
|
|
99
98
|
config: result,
|
|
100
|
-
filePath: configLocation
|
|
99
|
+
filePath: configLocation
|
|
101
100
|
};
|
|
102
101
|
}
|
|
103
102
|
}
|
|
@@ -108,7 +107,7 @@ function findConfig({ parts, loaderByExt, loadersByOrder, configFileName, packag
|
|
|
108
107
|
if (pkgJSON[packageJSONFieldName]) {
|
|
109
108
|
return {
|
|
110
109
|
config: pkgJSON[packageJSONFieldName],
|
|
111
|
-
filePath: pkgJSONLoc
|
|
110
|
+
filePath: pkgJSONLoc
|
|
112
111
|
};
|
|
113
112
|
}
|
|
114
113
|
}
|
|
@@ -128,7 +127,7 @@ function loadJSConfigFile(filePath, suppress) {
|
|
|
128
127
|
debug(`Loading JavaScript config file: ${filePath}`);
|
|
129
128
|
try {
|
|
130
129
|
const content = fs_1.default.readFileSync(filePath, "utf-8");
|
|
131
|
-
return require_from_string_1.default(content, filePath);
|
|
130
|
+
return (0, require_from_string_1.default)(content, filePath);
|
|
132
131
|
}
|
|
133
132
|
catch (error) {
|
|
134
133
|
debug(`Error reading JavaScript file: ${filePath}`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rc-config-loader.js","sourceRoot":"","sources":["../src/rc-config-loader.ts"],"names":[],"mappings":";;;;;;AAAA,iBAAiB;AACjB,sBAAsB;AACtB,6CAA6C;AAC7C,gDAAwB;AACxB,4CAAoB;AACpB,8EAAoD;AACpD,kDAA0B;
|
|
1
|
+
{"version":3,"file":"rc-config-loader.js","sourceRoot":"","sources":["../src/rc-config-loader.ts"],"names":[],"mappings":";;;;;;AAAA,iBAAiB;AACjB,sBAAsB;AACtB,6CAA6C;AAC7C,gDAAwB;AACxB,4CAAoB;AACpB,8EAAoD;AACpD,kDAA0B;AAW1B,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,kBAAkB,CAAC,CAAC;AAEnD,MAAM,kBAAkB,GAAuB;IAC3C,MAAM,EAAE,gBAAgB;IACxB,KAAK,EAAE,gBAAgB;IACvB,OAAO,EAAE,kBAAkB;IAC3B,OAAO,EAAE,kBAAkB;IAC3B,MAAM,EAAE,kBAAkB;CAC7B,CAAC;AAEF,MAAM,cAAc,GAC6B;IAC7C,WAAW,EAAE,KAAK;IAClB,gBAAgB,EAAE,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,CAAC;IAC3D,GAAG,EAAE,OAAO,CAAC,GAAG,EAAE;CACrB,CAAC;AAEF,MAAM,YAAY,GAAG,CAAC,kBAAsC,EAAE,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|
+
export declare type ExtensionName = ".cjs" | ".js" | ".json" | ".yaml" | ".yml";
|
|
2
|
+
export declare type RequiredOption = "packageJSON" | "defaultExtension" | "cwd";
|
|
3
|
+
export declare type Loader = <R extends {}>(fileName: string, supperes: boolean) => R;
|
|
4
|
+
export declare type ExtensionLoaderMap = Record<ExtensionName, Loader>;
|
|
5
|
+
export declare type PossibleUndefined<T> = T | undefined;
|
|
6
|
+
export interface rcConfigResult<R extends Record<string, unknown>> {
|
|
7
|
+
config: R;
|
|
8
|
+
filePath: string;
|
|
9
|
+
}
|
|
10
|
+
export interface rcConfigLoaderOption {
|
|
11
|
+
/** does look for `package.json` */
|
|
12
|
+
packageJSON?: boolean | {
|
|
13
|
+
fieldName: string;
|
|
14
|
+
};
|
|
15
|
+
/** if config file name is not same with packageName, set the name */
|
|
16
|
+
configFileName?: string;
|
|
17
|
+
/** treat default(no ext file) as some extension */
|
|
18
|
+
defaultExtension?: ExtensionName | ExtensionName[];
|
|
19
|
+
/** where start to load */
|
|
20
|
+
cwd?: string;
|
|
21
|
+
}
|
package/lib/types.js
ADDED
package/lib/types.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rc-config-loader",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.1.1",
|
|
4
4
|
"description": "load config file from .{product}rc.{json,yml,js}",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"config",
|
|
@@ -33,10 +33,11 @@
|
|
|
33
33
|
},
|
|
34
34
|
"scripts": {
|
|
35
35
|
"build": "tsc -p .",
|
|
36
|
-
"prettier": "prettier --write '**/*.{js,jsx,ts,tsx,css}'",
|
|
37
36
|
"prepublish": "npm run --if-present build",
|
|
38
37
|
"test": "mocha \"test/**/*.{js,ts}\"",
|
|
39
|
-
"watch": "tsc -p . --watch"
|
|
38
|
+
"watch": "tsc -p . --watch",
|
|
39
|
+
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\"",
|
|
40
|
+
"prepare": "git config --local core.hooksPath .githooks"
|
|
40
41
|
},
|
|
41
42
|
"husky": {
|
|
42
43
|
"hooks": {
|
|
@@ -50,27 +51,26 @@
|
|
|
50
51
|
]
|
|
51
52
|
},
|
|
52
53
|
"prettier": {
|
|
54
|
+
"singleQuote": false,
|
|
53
55
|
"printWidth": 120,
|
|
54
|
-
"tabWidth": 4
|
|
56
|
+
"tabWidth": 4,
|
|
57
|
+
"trailingComma": "none"
|
|
55
58
|
},
|
|
56
59
|
"dependencies": {
|
|
57
|
-
"debug": "^4.
|
|
58
|
-
"js-yaml": "^4.
|
|
59
|
-
"json5": "^2.1
|
|
60
|
+
"debug": "^4.3.4",
|
|
61
|
+
"js-yaml": "^4.1.0",
|
|
62
|
+
"json5": "^2.2.1",
|
|
60
63
|
"require-from-string": "^2.0.2"
|
|
61
64
|
},
|
|
62
65
|
"devDependencies": {
|
|
63
|
-
"@types/
|
|
64
|
-
"@types/
|
|
65
|
-
"@types/
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
"ts-node": "^9.1.1",
|
|
73
|
-
"ts-node-test-register": "^9.0.1",
|
|
74
|
-
"typescript": "^4.1.3"
|
|
66
|
+
"@types/mocha": "^9.1.0",
|
|
67
|
+
"@types/node": "^17.0.23",
|
|
68
|
+
"@types/require-from-string": "^1.2.1",
|
|
69
|
+
"lint-staged": "^12.3.7",
|
|
70
|
+
"mocha": "^9.2.2",
|
|
71
|
+
"prettier": "^2.6.2",
|
|
72
|
+
"ts-node": "^10.7.0",
|
|
73
|
+
"ts-node-test-register": "^10.0.0",
|
|
74
|
+
"typescript": "^4.6.3"
|
|
75
75
|
}
|
|
76
76
|
}
|
package/src/rc-config-loader.ts
CHANGED
|
@@ -5,42 +5,35 @@ import path from "path";
|
|
|
5
5
|
import fs from "fs";
|
|
6
6
|
import requireFromString from "require-from-string";
|
|
7
7
|
import JSON5 from "json5";
|
|
8
|
+
import type {
|
|
9
|
+
PossibleUndefined,
|
|
10
|
+
rcConfigLoaderOption,
|
|
11
|
+
rcConfigResult,
|
|
12
|
+
RequiredOption,
|
|
13
|
+
ExtensionName,
|
|
14
|
+
ExtensionLoaderMap,
|
|
15
|
+
Loader
|
|
16
|
+
} from "./types";
|
|
8
17
|
|
|
9
18
|
const debug = require("debug")("rc-config-loader");
|
|
10
|
-
|
|
19
|
+
|
|
20
|
+
const defaultLoaderByExt: ExtensionLoaderMap = {
|
|
21
|
+
".cjs": loadJSConfigFile,
|
|
11
22
|
".js": loadJSConfigFile,
|
|
12
23
|
".json": loadJSONConfigFile,
|
|
13
24
|
".yaml": loadYAMLConfigFile,
|
|
14
|
-
".yml": loadYAMLConfigFile
|
|
25
|
+
".yml": loadYAMLConfigFile
|
|
15
26
|
};
|
|
16
27
|
|
|
17
|
-
const defaultOptions
|
|
18
|
-
|
|
28
|
+
const defaultOptions: Required<Pick<rcConfigLoaderOption, RequiredOption>> &
|
|
29
|
+
Omit<rcConfigLoaderOption, RequiredOption> = {
|
|
19
30
|
packageJSON: false,
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
cwd: process.cwd(),
|
|
31
|
+
defaultExtension: [".json", ".yaml", ".yml", ".js", ".cjs"],
|
|
32
|
+
cwd: process.cwd()
|
|
23
33
|
};
|
|
24
34
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
packageJSON?:
|
|
28
|
-
| boolean
|
|
29
|
-
| {
|
|
30
|
-
fieldName: string;
|
|
31
|
-
};
|
|
32
|
-
// if config file name is not same with packageName, set the name
|
|
33
|
-
configFileName?: string;
|
|
34
|
-
// treat default(no ext file) as some extension
|
|
35
|
-
defaultExtension?: string | string[];
|
|
36
|
-
// where start to load
|
|
37
|
-
cwd?: string;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
type Loader = <R extends object>(fileName: string, suppress: boolean) => R;
|
|
41
|
-
|
|
42
|
-
const selectLoader = (defaultLoaderByExt: { [index: string]: Loader }, extension: string) => {
|
|
43
|
-
if (![".json", ".yaml", ".yml", ".js"].includes(extension)) {
|
|
35
|
+
const selectLoader = (defaultLoaderByExt: ExtensionLoaderMap, extension: ExtensionName): Loader => {
|
|
36
|
+
if (!defaultOptions.defaultExtension.includes(extension)) {
|
|
44
37
|
throw new Error(`${extension} is not supported.`);
|
|
45
38
|
}
|
|
46
39
|
return defaultLoaderByExt[extension];
|
|
@@ -56,12 +49,7 @@ const selectLoader = (defaultLoaderByExt: { [index: string]: Loader }, extension
|
|
|
56
49
|
export function rcFile<R extends {}>(
|
|
57
50
|
pkgName: string,
|
|
58
51
|
opts: rcConfigLoaderOption = {}
|
|
59
|
-
):
|
|
60
|
-
| {
|
|
61
|
-
config: R;
|
|
62
|
-
filePath: string;
|
|
63
|
-
}
|
|
64
|
-
| undefined {
|
|
52
|
+
): PossibleUndefined<rcConfigResult<R>> {
|
|
65
53
|
// path/to/config or basename of config file.
|
|
66
54
|
const configFileName = opts.configFileName || `.${pkgName}rc`;
|
|
67
55
|
const defaultExtension = opts.defaultExtension || defaultOptions.defaultExtension;
|
|
@@ -76,7 +64,7 @@ export function rcFile<R extends {}>(
|
|
|
76
64
|
|
|
77
65
|
const loaderByExt = {
|
|
78
66
|
...defaultLoaderByExt,
|
|
79
|
-
"": loadersByOrder
|
|
67
|
+
"": loadersByOrder
|
|
80
68
|
};
|
|
81
69
|
return findConfig<R>({
|
|
82
70
|
parts,
|
|
@@ -84,7 +72,7 @@ export function rcFile<R extends {}>(
|
|
|
84
72
|
loadersByOrder,
|
|
85
73
|
configFileName,
|
|
86
74
|
packageJSON,
|
|
87
|
-
packageJSONFieldName
|
|
75
|
+
packageJSONFieldName
|
|
88
76
|
});
|
|
89
77
|
}
|
|
90
78
|
|
|
@@ -101,7 +89,7 @@ function findConfig<R extends {}>({
|
|
|
101
89
|
loadersByOrder,
|
|
102
90
|
configFileName,
|
|
103
91
|
packageJSON,
|
|
104
|
-
packageJSONFieldName
|
|
92
|
+
packageJSONFieldName
|
|
105
93
|
}: {
|
|
106
94
|
parts: string[];
|
|
107
95
|
loaderByExt: {
|
|
@@ -135,7 +123,7 @@ function findConfig<R extends {}>({
|
|
|
135
123
|
}
|
|
136
124
|
return {
|
|
137
125
|
config: result,
|
|
138
|
-
filePath: configLocation
|
|
126
|
+
filePath: configLocation
|
|
139
127
|
};
|
|
140
128
|
}
|
|
141
129
|
for (let i = 0; i < loaders.length; i++) {
|
|
@@ -146,7 +134,7 @@ function findConfig<R extends {}>({
|
|
|
146
134
|
}
|
|
147
135
|
return {
|
|
148
136
|
config: result,
|
|
149
|
-
filePath: configLocation
|
|
137
|
+
filePath: configLocation
|
|
150
138
|
};
|
|
151
139
|
}
|
|
152
140
|
}
|
|
@@ -158,7 +146,7 @@ function findConfig<R extends {}>({
|
|
|
158
146
|
if (pkgJSON[packageJSONFieldName]) {
|
|
159
147
|
return {
|
|
160
148
|
config: pkgJSON[packageJSONFieldName],
|
|
161
|
-
filePath: pkgJSONLoc
|
|
149
|
+
filePath: pkgJSONLoc
|
|
162
150
|
};
|
|
163
151
|
}
|
|
164
152
|
}
|
|
@@ -182,7 +170,7 @@ function loadJSConfigFile(filePath: string, suppress: boolean) {
|
|
|
182
170
|
try {
|
|
183
171
|
const content = fs.readFileSync(filePath, "utf-8");
|
|
184
172
|
return requireFromString(content, filePath);
|
|
185
|
-
} catch (error) {
|
|
173
|
+
} catch (error: any) {
|
|
186
174
|
debug(`Error reading JavaScript file: ${filePath}`);
|
|
187
175
|
if (!suppress) {
|
|
188
176
|
error.message = `Cannot read config file: ${filePath}\nError: ${error.message}`;
|
|
@@ -196,7 +184,7 @@ function loadJSONConfigFile(filePath: string, suppress: boolean) {
|
|
|
196
184
|
|
|
197
185
|
try {
|
|
198
186
|
return JSON5.parse(readFile(filePath));
|
|
199
|
-
} catch (error) {
|
|
187
|
+
} catch (error: any) {
|
|
200
188
|
debug(`Error reading JSON file: ${filePath}`);
|
|
201
189
|
if (!suppress) {
|
|
202
190
|
error.message = `Cannot read config file: ${filePath}\nError: ${error.message}`;
|
|
@@ -216,7 +204,7 @@ function loadYAMLConfigFile(filePath: string, suppress: boolean) {
|
|
|
216
204
|
try {
|
|
217
205
|
// empty YAML file can be null, so always use
|
|
218
206
|
return yaml.load(readFile(filePath)) || {};
|
|
219
|
-
} catch (error) {
|
|
207
|
+
} catch (error: any) {
|
|
220
208
|
debug(`Error reading YAML file: ${filePath}`);
|
|
221
209
|
if (!suppress) {
|
|
222
210
|
error.message = `Cannot read config file: ${filePath}\nError: ${error.message}`;
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export type ExtensionName = ".cjs" | ".js" | ".json" | ".yaml" | ".yml";
|
|
2
|
+
|
|
3
|
+
export type RequiredOption = "packageJSON" | "defaultExtension" | "cwd";
|
|
4
|
+
|
|
5
|
+
export type Loader = <R extends {}>(fileName: string, supperes: boolean) => R;
|
|
6
|
+
|
|
7
|
+
export type ExtensionLoaderMap = Record<ExtensionName, Loader>;
|
|
8
|
+
|
|
9
|
+
export type PossibleUndefined<T> = T | undefined;
|
|
10
|
+
|
|
11
|
+
export interface rcConfigResult<R extends Record<string, unknown>> {
|
|
12
|
+
config: R;
|
|
13
|
+
filePath: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface rcConfigLoaderOption {
|
|
17
|
+
/** does look for `package.json` */
|
|
18
|
+
packageJSON?:
|
|
19
|
+
| boolean
|
|
20
|
+
| {
|
|
21
|
+
fieldName: string;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/** if config file name is not same with packageName, set the name */
|
|
25
|
+
configFileName?: string;
|
|
26
|
+
|
|
27
|
+
/** treat default(no ext file) as some extension */
|
|
28
|
+
defaultExtension?: ExtensionName | ExtensionName[];
|
|
29
|
+
|
|
30
|
+
/** where start to load */
|
|
31
|
+
cwd?: string;
|
|
32
|
+
}
|