@easyv/react-component-builder 0.24.0 → 0.25.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.
@@ -10,30 +10,46 @@ if (fs.existsSync(webpackConfigExtend)) {
10
10
  webpackConfigExtendFn = require(webpackConfigExtend);
11
11
  }
12
12
 
13
- process.env.NODE_ENV = 'production';
14
-
15
13
  module.exports = (
16
14
  { filename, module_name, version },
17
- { uniqueId, removeConsole },
15
+ { uniqueId, removeConsole, format = 'amd' },
18
16
  ) => {
17
+ // launcher/start.js loads the dev config, which sets NODE_ENV=development.
18
+ // babel-preset-react-app reads BABEL_ENV first, so pin both before compile.
19
+ process.env.BABEL_ENV = 'production';
20
+ process.env.NODE_ENV = 'production';
21
+
19
22
  const entry = path.resolve(appComponents, filename, 'index');
23
+ const isEsm = format === 'esm';
20
24
  const cf = {
21
25
  ...commonConfig({ isProduction: true, uniqueId, entrys: [entry] }),
22
- mode: process.env.NODE_ENV,
26
+ mode: 'production',
23
27
  entry,
24
28
  output: {
25
29
  path: appBuild,
26
30
  filename: `${module_name}.${version}.js`,
27
- library: { name: `${module_name}@${version}`, type: 'amd' },
31
+ pathinfo: false,
32
+ library: isEsm
33
+ ? { type: 'module' }
34
+ : { name: `${module_name}@${version}`, type: 'amd' },
28
35
  },
29
- externals: [
30
- 'react',
31
- 'react-dom',
32
- 'three',
33
- 'lodash',
34
- 'd3',
35
- '@material-ui/styles',
36
- ],
36
+ externals: isEsm
37
+ ? {
38
+ react: 'react',
39
+ 'react-dom': 'react-dom',
40
+ three: 'three',
41
+ lodash: 'lodash',
42
+ d3: 'd3',
43
+ '@material-ui/styles': '@material-ui/styles',
44
+ }
45
+ : [
46
+ 'react',
47
+ 'react-dom',
48
+ 'three',
49
+ 'lodash',
50
+ 'd3',
51
+ '@material-ui/styles',
52
+ ],
37
53
  devtool: false,
38
54
  plugins: [
39
55
  new webpack.ProvidePlugin({
@@ -42,27 +58,36 @@ module.exports = (
42
58
  ReactDom: 'react-dom',
43
59
  }),
44
60
  ],
45
- };
46
-
47
- if (webpackConfigExtendFn) {
48
- return webpackConfigExtendFn(cf, {
49
- production: true,
50
- });
51
- }
52
-
53
- if (removeConsole) {
54
- cf['optimization'] = {
61
+ optimization: {
62
+ minimize: true,
63
+ concatenateModules: true,
55
64
  minimizer: [
56
65
  new TerserWebpackPlugin({
57
66
  parallel: true,
67
+ extractComments: false,
58
68
  terserOptions: {
59
69
  compress: {
60
- drop_console: true,
70
+ drop_console: !!removeConsole,
71
+ passes: 2,
72
+ },
73
+ format: {
74
+ comments: false,
61
75
  },
62
76
  },
63
77
  }),
64
78
  ],
65
- };
79
+ },
80
+ };
81
+
82
+ if (isEsm) {
83
+ cf.experiments = { outputModule: true };
84
+ cf.externalsType = 'module';
85
+ }
86
+
87
+ if (webpackConfigExtendFn) {
88
+ return webpackConfigExtendFn(cf, {
89
+ production: true,
90
+ });
66
91
  }
67
92
 
68
93
  return cf;
@@ -1,5 +1,26 @@
1
1
  const { appSrc, ownLoaders, cliNodeModules } = require("../config/paths");
2
2
 
3
+ function styleUse(isProduction, cssLoaderOptions, extraLoaders) {
4
+ const cssOptions = { esModule: true };
5
+ if (isProduction) cssOptions.sourceMap = false;
6
+ if (cssLoaderOptions) Object.assign(cssOptions, cssLoaderOptions);
7
+
8
+ const loaders = [
9
+ {
10
+ loader: isProduction ? "mini-style-loader" : "style-loader",
11
+ },
12
+ ];
13
+ if (isProduction) {
14
+ loaders.push({ loader: "mini-css-runtime-loader" });
15
+ }
16
+ loaders.push({
17
+ loader: "css-loader",
18
+ options: cssOptions,
19
+ });
20
+ if (extraLoaders) loaders.push.apply(loaders, extraLoaders);
21
+ return loaders;
22
+ }
23
+
3
24
  module.exports = function ({ isProduction, uniqueId, entrys }) {
4
25
  return {
5
26
  stats: "errors-only",
@@ -62,44 +83,33 @@ module.exports = function ({ isProduction, uniqueId, entrys }) {
62
83
  oneOf: [
63
84
  {
64
85
  test: /\.(M|m)odule\.css$/,
65
- use: [
66
- {
67
- loader: "style-loader",
68
- },
69
- {
70
- loader: "css-loader",
71
- options: {
72
- modules: {
73
- localIdentName: isProduction
74
- ? "[name]_[local]_[hash:base64:5]"
75
- : "[path][name]__[local]",
76
- },
77
- },
86
+ use: styleUse(isProduction, {
87
+ modules: {
88
+ localIdentName: isProduction
89
+ ? "[name]_[local]_[hash:base64:5]"
90
+ : "[path][name]__[local]",
78
91
  },
79
- ],
92
+ }),
80
93
  },
81
94
  {
82
- use: ["style-loader", "css-loader"],
95
+ use: styleUse(isProduction),
83
96
  },
84
97
  ],
85
98
  },
86
99
  {
87
100
  test: /\.less$/,
88
101
  include: [appSrc],
89
- use: [
102
+ use: styleUse(
103
+ isProduction,
90
104
  {
91
- loader: "style-loader",
105
+ modules: true,
92
106
  },
93
- {
94
- loader: "css-loader",
95
- options: {
96
- modules: true,
107
+ [
108
+ {
109
+ loader: "less-loader",
97
110
  },
98
- },
99
- {
100
- loader: "less-loader",
101
- },
102
- ],
111
+ ]
112
+ ),
103
113
  },
104
114
  {
105
115
  test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)(\?.*$|$)/,
package/launcher/build.js CHANGED
@@ -18,7 +18,13 @@ function canReadAsset(asset) {
18
18
  }
19
19
 
20
20
  module.exports = (name, option = {}) => {
21
- const { unique = true, removeConsole } = option;
21
+ process.env.BABEL_ENV = 'production';
22
+ process.env.NODE_ENV = 'production';
23
+ const { unique = true, removeConsole, format = 'amd' } = option;
24
+ if (format !== 'amd' && format !== 'esm') {
25
+ console.log(chalk.red(`不支持的打包格式 "${format}",可选:amd、esm`));
26
+ process.exit(1);
27
+ }
22
28
  return new Promise(async (resolve, reject) => {
23
29
  // 检查组件是否存在
24
30
  // 开始打包
@@ -53,10 +59,10 @@ module.exports = (name, option = {}) => {
53
59
  base.filename = `${componentInfo.dirname}`;
54
60
  base.module_name = `${base.module_name}${unique ? '.' + easyvcid : ''}`;
55
61
  const compiler = webpack(
56
- getWebpackConfig(base, { uniqueId: easyvcid, removeConsole }),
62
+ getWebpackConfig(base, { uniqueId: easyvcid, removeConsole, format }),
57
63
  );
58
64
 
59
- console.log('Creating an optimized production build...');
65
+ console.log(`Creating an optimized production build (${format})...`);
60
66
 
61
67
  compiler.run((err, stats) => {
62
68
  const message = stats.toJson({
@@ -0,0 +1,56 @@
1
+ const crypto = require('crypto');
2
+
3
+ function shortId(file) {
4
+ return crypto
5
+ .createHash('sha1')
6
+ .update(file)
7
+ .digest('base64')
8
+ .replace(/[+/=]/g, '')
9
+ .slice(0, 8);
10
+ }
11
+
12
+ const TINY_LIST = `var ___CSS_LOADER_EXPORT___ = [];
13
+ ___CSS_LOADER_EXPORT___.toString = function () {
14
+ return this.map(function (item) {
15
+ return item[1];
16
+ }).join("");
17
+ };`;
18
+
19
+ // Rewrites css-loader output so style injection does not pull in
20
+ // css-loader/dist/runtime/api.js or noSourceMaps.js.
21
+ module.exports = function miniCssRuntimeLoader(source) {
22
+ if (!source.includes('___CSS_LOADER_API_IMPORT___')) return source;
23
+
24
+ if (/___CSS_LOADER_EXPORT___\.i\(/.test(source)) {
25
+ this.emitError(
26
+ new Error(
27
+ `CSS @import / composes is not supported in the production build: ${this.resourcePath}`,
28
+ ),
29
+ );
30
+ return source;
31
+ }
32
+
33
+ const moduleId = JSON.stringify(shortId(this.resourcePath || ''));
34
+ let code = source
35
+ .replace(/import ___CSS_LOADER_API_NO_SOURCEMAP_IMPORT___ from [^;]+;\n?/g, '')
36
+ .replace(/import ___CSS_LOADER_API_SOURCEMAP_IMPORT___ from [^;]+;\n?/g, '')
37
+ .replace(/import ___CSS_LOADER_API_IMPORT___ from [^;]+;\n?/g, '')
38
+ .replace(
39
+ /var ___CSS_LOADER_EXPORT___ = ___CSS_LOADER_API_IMPORT___\(___CSS_LOADER_API_(?:NO_)?SOURCEMAP_IMPORT___\);/,
40
+ TINY_LIST,
41
+ )
42
+ .replace(
43
+ /___CSS_LOADER_EXPORT___\.push\(\[module\.id,/g,
44
+ `___CSS_LOADER_EXPORT___.push([${moduleId},`,
45
+ );
46
+
47
+ if (code.includes('___CSS_LOADER_API_') || code.includes('[module.id,')) {
48
+ this.emitError(
49
+ new Error(
50
+ `mini-css-runtime-loader failed to strip css-loader runtime: ${this.resourcePath}`,
51
+ ),
52
+ );
53
+ }
54
+
55
+ return code;
56
+ };
@@ -0,0 +1,19 @@
1
+ function pitch(request) {
2
+ const contentRequest = JSON.stringify(
3
+ this.utils.contextify(this.context, `!!${request}`),
4
+ );
5
+
6
+ return `
7
+ import content from ${contentRequest};
8
+ var css = content && content.toString ? content.toString() : "";
9
+ if (typeof document !== "undefined" && css) {
10
+ var style = document.createElement("style");
11
+ style.textContent = css;
12
+ document.head.appendChild(style);
13
+ }
14
+ export default content && content.locals;
15
+ `;
16
+ }
17
+
18
+ module.exports = function miniStyleLoader() {};
19
+ module.exports.pitch = pitch;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@easyv/react-component-builder",
3
- "version": "0.24.0",
3
+ "version": "0.25.1",
4
4
  "files": [
5
5
  "launcher",
6
6
  "templates",
@@ -20,7 +20,7 @@
20
20
  "@babel/core": "^7.12.0",
21
21
  "@babel/plugin-proposal-private-property-in-object": "^7.21.11",
22
22
  "@babel/register": "^7.10.4",
23
- "@easyv/react-gui": "3.1.0",
23
+ "@easyv/react-gui": "3.3.0",
24
24
  "@easyv/simulator": "0.6.11",
25
25
  "@svgr/webpack": "8.1.0",
26
26
  "acorn": "^7.3.1",