@ecoding/base.build 0.0.9 → 0.0.10

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.
@@ -6,7 +6,7 @@ const getEntries = () => {
6
6
  return entry.pagesObj;
7
7
  }
8
8
  return {
9
- app: rootPath("./src/app.tsx")
9
+ app: rootPath("./src/index.tsx")
10
10
  };
11
11
  };
12
12
 
@@ -0,0 +1,13 @@
1
+ const { rootPath, ssrEntry } = require("../z-helpers/paths");
2
+ const { customConfig } = require("../z-helpers/config");
3
+
4
+ const getEntries = () => {
5
+ if (customConfig.mpa) {
6
+ return ssrEntry.pagesObj;
7
+ }
8
+ return {
9
+ app: rootPath("./src/app.tsx")
10
+ };
11
+ };
12
+
13
+ module.exports = getEntries;
@@ -0,0 +1,13 @@
1
+ const { packageJSON } = require("../z-helpers/paths");
2
+ const nodeExternals = require("webpack-node-externals");
3
+
4
+ const getExternals = () => {
5
+ return {
6
+ // "@alilc/lowcode-engine": "var window.AliLowCodeEngine",
7
+ // "@alilc/lowcode-engine-ext": "var window.AliLowCodeEngineExt",
8
+ ...nodeExternals(),
9
+ ...Object.keys(packageJSON.dependencies)
10
+ };
11
+ };
12
+
13
+ module.exports = getExternals;
@@ -0,0 +1,27 @@
1
+ const getTsRule = require("./ts-loader");
2
+ const getImgRule = require("./img-loader");
3
+ const getSvgRule = require("./svg-loader");
4
+ const getFontRule = require("./font-loader");
5
+
6
+ const getModule = () => {
7
+ const tsRule = getTsRule();
8
+ const imgRule = getImgRule();
9
+ const svgRule = getSvgRule();
10
+ const fontRule = getFontRule();
11
+
12
+ return {
13
+ // noParse:/test.js$/, 不编译正则匹配上的文件
14
+ rules: [
15
+ tsRule,
16
+ {
17
+ test: /\.(styl|css|less)$/, //node端不能 require('xx.css'),会报错
18
+ loader: "null"
19
+ },
20
+ imgRule,
21
+ svgRule,
22
+ fontRule
23
+ ]
24
+ };
25
+ };
26
+
27
+ module.exports = getModule;
@@ -3,7 +3,7 @@ const babelLoader = require("./babel");
3
3
 
4
4
  const getTsRule = () => {
5
5
  return {
6
- test: /\.tsx?$/,
6
+ test: /\.(tsx?|jsx?)$/,
7
7
  use: [babelLoader],
8
8
  // 指定要编译的路径
9
9
  include: [rootPath("src"), /\/node_modules\/@ecoding.*/],
@@ -1,8 +1,9 @@
1
- const getMPAOptimization = require('./mpa');
2
- const getSPAOptimization = require('./spa');
3
-
1
+ const getMPAOptimization = require("./mpa");
2
+ const getSPAOptimization = require("./spa");
3
+ const getSSROptimization = require("./ssr");
4
4
 
5
5
  module.exports = {
6
6
  getMPAOptimization,
7
- getSPAOptimization
8
- };
7
+ getSPAOptimization,
8
+ getSSROptimization
9
+ };
@@ -13,56 +13,63 @@ const { customConfig } = require("../z-helpers/config");
13
13
  * size:文件大小
14
14
  */
15
15
  const getOptimization = () => {
16
- return {
16
+ const obj = {
17
17
  chunkIds: isEnvDevelopment ? "named" : "deterministic",
18
18
  moduleIds: isEnvDevelopment ? "named" : "deterministic",
19
- runtimeChunk: {
20
- // 单独分割运行时代码 webpack 运行时所需代码
21
- name: (entrypoint) => `wp-runtime-${entrypoint.name}`
22
- },
23
19
  minimize: isEnvDevelopment ? false : true,
24
20
  minimizer: [
25
21
  new TerserPlugin({ parallel: true }), // parallel:多进程压缩
26
22
  new CssMinimizerPlugin()
27
- ],
28
- // 这里的体积大小时指压缩前的
29
- // 如果不想代码分割,设置 splitChunks: false,通常用于微前端或微模块
30
- splitChunks: customConfig.dll
31
- ? false
32
- : {
33
- chunks: "all", // initial | all | async all 表明了在异步和同步之间共享
34
- minSize: 10000, // 生成 chunk 的最小体积(以 bytes 为单位) 不限制
35
- maxSize: 500000, // 封装的模块的最小体积 0 不限制
36
- minRemainingSize: 10000, // 分割后剩下的模块大小 0 不限制
37
- minChunks: 1, // 模块的最小被引用次数
38
- maxAsyncRequests: 30, // 异步模块按需加载的最大并行请求数 import('xxx.js').then
39
- maxInitialRequests: 30, // 同步模块最大并行请求数
40
- // enforceSizeThreshold: 1000000000, // 如果代码块体积超过了 50000,则强制打包,无视其他配置
41
- cacheGroups: {
42
- // 缓存组
43
- vendors: {
44
- name: "vendors",
45
- test: /[\\/]node_modules[\\/]/,
46
- // test: /[\\/]node_modules[\\/](react|react-dom|react-router-dom|redux|react-redux|redux-thunk|connected-react-router|hoist-non-react-statics)/,
47
- // minSize: 0,
48
- enforce: true, // ignore splitChunks.minSize, splitChunks.minChunks, splitChunks.maxAsyncRequests and splitChunks.maxInitialRequests
49
- priority: -10
50
- },
51
- // vendors2: {
52
- // name: 'vendors2',
53
- // test: /[\\/]node_modules[\\/](axios)/,
54
- // // minSize: 0,
55
- // priority: -10,
56
- // },
57
- default: {
58
- test: /[\\/]src[\\/]/,
59
- minChunks: 2, // 如果一个模块被引用2次的话,会单独提取
60
- priority: -20, // 优先级
61
- reuseExistingChunk: true // 是否使用其他已被分割出去的代码块
62
- }
63
- }
64
- }
23
+ ]
65
24
  };
25
+
26
+ if (customConfig.dll) {
27
+ obj.splitChunks = false;
28
+ } else {
29
+ // 这里的体积大小时指压缩前的
30
+ obj.splitChunks = {
31
+ chunks: "all", // initial | all | async all 表明了在异步和同步之间共享
32
+ minSize: 10000, // 生成 chunk 的最小体积(以 bytes 为单位) 不限制
33
+ maxSize: 500000, // 封装的模块的最小体积 0 不限制
34
+ minRemainingSize: 10000, // 分割后剩下的模块大小 0 不限制
35
+ minChunks: 1, // 模块的最小被引用次数
36
+ maxAsyncRequests: 30, // 异步模块按需加载的最大并行请求数 import('xxx.js').then
37
+ maxInitialRequests: 30, // 同步模块最大并行请求数
38
+ // enforceSizeThreshold: 1000000000, // 如果代码块体积超过了 50000,则强制打包,无视其他配置
39
+ cacheGroups: {
40
+ // 缓存组
41
+ vendors: {
42
+ name: "vendors",
43
+ test: /[\\/]node_modules[\\/]/,
44
+ // test: /[\\/]node_modules[\\/](react|react-dom|react-router-dom|redux|react-redux|redux-thunk|connected-react-router|hoist-non-react-statics)/,
45
+ // minSize: 0,
46
+ enforce: true, // ignore splitChunks.minSize, splitChunks.minChunks, splitChunks.maxAsyncRequests and splitChunks.maxInitialRequests
47
+ priority: -10
48
+ },
49
+ // vendors2: {
50
+ // name: 'vendors2',
51
+ // test: /[\\/]node_modules[\\/](axios)/,
52
+ // // minSize: 0,
53
+ // priority: -10,
54
+ // },
55
+ default: {
56
+ test: /[\\/]src[\\/]/,
57
+ minChunks: 2, // 如果一个模块被引用2次的话,会单独提取
58
+ priority: -20, // 优先级
59
+ reuseExistingChunk: true // 是否使用其他已被分割出去的代码块
60
+ }
61
+ }
62
+ };
63
+ }
64
+
65
+ if (customConfig.micro !== "child") {
66
+ obj.runtimeChunk = {
67
+ // 单独分割运行时代码 webpack 运行时所需代码
68
+ name: (entrypoint) => `wp-runtime-${entrypoint.name}`
69
+ };
70
+ }
71
+
72
+ return obj;
66
73
  };
67
74
 
68
75
  module.exports = getOptimization;
@@ -0,0 +1,28 @@
1
+ const TerserPlugin = require("terser-webpack-plugin");
2
+ const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
3
+ const { isEnvDevelopment } = require("../z-helpers/util");
4
+
5
+ /** optimization 配置
6
+ * module: 每一个文件其实都可以看成一个module
7
+ * chunk:webpack打包最终生成的代码块,代码块会生成文件,一个文件对应一个chunk
8
+ * chunkIds、moduleIds:natural | named | deterministic(生产环境默认) | size,chunk 命名算法
9
+ * natural:自然数 1,2,3
10
+ * named:文件名
11
+ * deterministic:简短hash
12
+ * size:文件大小
13
+ */
14
+ const getOptimization = () => {
15
+ const obj = {
16
+ chunkIds: isEnvDevelopment ? "named" : "deterministic",
17
+ moduleIds: isEnvDevelopment ? "named" : "deterministic",
18
+ minimize: isEnvDevelopment ? false : true,
19
+ minimizer: [
20
+ new TerserPlugin({ parallel: true }), // parallel:多进程压缩
21
+ new CssMinimizerPlugin()
22
+ ]
23
+ };
24
+ obj.splitChunks = false;
25
+ return obj;
26
+ };
27
+
28
+ module.exports = getOptimization;
@@ -0,0 +1,34 @@
1
+ const { rootPath } = require("../z-helpers/paths");
2
+ const { customConfig } = require("../z-helpers/config");
3
+
4
+ const pubPath = () => {
5
+ const dev = customConfig.dev || {};
6
+ switch (process.env.NODE_ENV) {
7
+ case "production":
8
+ return customConfig.publicPath || "";
9
+ default:
10
+ return dev.publicPath || "/";
11
+ }
12
+ };
13
+
14
+ const getOutput = () => {
15
+ return Object.assign({
16
+ // 输出文件目录(将来所有资源输出的公共目录)
17
+ path: rootPath(customConfig.outputPath || "./dist"),
18
+
19
+ /**
20
+ * 所有资源引入公共路径前缀 --> 'imgs/a.jpg' --> '/imgs/a.jpg'
21
+ * 注意:可在业务代码中 动态修改 output 内的publicPath
22
+ * if(...) {
23
+ __webpack_public_path__ = 'https://cdn.com'
24
+ }
25
+ */
26
+ publicPath: pubPath(),
27
+
28
+ // 入口代码块文件名称(指定名称+目录)
29
+ filename: "[name].server-entry.js",
30
+ libraryTarget: "commonjs2"
31
+ });
32
+ };
33
+
34
+ module.exports = getOutput;
@@ -45,7 +45,7 @@ const getHtmlWebpack = () => {
45
45
  // favicon: rootPath('./src/assets/img/favicon.ico')
46
46
  };
47
47
  if (isEnvProduction) {
48
- obj.output = rootPath("./dist");
48
+ // obj.output = rootPath("./dist");
49
49
  obj.minify = {
50
50
  minimize: true,
51
51
  removeConments: true,
@@ -65,7 +65,7 @@ const getHtmlWebpack = () => {
65
65
  inject: "body"
66
66
  };
67
67
  if (isEnvProduction) {
68
- obj.output = rootPath("./dist");
68
+ // obj.output = rootPath("./dist");
69
69
  obj.minify = {
70
70
  minimize: true,
71
71
  removeConments: true,
@@ -0,0 +1,18 @@
1
+ const WebpackBar = require("webpackbar");
2
+ const MiniCssExtractPlugin = require("mini-css-extract-plugin"); // 提取分离css
3
+ const { isEnvProduction } = require("../z-helpers/util");
4
+
5
+ module.exports = () => {
6
+ const plugins = [
7
+ new WebpackBar({
8
+ profile: true
9
+ }),
10
+
11
+ isEnvProduction &&
12
+ new MiniCssExtractPlugin({
13
+ // filename: 'css/[name].css',
14
+ filename: "[id].[chunkhash:5].css"
15
+ })
16
+ ].filter(Boolean);
17
+ return plugins;
18
+ };
@@ -0,0 +1,76 @@
1
+ const { Production, Development } = require("./z-helpers/const");
2
+ const { isEnvDevelopment } = require("./z-helpers/util");
3
+ const { customConfig } = require("./z-helpers/config");
4
+ const getEntries = require("./entries/ssr");
5
+ const getOutput = require("./output/ssr");
6
+ const { getSSROptimization } = require("./optimization");
7
+ const getModule = require("./module/ssr");
8
+ const { getResolve, getResolveLoader } = require("./resolve");
9
+ const getPlugins = require("./plugins/ssr");
10
+ const getExternals = require("./externals/ssr");
11
+
12
+ /**
13
+ *
14
+ * @param {*} cmdOpts : build时 { WEBPACK_BUNDLE: true, WEBPACK_BUILD: true } | 本地开发dev时 { WEBPACK_SERVE: true }
15
+ * @param {*} runtimeOpts :
16
+ * {
17
+ mode: 'development',
18
+ config: [ './deploy/webpack.config.js' ],
19
+ env: { WEBPACK_SERVE: true }
20
+ }
21
+ * @returns
22
+ */
23
+ const getWebpackConfig = (env) => {
24
+ // 打包环境
25
+ const mode = isEnvDevelopment ? Development : Production;
26
+
27
+ // 配置缓存chunk
28
+ /**
29
+ * 'filesystem', // filesystem(prod默认) | memory(dev默认)
30
+ */
31
+ const cache = {
32
+ type: isEnvDevelopment ? "memory" : "filesystem"
33
+ };
34
+
35
+ // 配置入口 entry
36
+ const entry = getEntries();
37
+
38
+ // 配置输出bundle output
39
+ const output = getOutput();
40
+
41
+ // 配置resolveLoader
42
+ const resolveLoader = getResolveLoader();
43
+
44
+ // 配置optimization
45
+ const optimization = getSSROptimization();
46
+
47
+ // 配置loaders
48
+ const module = getModule();
49
+
50
+ // 配置resolve
51
+ const resolve = getResolve();
52
+
53
+ // 配置plugins
54
+ const plugins = getPlugins();
55
+
56
+ // 配置externals
57
+ const externals = getExternals();
58
+
59
+ const config = {
60
+ target: "node",
61
+ mode,
62
+ cache,
63
+ entry,
64
+ output,
65
+ resolveLoader,
66
+ optimization,
67
+ module,
68
+ resolve,
69
+ plugins,
70
+ externalsPresets: { node: true }, // 不打包 node 模块,什么 fs path 那些
71
+ externals
72
+ };
73
+ return config;
74
+ };
75
+
76
+ module.exports = getWebpackConfig;
@@ -5,15 +5,16 @@ const cwd = process.cwd();
5
5
  const rootPath = (pathname) => {
6
6
  return resolve(cwd, pathname);
7
7
  };
8
+ const packageJSON = require(rootPath("./package.json"));
8
9
 
9
10
  const resolvePath = resolve;
10
11
 
11
- const getEntries = () => {
12
+ const getEntries = (filename) => {
12
13
  const dirPath = rootPath("./src/pages");
13
14
  const pages = fs.readdirSync(dirPath);
14
15
  const pagesObj = {};
15
16
  pages.forEach((pageName) => {
16
- pagesObj[pageName] = rootPath(`./src/pages/${pageName}/index.tsx`);
17
+ pagesObj[pageName] = rootPath(`./src/pages/${pageName}/${filename}`);
17
18
  });
18
19
  return {
19
20
  pagesObj,
@@ -24,5 +25,7 @@ const getEntries = () => {
24
25
  module.exports = {
25
26
  rootPath,
26
27
  resolvePath,
27
- entry: getEntries()
28
+ packageJSON,
29
+ entry: getEntries("index.tsx"),
30
+ ssrEntry: getEntries("index.server.tsx")
28
31
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ecoding/base.build",
3
- "version": "0.0.9",
3
+ "version": "0.0.10",
4
4
  "description": "tpl building",
5
5
  "author": "cxc",
6
6
  "license": "MIT",
@@ -42,15 +42,16 @@
42
42
  "postcss-loader": "7.0.0",
43
43
  "style-loader": "3.3.1",
44
44
  "terser-webpack-plugin": "5.3.3",
45
- "typescript": "4.7.4",
45
+ "typescript": "4.8.3",
46
46
  "webpack": "5.73.0",
47
47
  "webpack-cli": "4.10.0",
48
48
  "webpack-dev-server": "4.9.2",
49
49
  "webpack-merge": "5.8.0",
50
+ "webpack-node-externals": "^3.0.0",
50
51
  "webpackbar": "5.0.2"
51
52
  },
52
53
  "publishConfig": {
53
54
  "access": "public"
54
55
  },
55
- "gitHead": "dba212f7c4b60a4ff6136340c0df65d90244a2f7"
56
+ "gitHead": "c360835b00f1834e9958a5a0fb7d0d4185cad6f7"
56
57
  }