akfun 5.1.9 → 5.1.11

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 CHANGED
@@ -1,161 +1,88 @@
1
- # AKFun 前端脚手架
2
- > AKFun 是一个基于 Webpack rollup 的前端多场景打包工具,支持多种技术栈:Vue技术栈、React技术栈、React&TS技术栈
3
- - 核心理念:提供完整&全面的前端工程能力,并尽可能屏蔽掉前端工程相关配置,让开发者更专注业务研发工作。
4
- - 技术栈:node/webpack/rollup/babel/eslint/stylelint
5
-
6
- ## 特性
7
- - ⚡️ 零配置,开箱即用
8
- - 👏 支持Vue和React项目的调试和构建
9
- - 支持单页面和多页面
10
- - 提供三种构建场景: 本地开发模式(包含热更新、接口代理等功能)、生产环境代码构建、library库构建(支持umd和esm的打包能力)
11
- - 开放配置能力: 可配置构建入口文件、开启ESLint代码检测、接口代理等
12
- - 支持 [Autoprefixer](https://github.com/postcss/autoprefixer#readme)、[Sass](https://sass-lang.com/)、[PostCSS](https://postcss.org/)、[ESLint](http://eslint.cn/)、[StyleLint](https://stylelint.io/)
13
- - 支持项目系统参数自动批量替换 [params-replace-loader](https://www.npmjs.com/package/params-replace-loader)
14
- - 提供完整的Vue和React项目模板
15
-
16
- ## 快速开始 / 使用方法一
17
- > 全局安装AKFun,用于同时管理多个前端项目代码(可使用AKFun创建一个新项目)
18
-
19
- 1. **全局安装**
1
+ ## AKFun 前端脚手架
2
+ AKFun 是一个基于 Webpack Rollup 的多场景前端打包工具,支持 VueReactReact+TS 技术栈,致力于提供“零配置、开箱即用”的工程能力,让开发者专注业务。
3
+
4
+ ### 主要特性
5
+ - **零配置**: 内置默认配置,开箱即用;
6
+ - **多技术栈**: 支持 Vue、React、React+TS 的调试与构建;
7
+ - **多构建场景**: 本地开发(含热更新/代理)、生产构建、库构建(UMD/ESM);
8
+ - **灵活可配**: 支持入口、别名、代理、SASS 注入、ESLint/StyleLint、Babel/Loader/Plugin 扩展等配置;
9
+ - **样式与规范**: 集成 Autoprefixer、Sass、PostCSS、ESLint、StyleLint;
10
+ - **参数替换**: 支持基于 [params-replace-loader](https://www.npmjs.com/package/params-replace-loader) 的环境变量批量替换;
11
+ - **模板支持**: 提供完整的 Vue/React 项目模板。
12
+
13
+ ## 快速开始
14
+
15
+ ### 方法一:全局安装
16
+ 1) 安装
20
17
  ```bash
21
- $ yarn global add akfun 或者 npm i -g akfun
18
+ yarn global add akfun
19
+ # 或
20
+ npm i -g akfun
22
21
  ```
23
-
24
- 2. **初始化一个项目**
25
- > 可选择项目类型:vue或者react,默认react类型的项目,也可通过--dir参数指定存放项目模板的目录
22
+ 2) 创建项目(可指定模板与目录)
26
23
  ```bash
27
- $ akfun init -t=vue
24
+ akfun init -t=vue
25
+ # 指定目录
26
+ akfun init -t=vue --dir=myTest1
28
27
  ```
28
+ 3) 运行构建(需先安装依赖)
29
+ ```bash
30
+ # 本地开发调试
31
+ akfun dev
29
32
 
30
- 3. **开始使用:以全局命令方式构建项目(确保已yarn install或者npm install)**
33
+ # 生产环境构建
34
+ akfun build
31
35
 
32
- ```bash
33
- # 1、开启本地调试模式
34
- $ akfun dev
35
- ```
36
+ # 构建库(UMD)
37
+ akfun build2lib
36
38
 
37
- ```bash
38
- # 2、构建生产环境代码
39
- $ akfun build
39
+ # 构建库(ESM)
40
+ akfun build2esm
40
41
  ```
41
42
 
43
+ ### 方法二:在现有项目中使用
44
+ 1) 安装到当前项目
42
45
  ```bash
43
- # 3、构建第三方功能包(以umd格式输出)
44
- $ akfun build2lib
46
+ yarn add akfun --dev
47
+ #
48
+ npm i akfun --save-dev
45
49
  ```
46
-
50
+ 2) 在 package.json 添加脚本
47
51
  ```bash
48
- # 4、构建第三方功能包的esm输出格式
49
- $ akfun build2esm
52
+ "dev": "akfun dev",
53
+ "build": "akfun build",
54
+ "build2lib": "akfun build2lib",
55
+ "build2esm": "akfun build2esm"
50
56
  ```
51
-
52
- ## 快速开始 / 使用方法二
53
- > 在现有项目中局部安装AKFun,给现有项目赋予AKFun的前端工程能力
54
-
55
- 1. **本地安装**
56
-
57
+ 3) 初始化配置文件(按需调整入口、别名、代理等)
57
58
  ```bash
58
- $ yarn add akfun --dev 或者 npm i akfun --save-dev
59
+ akfun config init
59
60
  ```
60
-
61
- 2. **在package.json中创建可执行脚本**
62
- > 打开package.json,在scripts中新增三条可执行命令
63
-
61
+ 4) 开发与构建
64
62
  ```bash
65
- # 用于开启本地调试模式
66
- "dev": "akfun dev"
67
-
68
- # 用于构建生产环境代码
69
- "build": "akfun build"
70
-
71
- # 用于构建第三方功能包(以umd格式输出)
72
- "build2lib": "akfun build2lib"
73
-
74
- # 用于构建第三方功能包(以esm格式输出)
75
- "build2esm": "akfun build2esm"
63
+ npm run dev
64
+ npm run build
65
+ npm run build2lib
66
+ npm run build2esm
76
67
  ```
77
68
 
78
- 3. **创建AKFun的配置文件**
79
- > 需要根据实际情况调整配置文件内(比如:入口文件、路径缩写、接口代理等)
80
-
81
- ```bash
82
- $ akfun config init
83
- ```
84
-
85
- 4. **开始构建当前项目**
86
-
87
- 4.1 开启本地调试模式
88
- ```bash
89
- $ npm run dev
90
- ```
91
- 4.2 构建生产环境代码
92
- ```bash
93
- $ npm run build
94
- ```
95
- 4.3 构建第三方功能包(以umd格式输出)
96
- ```bash
97
- $ npm run build2lib
98
- ```
99
- 4.4 构建第三方功能包(以esm格式输出)
100
- ```bash
101
- $ npm run build2esm
102
- ```
69
+ ## 常用命令
70
+ - **akfun init**: 交互式创建项目(支持 -t、--dir)。
71
+ - **akfun config init**: 在当前项目生成 `akfun.config.js`。
72
+ - **akfun dev**: 本地开发调试(含热更新、接口代理、可选 HTTPS、可选 ESLint/StyleLint)。
73
+ - **akfun build**: 生产环境构建(压缩优化、可选分析)。
74
+ - **akfun build2lib**: 构建 UMD 库产物。
75
+ - **akfun build2esm**: 构建 ESM 库产物。
103
76
 
104
- ## AKFun使用说明
77
+ ## 多页面与模板
78
+ - 当 `entry` 仅配置一个且对应文件不存在时,会自动从 `src/pages` 扫描以 `.ts/.tsx/.js/.jsx` 结尾的文件作为入口,匹配同名 HTML 作为模板(对应正则 `/\.[tj]sx?$/`)。
79
+ - 仅 `dev` 和 `build` 使用页面模板;`build2lib` 不向页面注入打包产物。
80
+ - 优先使用 `./src/index.html`;不存在时使用内置默认模板。多页面时若 `pages` 下存在同名 HTML,将其作为页面模板。
105
81
 
106
- 1. **使用AKFun新建一个新项目**
82
+ ## 配置说明(akfun.config.js)
83
+ AKFun 默认提供完整配置;如需自定义,执行 `akfun config init` 生成 `akfun.config.js` 并按需修改。以下为常用配置。
107
84
 
108
- 1.1 创建一个react项目
109
- ```bash
110
- $ akfun init
111
- ```
112
- 1.2 创建一个vue类型项目
113
- ```bash
114
- $ akfun init -t=vue
115
- ```
116
- 1.3 在指定的目录中创建一个新项目
117
- ```bash
118
- $ akfun init -t=vue --dir=myTest1
119
- ```
120
-
121
- 2. **创建AKFun的配置文件**
122
- ```bash
123
- $ akfun config init
124
- ```
125
-
126
- 3. **关于AKFun提供三种构建场景**
127
- 1. **dev**: 本地开发调试模式,用于本地开发和调试项目(包含热更新、接口代理等功能),编译的代码没有压缩,默认会开启ESLint检测代码规范(可关闭)
128
- 2. **build**: 用于构建生产环境代码,编译输出的代码会进行压缩优化
129
- 3. **build2lib**: 用于构建library库,以umd进行输出
130
- 4. **build2esm**: 用于构建library库,以esm进行输出
131
-
132
- 4. **关于AKFun的配置文件**
133
- 1. AKFun会提供全量的默认配置,实现零配置、开箱即用的能力
134
- 2. 自定义构建配置,请在当前项目根目录创建AKFun配置文件(akfun.config.js),AKFun提供初始化配置文件的方法:
135
- ```bash
136
- $ akfun config init
137
- ```
138
- 3. akfun.config.js为当前项目的配置文件,优先级最高(可覆盖AKFun提供的默认配置)
139
-
140
- 5. **配置构建入口文件(webpack.entry)**
141
- 1. 默认的构建入口文件: ./src/index.js
142
- 2. 自定义构建入口(akfun.config.js中提供对应的配置入口)
143
- 1. 在webpack.entry配置构建入口,dev\build\build2lib都会以此为构建入口 ([关于entry的配置方法](https://www.webpackjs.com/configuration/entry-context/#entry))
144
- 2. 在dev.entry、build.entry、build2lib.entry中配置对应执行环境的构建入口,优先级高于webpack.entry
145
-
146
- 6. **关于多页面**
147
- 1. 当akfun.config.js的entry只有一个入口配置,且对应的构建入口文件不存在时,AKFun会自动从src/pages中获取构建入口(支持多页面多模板)
148
- 2. 多页面模式下,会自动将src/pages中以.ts、.tsx、.js、.jsx结尾(对应的匹配正则:/\.[tj]sx?$/)的文件作为构建入口文件,同时将同名的html文件作为当前页面模板
149
-
150
- 7. **关于多页面多模板**
151
- 1. 只有dev和build的构建过程中才会使用到页面模板,build2lib构建中不会将打包完成的代码输出到页面模板中
152
- 2. 默认使用./src/index.html作为页面模板
153
- 3. 当项目中./src/index.html不存在时,会使用AKFun的提供的默认页面模板
154
- 4. 多页面模式时,如果pages下存在对应的html页面(与入口文件同名的html文件),会自动将其设置为页面模板
155
-
156
- ## AKFun开放的配置能力
157
- > AKFun配置文件(akfun.config.js),以下使用AKFunConfig代表akfun.config.js配置对象
158
- 1. 开启/关闭 ESLint代码规范检测: AKFunConfig.settings.enableEslint (也可配置StyleLint的使用)
85
+ ### 1) 基础规范与检查
159
86
  ```bash
160
87
  module.exports = {
161
88
  settings: {
@@ -167,37 +94,29 @@ module.exports = {
167
94
  ...
168
95
  }
169
96
  ```
170
- 2. 配置构建入口文件: 关于配置优先级请查看 AKFun使用说明 / 配置构建入口文件
171
- > 以下是entry的配置位置,具体配置方法请查看Webpack官网 ([关于entry的配置方法](https://www.webpackjs.com/configuration/entry-context/#entry))
172
- > 备注:建议以key/value形式(object { <key>: string | [string] })配置entry
97
+
98
+ ### 2) 构建入口(优先级:dev/build/build2lib.entry > webpack.entry
99
+ > 备注:建议以 key/valueobject { <key>: string | [string] })配置 entry。详情参考 Webpack 文档:[关于 entry 的配置方法](https://www.webpackjs.com/configuration/entry-context/#entry)
173
100
  ```bash
174
101
  module.exports = {
175
102
  ...
176
103
  webpack: {
177
- entry: {
178
- index: './src/index.js',
179
- }
104
+ entry: { index: './src/index.js' },
180
105
  },
181
106
  ...
182
- dev: {
183
- entry: {}
184
- }
185
- build: {
186
- entry: {}
187
- }
188
- build2lib: {
189
- entry: {}
190
- }
107
+ dev: { entry: {} },
108
+ build: { entry: {} },
109
+ build2lib: { entry: {} },
191
110
  build2esm: {
192
111
  input: resolve('src/main.js'),
193
112
  fileName: 'index',
194
- }
113
+ },
195
114
  ...
196
115
  }
197
116
  ```
198
117
 
199
- 3. 解析(resolve) / extensions配置: 自动解析确定的扩展(配置可识别的文件后缀)
200
- > 以下是extensions的配置位置,具体配置方法请查看Webpack官网 ([关于resolve-extensions的配置方法](https://www.webpackjs.com/configuration/resolve/#resolve-extensions))
118
+ ### 3) 解析配置(extensions
119
+ > 详情参考 Webpack 文档:[关于 resolve.extensions 的配置方法](https://www.webpackjs.com/configuration/resolve/#resolve-extensions)
201
120
  ```bash
202
121
  module.exports = {
203
122
  ...
@@ -210,8 +129,8 @@ module.exports = {
210
129
  }
211
130
  ```
212
131
 
213
- 4. 解析(resolve) / alias配置: 创建 import 或 require 的别名,来确保模块引入变得更简单
214
- > 以下是alias的配置位置,具体配置方法请查看Webpack官网 ([关于resolve-alias的配置方法](https://www.webpackjs.com/configuration/resolve/#resolve-alias))
132
+ ### 4) 别名配置(alias
133
+ > 详情参考 Webpack 文档:[关于 resolve.alias 的配置方法](https://www.webpackjs.com/configuration/resolve/#resolve-alias)
215
134
  ```bash
216
135
  module.exports = {
217
136
  ...
@@ -223,68 +142,46 @@ module.exports = {
223
142
  ...
224
143
  }
225
144
  ```
226
- 5. 页面模板路径配置:关于页面模板请查看 AKFun使用说明 / 关于页面模板
227
- ```bash
228
- module.exports = {
229
- ...
230
- webpack: {
231
- template: '',
232
- }
233
- ...
234
- }
235
- ```
236
145
 
237
- 6. 注入公共的SASS文件
238
- > 为项目中每个.scss后缀的样式文件注入公共的SASS内容(变量、mixin、function等)
146
+ ### 5) 页面模板与样式资源
239
147
  ```bash
240
148
  module.exports = {
241
149
  ...
242
150
  webpack: {
243
- sassResources: [],
151
+ template: '', // 自定义页面模板
152
+ sassResources: [], // 为每个 .scss 注入公共 SASS(变量、mixin、function 等)
244
153
  }
245
154
  ...
246
155
  }
247
156
  ```
248
157
 
249
- 7. 打包忽略node_modules配置项: ignoreNodeModules(默认为false
250
- > 打包过程中,忽略node_modules中的依赖文件,不注入到bundle中,减少最后生成代码体积
158
+ ### 6) 依赖打包策略(忽略 node_modules)
159
+ > 打包过程中忽略 node_modules 依赖,减少最终体积;可通过 allowList 指定需注入 bundle 的依赖。
251
160
  ```bash
252
161
  module.exports = {
253
162
  ...
254
163
  webpack: {
255
- ignoreNodeModules: true,
164
+ ignoreNodeModules: true, // 是否忽略node_modules中的依赖文件
256
165
  allowList: [], // 用于配置会注入bundle中的依赖包(ignoreNodeModules为true时生效)
257
166
  }
258
167
  ...
259
168
  }
260
169
  ```
261
170
 
262
- 8. 是否生成ts声明文件配置项: createDeclaration(默认为false)
263
- > 构建ts项目中,可以选择是否生成ts声明文件
264
- ```bash
265
- module.exports = {
266
- ...
267
- webpack: {
268
- createDeclaration: true,
269
- }
270
- ...
271
- }
272
- ```
273
-
274
- 9. 配置项目源码目录(工程有效目录范围): projectDir
275
- > 构建项目中,设置生效的目录(可同时设置多个目录),用于提高前端工程执行效率。可以不配置,默认为['./src']
171
+ ### 7) TypeScript 声明文件与工程目录
276
172
  ```bash
277
173
  module.exports = {
278
174
  ...
279
175
  webpack: {
280
- projectDir: ['./src'],
176
+ createDeclaration: true, // 是否生成ts声明文件
177
+ projectDir: ['./src'], // 可配置多个目录,用于提升工程执行效率
281
178
  }
282
179
  ...
283
180
  }
284
181
  ```
285
182
 
286
- 10. 项目源码环境变量批量替换
287
- > [关于params-replace-loader的使用方法](https://www.npmjs.com/package/params-replace-loader)
183
+ ### 8) 环境变量替换(params-replace-loader)
184
+ > [关于 params-replace-loader 的使用方法](https://www.npmjs.com/package/params-replace-loader)
288
185
  ```bash
289
186
  module.exports = {
290
187
  ...
@@ -302,116 +199,119 @@ module.exports = {
302
199
  }
303
200
  ```
304
201
 
305
- 11. 接口代理配置:目前只有dev本地开发调试模式下会启动
306
- > [关于proxyTable的配置方法](https://www.webpackjs.com/configuration/dev-server/#devserver-proxy)
202
+ ### 9) 开发服务(代理与基础配置)
203
+ > [关于 proxyTable 的配置方法](https://www.webpackjs.com/configuration/dev-server/#devserver-proxy)
307
204
  ```bash
308
205
  module.exports = {
309
206
  ...
310
207
  dev: {
311
- proxyTable: {
208
+ NODE_ENV: 'development', // development 模式,不会启动UglifyJsPlugin服务
209
+ port: 80, // 启动server服务的端口
210
+ autoOpenBrowser: true, // 是否自动打开页面
211
+ assetsPublicPath: '/', // 设置静态资源的引用路径(根域名+路径)
212
+ assetsSubDirectory: '', // 资源引用二级路径
213
+ hostname: 'localhost', // 自动打开的页面主机
214
+ proxyTable: { // 接口代理
215
+ '/apiTest': {
216
+ target: 'http://api-test.com.cn', // 不支持跨域的接口根地址
217
+ ws: true,
218
+ changeOrigin: true
219
+ }
312
220
  },
313
- }
221
+ cssSourceMap: false,
222
+ },
314
223
  ...
315
224
  }
316
225
  ```
317
226
 
318
- 12. 用于开启本地调试模式的相关配置信息
227
+ ### 10) 生产构建
319
228
  ```bash
320
229
  module.exports = {
321
230
  ...
322
- dev: {
323
- NODE_ENV: 'development', // development 模式,不会启动UglifyJsPlugin服务
324
- port: 80, // 启动server服务的端口
325
- autoOpenBrowser: true, // 是否自动打开页面
326
- assetsPublicPath: '/', // 设置静态资源的引用路径(根域名+路径)
327
- assetsSubDirectory: '', // 资源引用二级路径
328
- hostname: 'localhost', // 自动打开的页面主机
329
- proxyTable: { // 接口代理
330
- '/apiTest': {
331
- target: 'http://api-test.com.cn', // 不支持跨域的接口根地址
332
- ws: true,
333
- changeOrigin: true
334
- }
335
- },
336
- cssSourceMap: false,
337
- },
231
+ build: {
232
+ NODE_ENV: 'production', // production 模式,会启动UglifyJsPlugin服务
233
+ assetsRoot: resolve('./dist'), // 打包后的文件绝对路径(物理路径)
234
+ assetsPublicPath: '/', // 设置静态资源的引用路径(根域名+路径)
235
+ assetsSubDirectory: '', // 资源引用二级路径
236
+ productionSourceMap: false, // 是否显示原始源代码
237
+ productionGzip: false, // 是否开启Gzip服务
238
+ productionGzipExtensions: ['js', 'css', 'json'], // Gzip识别的文件后缀
239
+ bundleAnalyzerReport: false, // 开启打包分析功能
240
+ }
338
241
  ...
339
242
  }
340
243
  ```
341
244
 
342
- 13. 用于构建生产环境代码的相关配置信息
245
+ ### 11) 库构建(UMD)
343
246
  ```bash
344
247
  module.exports = {
345
248
  ...
346
- build: {
347
- NODE_ENV: 'production', // production 模式,会启动UglifyJsPlugin服务
348
- assetsRoot: resolve('./dist'), // 打包后的文件绝对路径(物理路径)
349
- assetsPublicPath: '/', // 设置静态资源的引用路径(根域名+路径)
350
- assetsSubDirectory: '', // 资源引用二级路径
351
- productionSourceMap: false, // 是否显示原始源代码
352
- productionGzip: false, // 是否开启Gzip服务
353
- productionGzipExtensions: ['js', 'css', 'json'], // Gzip识别的文件后缀
354
- bundleAnalyzerReport: false, // 开启打包分析功能
355
- }
249
+ build2lib: {
250
+ NODE_ENV: 'production', // production 模式,会启动UglifyJsPlugin服务
251
+ libraryName: '', // 构建第三方功能包时最后导出的引用变量名
252
+ assetsRoot: resolve('dist'), // 编译完成的文件存放路径
253
+ assetsPublicPath: '/', // 设置静态资源的引用路径(根域名+路径)
254
+ assetsSubDirectory: '', // 资源引用二级路径
255
+ productionSourceMap: false, // 是否显示原始源代码
256
+ productionGzip: false, // 是否开启Gzip服务
257
+ productionGzipExtensions: ['js', 'css', 'json'], // Gzip识别的文件后缀
258
+ bundleAnalyzerReport: false, // 开启打包分析功能
259
+ },
356
260
  ...
357
261
  }
358
262
  ```
359
263
 
360
- 14. 用于构建第三方功能包的配置(以umd格式输出)
264
+ ### 12) 库构建(ESM)
361
265
  ```bash
362
266
  module.exports = {
363
267
  ...
364
- build2lib: {
365
- NODE_ENV: 'production', // production 模式,会启动UglifyJsPlugin服务
366
- libraryName: '', // 构建第三方功能包时最后导出的引用变量名
367
- assetsRoot: resolve('dist'), // 编译完成的文件存放路径
368
- assetsPublicPath: '/', // 设置静态资源的引用路径(根域名+路径)
369
- assetsSubDirectory: '', // 资源引用二级路径
370
- productionSourceMap: false, // 是否显示原始源代码
371
- productionGzip: false, // 是否开启Gzip服务
372
- productionGzipExtensions: ['js', 'css', 'json'], // Gzip识别的文件后缀
373
- bundleAnalyzerReport: false, // 开启打包分析功能
374
- },
268
+ build2esm: {
269
+ input: resolve('src/main.js'), // 入口文件
270
+ fileName: 'index', // 输出的文件名称
271
+ svgDir: 'src/icons/**', // 用于设置当前项目的icon所在目录,避免被 @rollup/plugin-image 编译成base64格式
272
+ },
375
273
  ...
376
274
  }
377
275
  ```
378
276
 
379
- 15. 用于构建esm格式的第三方功能包配置
277
+ ### 13) 自定义 Loader / Plugin / Babel Plugins
380
278
  ```bash
381
279
  module.exports = {
382
- ...
383
- build2esm: {
384
- input: resolve('src/main.js'), // 入口文件
385
- fileName: 'index', // 输出的文件名称
386
- svgDir: 'src/icons/**', // 用于设置当前项目的icon所在目录,避免被 @rollup/plugin-image 编译成base64格式
387
- },
388
- ...
280
+ webpack: {
281
+ moduleRules: [], // 用于添加自定义 loaders
282
+ plugins: [], // 用于添加自定义 plugins
283
+ babelPlugins: [ // 用于添加自定义 Babel plugins
284
+ [
285
+ 'component',
286
+ { libraryName: 'element-ui', styleLibraryName: 'theme-chalk' },
287
+ ],
288
+ ],
289
+ },
389
290
  }
390
291
  ```
292
+ 备注: 以上自定义 babelPlugins 用于实现 [element-ui 组件按需引入](https://element.eleme.cn/#/zh-CN/component/quickstart#an-xu-yin-ru)。
391
293
 
392
- 16. 自定义css-loader的配置
393
- > 比如用于启用/禁用 @import 解析。
294
+ 也支持以函数形式动态调整内置 Babel Plugins:
394
295
  ```bash
395
296
  module.exports = {
396
- ...
397
297
  webpack: {
398
- ...
399
- cssLoaderOption: {
400
- import: false, // 启用/禁用 @import 解析
401
- }
402
- }
403
- ...
298
+ babelPlugins: (curBabelPlugins) => {
299
+ curBabelPlugins.push(/* your plugin */)
300
+ return curBabelPlugins
301
+ },
302
+ },
404
303
  }
405
304
  ```
406
305
 
407
- 17. 自定义postcss-loader的配置
408
- > 比如用于添加 PostCSS 选项与插件。
306
+ ### 14) 自定义 css-loader / 自定义 postcss-loader
409
307
  ```bash
410
308
  module.exports = {
411
309
  ...
412
310
  webpack: {
413
- ...
414
- postCssLoaderOption: {
311
+ cssLoaderOption: {
312
+ import: false, // 启用/禁用 @import 解析
313
+ },
314
+ postCssLoaderOption: { // 自定义postcss-loader的配置
415
315
  postcssOptions: {
416
316
  plugins: [
417
317
  require('postcss-pxtorem')({ // 用于将px自动转化为rem
@@ -426,8 +326,8 @@ module.exports = {
426
326
  }
427
327
  ```
428
328
 
429
- 18. 本地开启https服务
430
- > 使用 https://localhost/index.html 访问当前项目。
329
+ ### 15) 本地 HTTPS
330
+ > 使用 `https://localhost/index.html` 访问当前项目。
431
331
  ```bash
432
332
  module.exports = {
433
333
  ...
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akfun",
3
- "version": "5.1.9",
3
+ "version": "5.1.11",
4
4
  "description": "前端脚手架:支持Vue技术栈和react技术栈",
5
5
  "keywords": [
6
6
  "前端工程",
package/src/dev-server.js CHANGED
@@ -92,15 +92,17 @@ module.exports = function (akfunConfig, _consoleTag) {
92
92
  // serve webpack bundle output
93
93
  app.use(devMiddleware);
94
94
 
95
- // 启动 webpack-hot-middleware,也就是我们常说的 Hot-reload
96
- const hotMiddleware = require('webpack-hot-middleware')(compiler, {
97
- log: false,
98
- heartbeat: 2000
99
- });
95
+ if (!curEnvConfig.closeHotReload) {
96
+ // 启动 webpack-hot-middleware,也就是我们常说的 Hot-reload
97
+ const hotMiddleware = require('webpack-hot-middleware')(compiler, {
98
+ log: false,
99
+ heartbeat: 2000
100
+ });
100
101
 
101
- // enable hot-reload and state-preserving
102
- // compilation error display
103
- app.use(hotMiddleware);
102
+ // enable hot-reload and state-preserving
103
+ // compilation error display
104
+ app.use(hotMiddleware);
105
+ }
104
106
 
105
107
  const afterCreateServerAction = (isHttps, port) => {
106
108
  spinner.succeed(`${consoleTag}调试模式已开启!`);
@@ -29,7 +29,7 @@ const BannerPack = new webpack.BannerPlugin({
29
29
  /**
30
30
  * webpack.base.conf.js
31
31
  * 主要用于设置 rules 和 通用插件
32
- * _curEnvConfig: 执行环境中的配置,比如:dev、build、build2lib等;
32
+ * _curEnvConfig: 执行环境中的配置,用于记录 dev、build、build2lib 等对应的配置内容;
33
33
  * _akfunConfig:完整的配置对象
34
34
  */
35
35
  module.exports = (_curEnvConfig, _akfunConfig) => {
@@ -74,15 +74,7 @@ module.exports = (_curEnvConfig, _akfunConfig) => {
74
74
  * 当webpack试图去加载模块的时候,它默认是查找以 .js 结尾的文件的
75
75
  */
76
76
  resolve: curWebpackConfig.resolve,
77
- externals: curWebpackConfig.ignoreNodeModules
78
- ? [
79
- nodeExternals({
80
- importType: 'commonjs',
81
- additionalModuleDirs: curWebpackConfig.additionalModuleDirs || [],
82
- allowlist: curWebpackConfig.allowList ? curWebpackConfig.allowList : []
83
- })
84
- ].concat(curWebpackConfig.externals)
85
- : curWebpackConfig.externals,
77
+ externals: {},
86
78
  module: {
87
79
  rules: [
88
80
  {
@@ -208,28 +200,37 @@ module.exports = (_curEnvConfig, _akfunConfig) => {
208
200
  new ProgressBarPlugin()
209
201
  ]
210
202
  };
203
+
204
+ let ignoreNodeModules = curWebpackConfig.ignoreNodeModules;
211
205
  // 优先使用执行环境中的配置
212
206
  if (curEnvConfig.ignoreNodeModules !== undefined) {
213
- const allowList = curEnvConfig.allowList || curWebpackConfig.allowList;
214
- const externals = curEnvConfig.externals || curWebpackConfig.external || [];
215
- webpackConfig.externals = curEnvConfig.ignoreNodeModules
216
- ? [
217
- nodeExternals({
218
- importType: 'commonjs',
219
- additionalModuleDirs:
220
- curEnvConfig.additionalModuleDirs || curWebpackConfig.additionalModuleDirs || [],
221
- allowlist: allowList || []
222
- })
223
- ].concat(externals)
224
- : externals;
207
+ ignoreNodeModules = curEnvConfig.ignoreNodeModules;
225
208
  }
209
+
210
+ // allowList 需要开启 ignoreNodeModules 后有效
211
+ const allowList = curEnvConfig.allowList || curWebpackConfig.allowList;
212
+ // 用户手动添加要剔除的依赖
213
+ const externals = curEnvConfig.externals || curWebpackConfig.external || [];
214
+
215
+ // 设置要剔除的依赖
216
+ webpackConfig.externals = ignoreNodeModules
217
+ ? [
218
+ nodeExternals({
219
+ importType: 'commonjs',
220
+ additionalModuleDirs:
221
+ curEnvConfig.additionalModuleDirs || curWebpackConfig.additionalModuleDirs || [],
222
+ allowlist: allowList || []
223
+ })
224
+ ].concat(externals)
225
+ : externals;
226
+
226
227
  // 集成构建入口相关的配置(优先级更高)
227
228
  if (curEnvConfig.entry) {
228
229
  webpackConfig.entry = curEnvConfig.entry; // 会覆盖config.webpack.entry的配置
229
230
  }
230
231
  // 多页面多模板支持能力
231
232
  let entryConfig = webpackConfig.entry || {}; // 获取构建入口配置
232
- const entryFiles = (entryConfig && Object.keys(entryConfig)) || [];
233
+ const entryFiles = Object.keys(entryConfig);
233
234
 
234
235
  if (
235
236
  !webpackConfig.entry ||
@@ -81,10 +81,10 @@ module.exports = (akfunConfig) => {
81
81
  });
82
82
  }
83
83
 
84
- // 开启热更新能力
85
- const devClientPath = path.resolve(__dirname, '../dev-client'); // 从akfun中获取
86
- // add hot-reload related code to entry chunks
87
84
  if (!curEnvConfig.closeHotReload && webpackDevConfig.entry) {
85
+ // 开启热更新能力
86
+ const devClientPath = path.resolve(__dirname, '../dev-client'); // 从akfun中获取
87
+ // add hot-reload related code to entry chunks
88
88
  Object.keys(webpackDevConfig.entry).forEach((name) => {
89
89
  webpackDevConfig.entry[name] = [devClientPath].concat(webpackDevConfig.entry[name]);
90
90
  });