Skip to content

Commit 845f5da

Browse files
authored
支持 resolve alias (#158)
* add resolve in build-config * comment typo
1 parent c68d51b commit 845f5da

File tree

5 files changed

+67
-11
lines changed

5 files changed

+67
-11
lines changed

build-config.md

+26-4
Original file line numberDiff line numberDiff line change
@@ -76,31 +76,31 @@ Build config 各个字段的定义。
7676

7777
类型:`object`
7878

79-
页面,在扩展时会覆盖原值。要求是一个 object,key 为页面名(如 `"index"`),value 为一个 object,包含三个字段:`template`, `entries`, `path`
79+
页面,在扩展时会覆盖原值。要求是一个 object,key 为页面名(如 `"index"`),value 为一个 object,包含三个字段:`template`, `entries`, `path`
8080

8181
`pages` 的字段描述如下:
8282

8383
- **`pages.(.*)`**
8484

8585
类型:`object`
8686

87-
一个 object 表示一个页面,包含三个字段:`template`, `entries`, `path`
87+
一个 object 表示一个页面,包含三个字段:`template`, `entries`, `path`
8888

8989
`pages.(.*)` 的字段描述如下:
9090

9191
- **`pages.(.*).template`**
9292

9393
类型:`string`
9494

95-
页面的模板文件相对于项目根目录的路径,支持 ejs
95+
页面的模板文件相对于项目根目录的路径,支持 ejs
9696

9797
- **`pages.(.*).entries`**
9898

9999
页面上的入口文件列表
100100

101101
* 在只有一个入口文件的情况下,可以直接传入一个字符串,即该入口文件名(如 "index");
102102

103-
* 也可以传入一个数组,数组每一项为一个入口文件名(如 `["sidebar", "index"]`
103+
* 也可以传入一个数组,数组每一项为一个入口文件名(如 `["sidebar", "index"]`
104104

105105
`pages.(.*).entries` 类型为以下几种之一:
106106

@@ -118,6 +118,28 @@ Build config 各个字段的定义。
118118

119119
在应用中该页面的路径正则(如 `""``"^\/financial\/"`),dev server 在请求匹配对应 path 时会返回该页面的内容作为响应。
120120

121+
## **`resolve`**
122+
123+
类型:`object`
124+
125+
对于模块解析行为的配置,在扩展时会合并原值。
126+
127+
`resolve` 的字段描述如下:
128+
129+
- **`resolve.alias`**
130+
131+
类型:`object`
132+
133+
配置别名以控制对特定模块或路径的解析行为;如配置 `{ "foo": "src/foo" }`,则模块 `foo` 会被解析到 `<项目根目录>/src/foo`,模块 `foo/bar` 会被解析到 `<项目根目录>/src/foo/bar`
134+
135+
`resolve.alias` 的字段描述如下:
136+
137+
- **`resolve.alias.(.*)`**
138+
139+
类型:`string`
140+
141+
解析目标的路径(相对于项目根目录),如 `"src/foo"`。
142+
121143
## **`transforms`**
122144

123145
类型:`object`

preset-configs/config.schema.json

+20-4
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@
3434
},
3535
"pages": {
3636
"type": "object",
37-
"description": "页面,在扩展时会覆盖原值。要求是一个 object,key 为页面名(如 `\"index\"`),value 为一个 object,包含三个字段:`template`, `entries`, `path`",
37+
"description": "页面,在扩展时会覆盖原值。要求是一个 object,key 为页面名(如 `\"index\"`),value 为一个 object,包含三个字段:`template`, `entries`, `path`",
3838
"patternProperties": {
3939
".*": {
4040
"type": "object",
41-
"description": "一个 object 表示一个页面,包含三个字段:`template`, `entries`, `path`",
41+
"description": "一个 object 表示一个页面,包含三个字段:`template`, `entries`, `path`",
4242
"properties": {
4343
"template": {
4444
"type": "string",
45-
"description": "页面的模板文件相对于项目根目录的路径,支持 ejs"
45+
"description": "页面的模板文件相对于项目根目录的路径,支持 ejs"
4646
},
4747
"entries": {
48-
"description": "页面上的入口文件列表\n* 在只有一个入口文件的情况下,可以直接传入一个字符串,即该入口文件名(如 \"index\");\n* 也可以传入一个数组,数组每一项为一个入口文件名(如 `[\"sidebar\", \"index\"]`)",
48+
"description": "页面上的入口文件列表\n* 在只有一个入口文件的情况下,可以直接传入一个字符串,即该入口文件名(如 \"index\");\n* 也可以传入一个数组,数组每一项为一个入口文件名(如 `[\"sidebar\", \"index\"]`)",
4949
"oneOf": [{
5050
"type": "string"
5151
}, {
@@ -61,6 +61,22 @@
6161
}
6262
}
6363
},
64+
"resolve": {
65+
"type": "object",
66+
"description": "对于模块解析行为的配置,在扩展时会合并原值。",
67+
"properties": {
68+
"alias": {
69+
"type": "object",
70+
"description": "配置别名以控制对特定模块或路径的解析行为;如配置 `{ \"foo\": \"src/foo\" }`,则模块 `foo` 会被解析到 `<项目根目录>/src/foo`,模块 `foo/bar` 会被解析到 `<项目根目录>/src/foo/bar`",
71+
"patternProperties": {
72+
".*": {
73+
"type": "string",
74+
"description": "解析目标的路径(相对于项目根目录),如 `\"src/foo\"`。"
75+
}
76+
}
77+
}
78+
}
79+
},
6480
"transforms": {
6581
"type": "object",
6682
"description": "构建过程中的转换配置,在扩展时会合并原值,要求是一个 object。key 为文件后缀名,value 为转换信息。转换信息支持两种格式:\n1. 直接使用 transformer 名,如 \"css\"\"less\"\n2. 一个 object,包含两个字段:`transformer` 与 `config`。对于不同的 transformer,我们可以通过与 `transformer` 平级的 `config` 字段对 transformer 的行为进行配置。",

preset-configs/default.json

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
"path": ""
1515
}
1616
},
17+
"resolve": {
18+
"alias": {}
19+
},
1720
"transforms": {
1821
"js": "babel",
1922
"css": "css",

src/utils/build-conf.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ export type PagesInput = Record<string, PageInput>
7575

7676
export type Pages = Record<string, Page>
7777

78+
export interface Resolve {
79+
/** 别名映射表,key 为要匹配的模块或路径,value 为(被实际使用的)目标路径 */
80+
alias: Record<string, string>
81+
}
82+
7883
export interface TransformObject {
7984
transformer: Transform
8085
config?: unknown
@@ -110,6 +115,7 @@ export interface BuildConfigInput {
110115
distDir?: string
111116
entries?: Entries
112117
pages?: PagesInput
118+
resolve?: Resolve
113119
transforms?: TransformsInput
114120
/** 注入到代码中的环境变量 */
115121
envVariables?: EnvVariables,
@@ -123,12 +129,14 @@ export interface BuildConfigInput {
123129

124130
export interface BuildConfig extends Required<BuildConfigInput> {
125131
pages: Pages
132+
resolve: Resolve
126133
transforms: Transforms
127134
}
128135

129136
/** merge two config content */
130137
function mergeConfig(cfg1: BuildConfigInput, cfg2: BuildConfigInput): BuildConfigInput {
131138
return extend(cfg1, cfg2, {
139+
resolve: extend(cfg1.resolve, cfg2.resolve) as Resolve,
132140
transforms: extend(cfg1.transforms, cfg2.transforms) as TransformsInput,
133141
envVariables: extend(cfg1.envVariables, cfg2.envVariables),
134142
optimization: extend(cfg1.optimization, cfg2.optimization) as Optimization,
@@ -265,7 +273,7 @@ function normalizeTransforms(input: TransformsInput): Transforms {
265273

266274
function normalizeConfig({
267275
extends: _extends, publicUrl: _publicUrl, srcDir, staticDir, distDir, entries, pages: _pages,
268-
transforms: _transforms, envVariables, optimization, devProxy,
276+
resolve, transforms: _transforms, envVariables, optimization, devProxy,
269277
deploy, targets, test, engines
270278
}: BuildConfigInput): BuildConfig {
271279
if (_extends == null) throw new Error('Invalid value of field extends')
@@ -275,6 +283,7 @@ function normalizeConfig({
275283
if (distDir == null) throw new Error('Invalid value of field distDir')
276284
if (entries == null) throw new Error('Invalid value of field entries')
277285
if (_pages == null) throw new Error('Invalid value of field pages')
286+
if (resolve == null) throw new Error('Invalid value of field resolve')
278287
if (_transforms == null) throw new Error('Invalid value of field transforms')
279288
if (envVariables == null) throw new Error('Invalid value of field envVariables')
280289
if (optimization == null) throw new Error('Invalid value of field optimization')
@@ -288,7 +297,7 @@ function normalizeConfig({
288297
const transforms = normalizeTransforms(_transforms)
289298
return {
290299
extends: _extends, publicUrl, srcDir, staticDir, distDir, entries, pages,
291-
transforms, envVariables, optimization, devProxy,
300+
resolve, transforms, envVariables, optimization, devProxy,
292301
deploy, targets, test, engines
293302
}
294303
}

src/webpack/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export async function getConfig(): Promise<Configuration> {
2828
const isProd = getEnv() === Env.Prod
2929
const isDev = getEnv() === Env.Dev
3030

31+
const resolveAlias = mapValues(
32+
buildConfig.resolve.alias,
33+
path => abs(path)
34+
)
35+
3136
let config: Configuration = {
3237
target: 'web', // TODO: 使用 `browserslist:...` 可能合适? 详情见 https://webpack.js.org/configuration/target/
3338
mode: getMode(),
@@ -40,7 +45,8 @@ export async function getConfig(): Promise<Configuration> {
4045
'node_modules',
4146
nodeModulesOfBuilder,
4247
abs('node_modules')
43-
]
48+
],
49+
alias: resolveAlias
4450
},
4551
resolveLoader: {
4652
modules: [

0 commit comments

Comments
 (0)