akfun 5.1.3 → 5.1.5

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "akfun",
3
- "version": "5.1.3",
3
+ "version": "5.1.5",
4
4
  "description": "前端脚手架:支持Vue技术栈和react技术栈",
5
5
  "keywords": [
6
6
  "前端工程",
package/src/dev-server.js CHANGED
@@ -21,28 +21,29 @@ module.exports = function (akfunConfig, _consoleTag) {
21
21
  const consoleTag = _consoleTag || curConsoleTag;
22
22
  let config = projectConfig; // 默认使用执行命令目录下的配置数据
23
23
  if (akfunConfig) {
24
- // 参数中的config配置优先级最高
24
+ // 优先使用参数中的config配置
25
25
  config = deepMergeConfig(defaultConfig, akfunConfig);
26
26
  }
27
+ const curEnvConfig = config.dev;
27
28
  // 检查当前npm版本号是否匹配
28
29
  checkVersion();
29
30
  const spinner = ora(`${consoleTag}开启调试模式...`).start();
30
31
  /**
31
32
  * 如果 Node 的环境无法判断当前是 dev / product 环境
32
- * 使用 config.dev.NODE_ENV 作为当前的环境
33
+ * 使用 curEnvConfig.NODE_ENV 作为当前的环境
33
34
  */
34
35
  if (!process.NODE_ENV) {
35
- process.NODE_ENV = config.dev.NODE_ENV;
36
+ process.NODE_ENV = curEnvConfig.NODE_ENV;
36
37
  // 此时process.NODE_ENV = ‘development’;
37
38
  }
38
39
 
39
40
  // default port where dev server listens for incoming traffic
40
- // 如果没有指定运行端口,使用 config.dev.port 作为运行端口
41
- const port = process.env.PORT || config.dev.port;
41
+ // 如果没有指定运行端口,使用 curEnvConfig.port 作为运行端口
42
+ const port = process.env.PORT || curEnvConfig.port;
42
43
 
43
44
  // automatically open browser, if not set will be false
44
45
  // 是否自动打开浏览器
45
- const autoOpenBrowser = !!config.dev.autoOpenBrowser;
46
+ const autoOpenBrowser = !!curEnvConfig.autoOpenBrowser;
46
47
 
47
48
  // 使用 express 启动一个服务
48
49
  const app = express();
@@ -52,9 +53,9 @@ module.exports = function (akfunConfig, _consoleTag) {
52
53
 
53
54
  // Define HTTP proxies to your custom API backend
54
55
  // https://github.com/chimurai/http-proxy-middleware
55
- // 使用 config.dev.proxyTable 的配置作为 proxyTable 的代理配置
56
+ // 使用 curEnvConfig.proxyTable 的配置作为 proxyTable 的代理配置
56
57
  // 备注:需放connect-history-api-fallback前面,避免get请求的代理失效
57
- const proxyTable = config.dev.proxyTable;
58
+ const proxyTable = curEnvConfig.proxyTable;
58
59
  if (proxyTable && JSON.stringify(proxyTable) !== '{}') {
59
60
  // 将 proxyTable 中的请求配置挂在到启动的 express 服务上
60
61
  // proxy api requests
@@ -74,7 +75,10 @@ module.exports = function (akfunConfig, _consoleTag) {
74
75
 
75
76
  // serve pure public assets
76
77
  // 拼接 public 文件夹的静态资源路径
77
- const staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory);
78
+ const staticPath = path.posix.join(
79
+ curEnvConfig.assetsPublicPath,
80
+ curEnvConfig.assetsSubDirectory
81
+ );
78
82
  app.use(staticPath, express.static(resolve('public')));
79
83
 
80
84
  const compiler = webpack(webpackConfig); // 启动 webpack 进行编译
@@ -104,8 +108,8 @@ module.exports = function (akfunConfig, _consoleTag) {
104
108
  process.env.PORT = port;
105
109
 
106
110
  const uri = isHttps
107
- ? `https://${config.dev.hostname}`
108
- : `http://${config.dev.hostname}:${port}`;
111
+ ? `https://${curEnvConfig.hostname}`
112
+ : `http://${curEnvConfig.hostname}:${port}`;
109
113
 
110
114
  console.log(`> Listening at ${uri}\n`);
111
115
 
@@ -117,17 +121,23 @@ module.exports = function (akfunConfig, _consoleTag) {
117
121
  if (entryFiles.length > 0) {
118
122
  // 获取第一个入口文件
119
123
  const filename = entryFiles[0];
120
- console.info(
121
- `当前运行脚本:\n ${projPath}${filename}.js\n当前运行样式[可能不存在]:\n${projPath}${filename}.css`
122
- );
124
+ const consoleInfo = curEnvConfig.consoleInfo || '当前运行脚本';
125
+
126
+ if (curEnvConfig.cssExtract || curEnvConfig.cssExtract === undefined) {
127
+ console.info(
128
+ `${consoleInfo}:\n ${projPath}${filename}.js\n当前可用样式[可能不存在]:\n${projPath}${filename}.css`
129
+ );
130
+ } else {
131
+ console.info(`${consoleInfo}:\n ${projPath}${filename}.js`);
132
+ }
123
133
  // 是否自动打开浏览器并跳转到第一个入口页面
124
- if (!config.dev.closeHtmlWebpackPlugin && autoOpenBrowser) {
134
+ if (!curEnvConfig.closeHtmlWebpackPlugin && autoOpenBrowser) {
125
135
  open(`${projPath}${filename}.html`, { wait: true });
126
136
  }
127
137
  }
128
138
  };
129
139
 
130
- if (config.dev.https) {
140
+ if (curEnvConfig.https) {
131
141
  const sslOptions = {
132
142
  key: fs.readFileSync(path.resolve(__dirname, './ssl/localhost.key')),
133
143
  cert: fs.readFileSync(path.resolve(__dirname, './ssl/localhost.cert')),
@@ -71,15 +71,19 @@ exports.cssLoaders = function (options) {
71
71
  let loaders = [];
72
72
  // 生产环境使用MiniCssExtractPlugin提取css内容,用于提取css内容到一个独立的文件中
73
73
  if (options.environment === 'prod') {
74
- // MiniCssExtractPlugin.loader需要配合MiniCssExtractPlugin使用
74
+ const curEnvConfig = options.envConfig || {};
75
+ const cssExtract = curEnvConfig.cssExtract || curEnvConfig.cssExtract === undefined;
76
+ // MiniCssExtractPlugin.loader 需要配合 MiniCssExtractPlugin 使用
75
77
  loaders = [
76
78
  VueCssLoader,
77
- {
78
- loader: MiniCssExtractPlugin.loader,
79
- options: {
80
- esModule: false // enable a CommonJS syntax using
81
- }
82
- },
79
+ cssExtract
80
+ ? {
81
+ loader: MiniCssExtractPlugin.loader,
82
+ options: {
83
+ esModule: false // enable a CommonJS syntax using
84
+ }
85
+ }
86
+ : undefined,
83
87
  cssLoader,
84
88
  postCssLoader
85
89
  ];
@@ -3,21 +3,24 @@ const utils = require('./loaderUtils');
3
3
  const config = require('../config/index');
4
4
  const isProduction = process.NODE_ENV === 'production';
5
5
 
6
- module.exports = {
7
- loaders: utils.cssLoaders({
8
- cssLoaderUrl: config.webpack.cssLoaderUrl, // 用于自定义css-loader配置项[url]
9
- cssLoaderUrlDir: config.webpack.cssLoaderUrlDir, // 用于设置css-loader配置项[url]的生效目录
10
- cssLoaderOption: config.webpack.cssLoaderOption, // 用于自定义css-loader配置项(优先级最高)
11
- sassOptions: config.webpack.sassOptions, // 用于设置sass-loader配置项
12
- sourceMap: isProduction // 生产环境sourceMap是true
13
- ? config.build.productionSourceMap
14
- : config.dev.cssSourceMap,
15
- environment: isProduction ? 'prod' : 'dev' // 生产环境下:将相关的样式内容提取出来合并到一个文件中
16
- }),
17
- transformToRequire: {
18
- video: 'src',
19
- source: 'src',
20
- img: 'src',
21
- image: 'xlink:href'
22
- }
6
+ module.exports = (curEnvConfig) => {
7
+ return {
8
+ loaders: utils.cssLoaders({
9
+ envConfig: curEnvConfig, // 当前环境变量
10
+ cssLoaderUrl: config.webpack.cssLoaderUrl, // 用于自定义css-loader配置项[url]
11
+ cssLoaderUrlDir: config.webpack.cssLoaderUrlDir, // 用于设置css-loader配置项[url]的生效目录
12
+ cssLoaderOption: config.webpack.cssLoaderOption, // 用于自定义css-loader配置项(优先级最高)
13
+ sassOptions: config.webpack.sassOptions, // 用于设置sass-loader配置项
14
+ sourceMap: isProduction // 生产环境sourceMap是true
15
+ ? config.build.productionSourceMap
16
+ : config.dev.cssSourceMap,
17
+ environment: isProduction ? 'prod' : 'dev' // 生产环境下:将相关的样式内容提取出来合并到一个文件中
18
+ }),
19
+ transformToRequire: {
20
+ video: 'src',
21
+ source: 'src',
22
+ img: 'src',
23
+ image: 'xlink:href'
24
+ }
25
+ };
23
26
  };
@@ -35,6 +35,7 @@ const BannerPack = new webpack.BannerPlugin({
35
35
  module.exports = (_curEnvConfig, _akfunConfig) => {
36
36
  const curEnvConfig = _curEnvConfig || {}; // 用于接收当前运行环境配置变量
37
37
  let config = _akfunConfig || projectConfig; // 默认使用执行命令目录下的配置数据
38
+ // 获取当前项目配置文件中的webpack配置
38
39
  const curWebpackConfig = config.webpack;
39
40
  // 获取当前项目目录
40
41
  const curProjectDir = getProjectDir(curWebpackConfig.projectDir);
@@ -75,12 +76,12 @@ module.exports = (_curEnvConfig, _akfunConfig) => {
75
76
  resolve: curWebpackConfig.resolve,
76
77
  externals: curWebpackConfig.ignoreNodeModules
77
78
  ? [
78
- nodeExternals({
79
- importType: 'commonjs',
80
- additionalModuleDirs: curWebpackConfig.additionalModuleDirs || [],
81
- allowlist: curWebpackConfig.allowList ? curWebpackConfig.allowList : []
82
- })
83
- ].concat(curWebpackConfig.externals)
79
+ nodeExternals({
80
+ importType: 'commonjs',
81
+ additionalModuleDirs: curWebpackConfig.additionalModuleDirs || [],
82
+ allowlist: curWebpackConfig.allowList ? curWebpackConfig.allowList : []
83
+ })
84
+ ].concat(curWebpackConfig.externals)
84
85
  : curWebpackConfig.externals,
85
86
  module: {
86
87
  rules: [
@@ -89,7 +90,7 @@ module.exports = (_curEnvConfig, _akfunConfig) => {
89
90
  use: [
90
91
  {
91
92
  loader: 'vue-loader',
92
- options: vueLoaderConfig // 配置vue-loader相关的loader插件
93
+ options: vueLoaderConfig(curEnvConfig) // 配置vue-loader相关的loader插件
93
94
  }
94
95
  ]
95
96
  },
@@ -130,7 +131,7 @@ module.exports = (_curEnvConfig, _akfunConfig) => {
130
131
  // 图片资源
131
132
  /*
132
133
  url-loader 功能类似于 file-loader,在文件大小(单位 byte)低于指定的限制时,可以返回一个 DataURL。
133
- */
134
+ */
134
135
  test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
135
136
  type: 'asset',
136
137
  issuer: /\.s?css$/,
@@ -213,12 +214,13 @@ module.exports = (_curEnvConfig, _akfunConfig) => {
213
214
  const externals = curEnvConfig.externals || curWebpackConfig.external || [];
214
215
  webpackConfig.externals = curEnvConfig.ignoreNodeModules
215
216
  ? [
216
- nodeExternals({
217
- importType: 'commonjs',
218
- additionalModuleDirs: curEnvConfig.additionalModuleDirs || curWebpackConfig.additionalModuleDirs || [],
219
- allowlist: allowList || []
220
- })
221
- ].concat(externals)
217
+ nodeExternals({
218
+ importType: 'commonjs',
219
+ additionalModuleDirs:
220
+ curEnvConfig.additionalModuleDirs || curWebpackConfig.additionalModuleDirs || [],
221
+ allowlist: allowList || []
222
+ })
223
+ ].concat(externals)
222
224
  : externals;
223
225
  }
224
226
  // 集成构建入口相关的配置(优先级更高)
@@ -2,9 +2,9 @@ const webpack = require('webpack');
2
2
  const path = require('path');
3
3
  const { merge } = require('webpack-merge');
4
4
  const utils = require('./loaderUtils');
5
+ const getBaseWebpackConfig = require('./webpack.base.conf'); // 获取webpack基本配置
5
6
  // 引入当前项目配置文件
6
7
  const projectConfig = require('../config/index');
7
- const getBaseWebpackConfig = require('./webpack.base.conf');
8
8
  const entrys2htmlWebpackPlugin = require('../utils/entrys2htmlWebpackPlugin');
9
9
  const MiniCssExtractPlugin = require('mini-css-extract-plugin');
10
10
  // const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
@@ -39,6 +39,7 @@ module.exports = (akfunConfig) => {
39
39
  },
40
40
  module: {
41
41
  rules: utils.styleLoaders({
42
+ envConfig: curEnvConfig, // 当前环境变量
42
43
  sourceMap: curEnvConfig.cssSourceMap,
43
44
  environment: 'prod', // 'dev': 不会将css单独提取出来
44
45
  cssLoaderUrl: config.webpack.cssLoaderUrl,
@@ -56,15 +57,21 @@ module.exports = (akfunConfig) => {
56
57
  },
57
58
  plugins: [
58
59
  // https://github.com/glenjamin/webpack-hot-middleware#installation--usage
59
- new webpack.HotModuleReplacementPlugin(),
60
+ new webpack.HotModuleReplacementPlugin()
61
+ ]
62
+ });
63
+
64
+ // 支持 cssExtract 配置
65
+ if (curEnvConfig.cssExtract || curEnvConfig.cssExtract === undefined) {
66
+ webpackDevConfig.plugins.push(
60
67
  new MiniCssExtractPlugin({
61
68
  // filename: utils.assetsPath('index.css'),
62
69
  filename: '[name].css',
63
70
  chunkFilename: '[name].css',
64
71
  ignoreOrder: false
65
72
  })
66
- ]
67
- });
73
+ );
74
+ }
68
75
 
69
76
  if (!webpackDevConfig.closeHtmlWebpackPlugin) {
70
77
  // 使用用户自定义的多入口配置,生产对应的多页面多模板(优先使用用户的自定义页面模板)
@@ -10,6 +10,11 @@ const utils = require('./loaderUtils');
10
10
  const projectConfig = require('../config/index');
11
11
  const getBaseWebpackConfig = require('./webpack.base.conf');
12
12
 
13
+ /**
14
+ * 第三方库 webpack 配置,用于构建第三方库(仅构建可用的 umd 模块,其构建产物不会注入到 html 中)
15
+ * @param {*} akfunConfig 当前项目配置
16
+ * @returns
17
+ */
13
18
  module.exports = (akfunConfig) => {
14
19
  let config = akfunConfig || projectConfig; // 默认使用执行命令目录下的配置数据
15
20
  const curEnvConfig = config.build2lib || {}; // 当前执行环境配置
@@ -29,7 +34,7 @@ module.exports = (akfunConfig) => {
29
34
  filename: '[name].umd.js',
30
35
  publicPath: '',
31
36
  library: {
32
- type: 'umd', // 定义打包方式Universal Module Definition,同时支持在CommonJS、AMD和全局变量使用
37
+ type: 'umd', // 定义打包方式Universal Module Definition,同时支持在CommonJS、AMD和全局变量使用
33
38
  name: curEnvConfig.libraryName || '[name]'
34
39
  },
35
40
  // 指定类库名,主要用于直接引用的方式(比如使用script 标签)
@@ -37,6 +42,7 @@ module.exports = (akfunConfig) => {
37
42
  },
38
43
  module: {
39
44
  rules: utils.styleLoaders({
45
+ envConfig: curEnvConfig, // 当前环境变量
40
46
  sourceMap: curEnvConfig.productionSourceMap,
41
47
  environment: 'prod',
42
48
  cssLoaderUrl: config.webpack.cssLoaderUrl,
@@ -60,14 +66,7 @@ module.exports = (akfunConfig) => {
60
66
  new CssMinimizerPlugin()
61
67
  ]
62
68
  },
63
- plugins: [
64
- new MiniCssExtractPlugin({
65
- // filename: utils.assetsPath('index.css'),
66
- filename: '[name].css',
67
- chunkFilename: '[name].css',
68
- ignoreOrder: false
69
- })
70
- ]
69
+ plugins: []
71
70
  });
72
71
 
73
72
  // 优先使用当前环境配置中的output
@@ -88,6 +87,18 @@ module.exports = (akfunConfig) => {
88
87
  );
89
88
  }
90
89
 
90
+ // 支持 cssExtract 配置
91
+ if (curEnvConfig.cssExtract || curEnvConfig.cssExtract === undefined) {
92
+ webpackLibConfig.plugins.push(
93
+ new MiniCssExtractPlugin({
94
+ // filename: utils.assetsPath('index.css'),
95
+ filename: '[name].css',
96
+ chunkFilename: '[name].css',
97
+ ignoreOrder: false
98
+ })
99
+ );
100
+ }
101
+
91
102
  // 判断当前环境是否有自定义plugins
92
103
  if (curEnvConfig.plugins && Array.isArray(curEnvConfig.plugins)) {
93
104
  // 添加自定义webpack插件
@@ -15,6 +15,11 @@ const entrys2htmlWebpackPlugin = require('../utils/entrys2htmlWebpackPlugin');
15
15
  const projectConfig = require('../config/index');
16
16
  const getBaseWebpackConfig = require('./webpack.base.conf');
17
17
 
18
+ /**
19
+ * 生产环境 webpack 配置,用于构建生产环境可运行的代码(构建后的脚本会注入到 html 中)
20
+ * @param {*} akfunConfig 当前项目配置
21
+ * @returns
22
+ */
18
23
  module.exports = (akfunConfig) => {
19
24
  let config = akfunConfig || projectConfig; // 默认使用执行命令目录下的配置数据
20
25
  const curEnvConfig = config.build || {}; // 当前执行环境配置
@@ -50,6 +55,7 @@ module.exports = (akfunConfig) => {
50
55
  },
51
56
  module: {
52
57
  rules: utils.styleLoaders({
58
+ envConfig: curEnvConfig, // 当前环境变量
53
59
  sourceMap: curEnvConfig.productionSourceMap,
54
60
  environment: 'prod',
55
61
  cssLoaderUrl: config.webpack.cssLoaderUrl,
@@ -87,22 +93,7 @@ module.exports = (akfunConfig) => {
87
93
  new CssMinimizerPlugin()
88
94
  ]
89
95
  },
90
- plugins: [
91
- new MiniCssExtractPlugin({
92
- filename: utils.assetsPath('css/[name].[contenthash:8].css'),
93
- ignoreOrder: false // Enable to remove warnings about conflicting order
94
- })
95
- // Compress extracted CSS. We are using this plugin so that possible
96
- // duplicated CSS from different components can be deduped.
97
- /**
98
- * 该插件可以接收以下选项(它们都是可选的):
99
- * assetNameRegExp:表示应优化的资产的名称的正则表达式\最小化,默认为 /\.css$/g
100
- * cssProcessor:用于优化\最小化CSS的CSS处理器,默认为cssnano。
101
- * 这应该是cssnano.process接口之后的一个函数(接收一个CSS和options参数并返回一个Promise)。
102
- * cssProcessorOptions:传递给cssProcessor的选项,默认为 {}
103
- * canPrint:一个布尔值,指示插件是否可以将消息打印到控制台,默认为 true
104
- */
105
- ]
96
+ plugins: []
106
97
  });
107
98
 
108
99
  // 使用用户自定义的多入口配置,生产对应的多页面多模板
@@ -140,6 +131,26 @@ module.exports = (akfunConfig) => {
140
131
  );
141
132
  }
142
133
 
134
+ // 支持 cssExtract 配置
135
+ if (curEnvConfig.cssExtract || curEnvConfig.cssExtract === undefined) {
136
+ webpackProdConfig.plugins.push(
137
+ new MiniCssExtractPlugin({
138
+ filename: utils.assetsPath('css/[name].[contenthash:8].css'),
139
+ ignoreOrder: false // Enable to remove warnings about conflicting order
140
+ })
141
+ );
142
+ // Compress extracted CSS. We are using this plugin so that possible
143
+ // duplicated CSS from different components can be deduped.
144
+ /**
145
+ * MiniCssExtractPlugin 插件可以接收以下选项(它们都是可选的):
146
+ * assetNameRegExp:表示应优化的资产的名称的正则表达式\最小化,默认为 /\.css$/g
147
+ * cssProcessor:用于优化\最小化CSS的CSS处理器,默认为cssnano。
148
+ * 这应该是cssnano.process接口之后的一个函数(接收一个CSS和options参数并返回一个Promise)。
149
+ * cssProcessorOptions:传递给cssProcessor的选项,默认为 {}
150
+ * canPrint:一个布尔值,指示插件是否可以将消息打印到控制台,默认为 true
151
+ */
152
+ }
153
+
143
154
  // 判断当前环境是否有自定义plugins
144
155
  if (curEnvConfig.plugins && Array.isArray(curEnvConfig.plugins)) {
145
156
  // 添加自定义webpack插件