neo-cmp-cli 1.1.12 → 1.1.13

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": "neo-cmp-cli",
3
- "version": "1.1.12",
3
+ "version": "1.1.13",
4
4
  "description": "前端脚手架:自定义组件开发工具,支持react 和 vue2.0技术栈。",
5
5
  "keywords": [
6
6
  "neo-cli",
@@ -8,11 +8,16 @@ const { isPlainObject } = require('lodash');
8
8
  * @param {*} cmpName 自定义组件名称
9
9
  * @returns 组件预览代码
10
10
  */
11
- const createCommonModulesCode = (neoExports) => {
12
- if (!neoExports) {
11
+ const createCommonModulesCode = (neoCommonModule, cmpTypes) => {
12
+ const {neoExports, remotes} = neoCommonModule;
13
+
14
+ if (!neoExports && !remotes) {
13
15
  return '';
14
16
  }
17
+ // 记录当前自定义组件共享出去的模块
15
18
  const CustomCmpCommonModules = {};
19
+ // 记录当前自定义组件需要的远程组件
20
+ const CustomCmpRemotes = {};
16
21
 
17
22
  // 根据 neoExports 获取共享的依赖模块
18
23
  if (Array.isArray(neoExports) && neoExports.length > 0) {
@@ -30,6 +35,13 @@ const createCommonModulesCode = (neoExports) => {
30
35
  process.exit(1);
31
36
  }
32
37
 
38
+ // 根据 cmpTypes 和 remotes 设置远程组件信息
39
+ if (Array.isArray(cmpTypes) && cmpTypes.length > 0) {
40
+ cmpTypes.forEach((cmpType) => {
41
+ CustomCmpRemotes[cmpType] = remotes;
42
+ });
43
+ }
44
+
33
45
  // 构建 CustomCmpCommonModules 对象的代码字符串
34
46
  let customCmpCommonModulesCode = '{\n';
35
47
  const moduleEntries = Object.entries(CustomCmpCommonModules);
@@ -44,16 +56,33 @@ const createCommonModulesCode = (neoExports) => {
44
56
  * 自定义组件 共享出来的依赖模块
45
57
  * 备注:可在其他模块中通过 neoRequire 中使用
46
58
  */
59
+ import { isPlainObject } from 'lodash';
47
60
  const CustomCmpCommonModules = ${customCmpCommonModulesCode};
61
+ const CustomCmpRemotes = ${JSON.stringify(CustomCmpRemotes)};
48
62
 
49
63
  // 用于添加共享的依赖模块
50
64
  const addNeoCommonModules = (modules) => {
51
65
  if (!window.__NeoCommonModules) {
52
66
  window.__NeoCommonModules = {};
53
67
  }
54
- window.__NeoCommonModules = Object.assign(window.__NeoCommonModules, modules);
68
+ if (isPlainObject(modules)) {
69
+ window.__NeoCommonModules = Object.assign(window.__NeoCommonModules, modules)
70
+ }
55
71
  };
56
72
 
73
+ // 用于添加自定义组件的远程组件(关联使用)
74
+ const addNeoRemotes = (remotes) => {
75
+ if (!window.__NeoCommonModules) {
76
+ window.__NeoCommonModules = {}
77
+ }
78
+ if (!window.__NeoCommonModules.remotes) {
79
+ window.__NeoCommonModules.remotes = {}
80
+ }
81
+ if (isPlainObject(remotes)) {
82
+ window.__NeoCommonModules.remotes = Object.assign(window.__NeoCommonModules.remotes, remotes)
83
+ }
84
+ }
85
+
57
86
  addNeoCommonModules(CustomCmpCommonModules);
58
87
  `;
59
88
 
@@ -5,15 +5,15 @@ const { resolveToCurrentRoot } = require('../utils/pathUtils');
5
5
 
6
6
  /**
7
7
  * 从指定目录获取组件入口文件
8
- * @param {*} buildType 构建类型
9
8
  * @param {*} defaultComponentsDir 默认组件目录
10
9
  * @returns 组件入口文件
11
10
  */
12
- module.exports = (buildType = 'build2lib', defaultComponentsDir = './src/components') => {
11
+ module.exports = (defaultComponentsDir = './src/components') => {
13
12
  const widgetEntries = {};
14
13
  const linkDebugEntries = {
15
14
  index: []
16
15
  };
16
+ const cmpTypes = []; // 用于记录组件名称
17
17
  const componentsBaseDir = resolveToCurrentRoot(defaultComponentsDir);
18
18
  if (!fs.existsSync(componentsBaseDir)) {
19
19
  console.error(`未找到组件目录,请检查 ${defaultComponentsDir} 目录是否存在`);
@@ -37,11 +37,13 @@ module.exports = (buildType = 'build2lib', defaultComponentsDir = './src/compone
37
37
  const curCmpName = dir; // _.camelCase(dir);
38
38
  const cmpNameKey = _.camelCase(curCmpName);
39
39
 
40
+ // 记录组件类型
41
+ cmpTypes.push(curCmpName);
42
+
40
43
  fs.readdirSync(curCmpPath)
41
44
  .filter((file) => file.match(/[register|model]\.[tj]sx?$/))
42
45
  .map((file) => path.join(defaultComponentsDir, curCmpName, file))
43
46
  .forEach((filePath) => {
44
- console.log(filePath);
45
47
  const curPath = `./${filePath}`;
46
48
  if (filePath.match(/register\.[tj]sx?$/)) {
47
49
  widgetEntries[cmpNameKey] = curPath;
@@ -52,10 +54,6 @@ module.exports = (buildType = 'build2lib', defaultComponentsDir = './src/compone
52
54
  });
53
55
  }
54
56
  });
55
-
56
- if (buildType === 'linkDebug') {
57
- return linkDebugEntries;
58
- }
59
57
  } catch (error) {
60
58
  console.error('获取自定义组件入口文件失败(getEntries):', error);
61
59
 
@@ -63,5 +61,9 @@ module.exports = (buildType = 'build2lib', defaultComponentsDir = './src/compone
63
61
  process.exit(1);
64
62
  }
65
63
 
66
- return widgetEntries;
64
+ return {
65
+ widgetEntries,
66
+ linkDebugEntries,
67
+ cmpTypes,
68
+ };
67
69
  };
@@ -6,15 +6,15 @@ const getCmpRegister = require('./getCmpRegister');
6
6
  const getCmpModelRegister = require('./getCmpModelRegister');
7
7
  /**
8
8
  * 从指定目录获取组件入口文件,并自动创建对应的注册文件
9
- * @param {*} buildType 构建类型
10
9
  * @param {*} defaultComponentsDir 默认组件目录
11
10
  * @returns 组件入口文件
12
11
  */
13
- module.exports = (buildType = 'build2lib', defaultComponentsDir = './src/components') => {
12
+ module.exports = (defaultComponentsDir = './src/components') => {
14
13
  const widgetEntries = {};
15
14
  const linkDebugEntries = {
16
15
  index: []
17
16
  };
17
+ const cmpTypes = []; // 用于记录组件名称
18
18
  const componentsBaseDir = resolveToCurrentRoot(defaultComponentsDir);
19
19
  if (!fs.existsSync(componentsBaseDir)) {
20
20
  console.error(`未找到组件目录,请检查 ${defaultComponentsDir} 目录是否存在`);
@@ -47,6 +47,9 @@ module.exports = (buildType = 'build2lib', defaultComponentsDir = './src/compone
47
47
  const curCmpName = dir; // _.camelCase(dir);
48
48
  const cmpNameKey = _.camelCase(curCmpName);
49
49
 
50
+ // 记录组件类型
51
+ cmpTypes.push(curCmpName);
52
+
50
53
  // 当前自定义组件临时目录
51
54
  const cmpTempDir = `${cliTempDir}/${curCmpName}`;
52
55
  if (!fs.existsSync(cmpTempDir)) {
@@ -81,10 +84,6 @@ module.exports = (buildType = 'build2lib', defaultComponentsDir = './src/compone
81
84
  });
82
85
  }
83
86
  });
84
-
85
- if (buildType === 'linkDebug') {
86
- return linkDebugEntries;
87
- }
88
87
  } catch (error) {
89
88
  console.error('获取自定义组件入口文件失败(getEntriesWithAutoRegister):', error);
90
89
 
@@ -92,5 +91,9 @@ module.exports = (buildType = 'build2lib', defaultComponentsDir = './src/compone
92
91
  process.exit(1);
93
92
  }
94
93
 
95
- return widgetEntries;
94
+ return {
95
+ widgetEntries,
96
+ linkDebugEntries,
97
+ cmpTypes,
98
+ };
96
99
  };
@@ -21,8 +21,6 @@ const getValue = (originValue, defaultValue) => {
21
21
  };
22
22
 
23
23
  const neoCommonModule = curConfig.neoCommonModule || {};
24
- // 自定义组件 共享出来的依赖模块
25
- const cmpNeoExports = neoCommonModule.neoExports || [];
26
24
  // 自定义组件 需要剔除的依赖模块
27
25
  const cmpNeoExternals = neoCommonModule.neoExternals || [];
28
26
 
@@ -72,16 +70,21 @@ module.exports = {
72
70
  curConfig.webpack.ignoreNodeModules = false;
73
71
 
74
72
  const curEntry = curConfig.dev.entry;
73
+ let curCmpTypes = [];
75
74
 
76
75
  if (!curEntry || Object.keys(curEntry).length === 0) {
77
76
  // 如果未配置 entry,则自动生成 entry
78
77
  let entries = {};
79
78
  if (curConfig.dev.disableAutoRegister) {
80
79
  // disableAutoRegister 为 true 时,仅自动生成入口文件(不自动注册)
81
- entries = getEntries('linkDebug', curConfig.componentsDir);
80
+ const { linkDebugEntries, cmpTypes } = getEntries(curConfig.componentsDir);
81
+ entries = linkDebugEntries;
82
+ curCmpTypes = cmpTypes;
82
83
  } else {
83
84
  // 自动生成入口文件(并自动创建对应的注册文件)
84
- entries = getEntriesWithAutoRegister('linkDebug', curConfig.componentsDir);
85
+ const { linkDebugEntries, cmpTypes } = getEntriesWithAutoRegister(curConfig.componentsDir);
86
+ entries = linkDebugEntries;
87
+ curCmpTypes = cmpTypes;
85
88
  }
86
89
 
87
90
  // 注入 webpack/entry
@@ -125,21 +128,14 @@ module.exports = {
125
128
  curConfig.dev.externals = neoExternals;
126
129
  }
127
130
 
128
- if (
129
- cmpNeoExports &&
130
- ((Array.isArray(cmpNeoExports) && cmpNeoExports.length > 0) ||
131
- (_.isPlainObject(cmpNeoExports) && Object.keys(cmpNeoExports).length > 0))
132
- ) {
133
- const commonModulesFilePath = createCommonModulesCode(cmpNeoExports);
131
+ // 写入自定义组件的共享模块和远程模块相关信息
132
+ const commonModulesFilePath = createCommonModulesCode(neoCommonModule, curCmpTypes);
134
133
 
135
- // 所有入口文件添加 commonModulesFile
136
- if (curConfig.dev.entry) {
137
- Object.keys(curConfig.dev.entry).forEach((name) => {
138
- curConfig.dev.entry[name] = [commonModulesFilePath].concat(
139
- curConfig.dev.entry[name]
140
- );
141
- });
142
- }
134
+ // 所有入口文件添加 commonModulesFile
135
+ if (commonModulesFilePath && curConfig.dev.entry) {
136
+ Object.keys(curConfig.dev.entry).forEach((name) => {
137
+ curConfig.dev.entry[name] = [commonModulesFilePath].concat(curConfig.dev.entry[name]);
138
+ });
143
139
  }
144
140
 
145
141
  akfun.dev(curConfig, consoleTag);
@@ -147,16 +143,21 @@ module.exports = {
147
143
  build: () => akfun.build('build', curConfig, consoleTag), // 构建脚本:生产环境
148
144
  build2lib: () => {
149
145
  const curEntry = curConfig.build2lib.entry;
146
+ let curCmpTypes = [];
150
147
 
151
148
  if (!curEntry || Object.keys(curEntry).length === 0) {
152
149
  // 如果未配置 entry,则自动生成 entry
153
150
  let entries = {};
154
151
  if (curConfig.build2lib.disableAutoRegister) {
155
152
  // disableAutoRegister 为 true 时,仅自动生成入口文件(不自动注册)
156
- entries = getEntries('build2lib', curConfig.componentsDir);
153
+ const { widgetEntries, cmpTypes } = getEntries(curConfig.componentsDir);
154
+ entries = widgetEntries;
155
+ curCmpTypes = cmpTypes;
157
156
  } else {
158
157
  // 自动生成入口文件(并自动创建对应的注册文件)
159
- entries = getEntriesWithAutoRegister('build2lib', curConfig.componentsDir);
158
+ const { widgetEntries, cmpTypes } = getEntriesWithAutoRegister(curConfig.componentsDir);
159
+ entries = widgetEntries;
160
+ curCmpTypes = cmpTypes;
160
161
  }
161
162
 
162
163
  // 注入 webpack/entry
@@ -189,16 +190,21 @@ module.exports = {
189
190
  curConfig.build2lib = Object.assign(curConfig.build2lib, publish2ossConfig);
190
191
 
191
192
  const curEntry = curConfig.build2lib.entry;
193
+ let curCmpTypes = [];
192
194
 
193
195
  if (!curEntry || Object.keys(curEntry).length === 0) {
194
196
  // 如果未配置 entry,则自动生成 entry
195
197
  let entries = {};
196
198
  if (curConfig.build2lib.disableAutoRegister) {
197
199
  // disableAutoRegister 为 true 时,仅自动生成入口文件(不自动注册)
198
- entries = getEntries('build2lib', curConfig.componentsDir);
200
+ const { widgetEntries, cmpTypes } = getEntries(curConfig.componentsDir);
201
+ entries = widgetEntries;
202
+ curCmpTypes = cmpTypes;
199
203
  } else {
200
204
  // 自动生成入口文件(并自动创建对应的注册文件)
201
- entries = getEntriesWithAutoRegister('build2lib', curConfig.componentsDir);
205
+ const { widgetEntries, cmpTypes } = getEntriesWithAutoRegister(curConfig.componentsDir);
206
+ entries = widgetEntries;
207
+ curCmpTypes = cmpTypes;
202
208
  }
203
209
 
204
210
  // 注入 webpack/entry
@@ -242,21 +248,16 @@ module.exports = {
242
248
  curConfig.build2lib.externals = neoExternals;
243
249
  }
244
250
 
245
- if (
246
- cmpNeoExports &&
247
- ((Array.isArray(cmpNeoExports) && cmpNeoExports.length > 0) ||
248
- (_.isPlainObject(cmpNeoExports) && Object.keys(cmpNeoExports).length > 0))
249
- ) {
250
- const commonModulesFilePath = createCommonModulesCode(cmpNeoExports);
251
+ // 写入自定义组件的共享模块和远程模块相关信息
252
+ const commonModulesFilePath = createCommonModulesCode(neoCommonModule, curCmpTypes);
251
253
 
252
- // 所有入口文件添加 commonModulesFile
253
- if (curConfig.build2lib.entry) {
254
- Object.keys(curConfig.build2lib.entry).forEach((name) => {
255
- curConfig.build2lib.entry[name] = [commonModulesFilePath].concat(
256
- curConfig.build2lib.entry[name]
257
- );
258
- });
259
- }
254
+ // 所有入口文件添加 commonModulesFile
255
+ if (commonModulesFilePath && curConfig.build2lib.entry) {
256
+ Object.keys(curConfig.build2lib.entry).forEach((name) => {
257
+ curConfig.build2lib.entry[name] = [commonModulesFilePath].concat(
258
+ curConfig.build2lib.entry[name]
259
+ );
260
+ });
260
261
  }
261
262
 
262
263
  akfun.build('lib', curConfig, consoleTag, () => {
@@ -37,18 +37,28 @@ const getExternalsByNeoCommonModules = (cmpNeoExternals) => {
37
37
  return neoExternals;
38
38
  };
39
39
 
40
- // 数组方式的 externals 失效,需要使用对象方式的 externals
41
- const getExternalsByNeoCommonModulesV2 = () => {
42
- return Object.keys(NeoCommonModules).map(moduleName => `commonjs ${moduleName}`);
43
- };
44
-
45
40
  // 用于添加共享的依赖模块
46
41
  const addNeoCommonModules = (modules) => {
47
42
  if (!window.__NeoCommonModules) {
48
- window.__NeoCommonModules = {};
43
+ window.__NeoCommonModules = {}
49
44
  }
50
- window.__NeoCommonModules = Object.assign(window.__NeoCommonModules, modules);
51
- };
45
+ if (_.isPlainObject(modules)) {
46
+ window.__NeoCommonModules = Object.assign(window.__NeoCommonModules, modules);
47
+ }
48
+ }
49
+
50
+ // 用于添加自定义组件的远程组件
51
+ const addNeoRemotes = (remotes) => {
52
+ if (!window.__NeoCommonModules) {
53
+ window.__NeoCommonModules = {}
54
+ }
55
+ if (!window.__NeoCommonModules.remotes) {
56
+ window.__NeoCommonModules.remotes = {}
57
+ }
58
+ if (_.isPlainObject(remotes)) {
59
+ window.__NeoCommonModules.remotes = Object.assign(window.__NeoCommonModules.remotes, remotes);
60
+ }
61
+ }
52
62
 
53
63
  const initNeoRequire = () => {
54
64
  if (!window.neoRequire) {
@@ -62,5 +72,6 @@ const initNeoRequire = () => {
62
72
  module.exports = {
63
73
  initNeoRequire,
64
74
  addNeoCommonModules,
75
+ addNeoRemotes,
65
76
  getExternalsByNeoCommonModules
66
77
  };
@@ -43,6 +43,7 @@ module.exports = {
43
43
  // neoExports: ['echarts'], // 自定义组件 共享出来的模块,支持数组和对象形式
44
44
  // neoExports: {}, // 对象写法
45
45
  // neoExternals: ['neo-register', 'chart-widget'], // 自定义组件 需要剔除的模块,仅支持数组写法
46
+ remotes: ['chart-widget'], // 远程组件,表示当前自定义组件会用到远程组件的导出模块,需要和 neoExternals 配合使用
46
47
  },
47
48
  preview: {
48
49
  // 用于开启本地预览模式的相关配置信息
@@ -38,7 +38,7 @@ module.exports = {
38
38
  // plugins: [],
39
39
  // babelPlugins: [],
40
40
  },
41
- // Neo 共享依赖模块
41
+ // 用于添加 Neo 共享依赖模块的配置信息
42
42
  /*
43
43
  neoCommonModule: {
44
44
  // neoExports: [], // 自定义组件 共享出来的模块,支持数组和对象形式