xhs-mp-compiler-cli 1.2.0 → 1.3.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.
package/dist/compiler.js CHANGED
@@ -107,6 +107,10 @@ class AbstractCompiler extends stream_1.EventEmitter {
107
107
  }
108
108
  return;
109
109
  }
110
+ if (method === 'debugger') {
111
+ console.log(`[debugger]:`, ...(data.data || []));
112
+ return;
113
+ }
110
114
  this.emit(method, data);
111
115
  };
112
116
  this.call = (method, data) => new Promise((resolve, reject) => {
package/dist/index.d.ts CHANGED
@@ -36,6 +36,7 @@ type ICompilePkgsConfig = ICompileBaseConfig & {
36
36
  };
37
37
  type ICompileBuildConfig = ICompileBaseConfig & {
38
38
  entryType: string;
39
+ mpUploadOptions?: ICompileAndZipOptions | false;
39
40
  };
40
41
  export interface ICompileAndZipOptions {
41
42
  entryType: string;
package/dist/index.js CHANGED
@@ -228,7 +228,7 @@ class Compiler extends events_1.default {
228
228
  return __awaiter(this, void 0, void 0, function* () {
229
229
  var _a, _b, _c;
230
230
  try {
231
- const { entryType, compressJs, enableSourcemap, enableV2, enableV1, enableVDom } = config;
231
+ const { entryType, compressJs, enableSourcemap, enableV2, enableV1, enableVDom, mpUploadOptions } = config;
232
232
  this.emit('compile-project-start');
233
233
  (_a = this.logger) === null || _a === void 0 ? void 0 : _a.log('Compile project...');
234
234
  const compilerImpl = this.getComilerImpl(entryType);
@@ -237,7 +237,8 @@ class Compiler extends events_1.default {
237
237
  enableSourcemap,
238
238
  enableV2,
239
239
  enableV1,
240
- enableVDom
240
+ enableVDom,
241
+ mpUploadOptions,
241
242
  });
242
243
  this.emit('compile-project-done');
243
244
  (_b = this.logger) === null || _b === void 0 ? void 0 : _b.log('Compile project done');
@@ -283,7 +284,8 @@ class Compiler extends events_1.default {
283
284
  enableSourcemap,
284
285
  enableV2,
285
286
  enableV1,
286
- enableVDom
287
+ enableVDom,
288
+ mpUploadOptions: this.project.appMode === constant_config_1.MiniMode.miniprogram && opts
287
289
  };
288
290
  // 编译
289
291
  this.emit('compile-and-zip-status', { status: 'build-start' });
@@ -2,12 +2,14 @@ import { Compilation, RawSource, CopyPatterns, Config, ConcatSource, MPPackSetti
2
2
  import { Configuration, WebpackError, library, LoaderContext } from 'webpack';
3
3
  import type { Project } from 'xhs-mp-project';
4
4
  import { IStart } from '../types';
5
+ import { FeatureReporter } from 'xhs-mp-utils';
5
6
  export type ICompilation = any;
6
7
  export type ICompiler = any;
7
8
  export type ILoaderContext = LoaderContext<any> & {
8
9
  _compiler: {
9
10
  project: Project;
10
11
  packSetting: MPPackSetting;
12
+ reporter: FeatureReporter.IReporter;
11
13
  };
12
14
  };
13
15
  export { Compilation, RawSource, ConcatSource, CopyPatterns, Config, Configuration, WebpackError, library, LoaderContext };
@@ -7,12 +7,15 @@ exports.createCompiler = void 0;
7
7
  const presets_1 = require("../../presets");
8
8
  const path_1 = __importDefault(require("path"));
9
9
  const xhs_mp_pack_1 = require("xhs-mp-pack");
10
+ const apm_1 = require("../../utils/apm");
10
11
  const createCompiler = (startConfig) => {
11
12
  const { action, enableDevServer } = startConfig;
12
13
  const mppack = new xhs_mp_pack_1.MPPack(startConfig);
13
14
  const compilerOptions = {
14
15
  project: mppack.project,
15
- packSetting: mppack.packSetting
16
+ packSetting: mppack.packSetting,
17
+ // 注入apm模块
18
+ reporter: (0, apm_1.createReporter)(mppack.project, startConfig.mpUploadOptions)
16
19
  };
17
20
  const mpPackConfigs = (0, presets_1.createConfigs)(compilerOptions);
18
21
  mpPackConfigs.forEach(config => {
@@ -14,6 +14,7 @@ const presets_1 = require("../../presets");
14
14
  const xhs_mp_project_1 = require("xhs-mp-project");
15
15
  const xhs_mp_pack_1 = require("xhs-mp-pack");
16
16
  const webpack_1 = require("./webpack");
17
+ const apm_1 = require("../../utils/apm");
17
18
  // 防御编程
18
19
  const handleVersion = (startConfig) => {
19
20
  let { action, enableV1 = true, enableV2 = false, enableVDom = false } = startConfig;
@@ -32,7 +33,7 @@ const handleVersion = (startConfig) => {
32
33
  };
33
34
  };
34
35
  const formatCompilerConfig = startConfig => {
35
- const { projectPath, action, tsConfigPath, platform, extJsonPath, compressJs = false, enableSourcemap = false } = startConfig;
36
+ const { projectPath, action, tsConfigPath, platform, extJsonPath, compressJs = false, enableSourcemap = false, mpUploadOptions = false } = startConfig;
36
37
  const { enableV1, enableV2, enableVDom } = handleVersion(startConfig);
37
38
  const distDir = startConfig.distDir || path_1.default.join(process.cwd(), 'dist');
38
39
  process.env.__platform = platform;
@@ -61,7 +62,8 @@ const formatCompilerConfig = startConfig => {
61
62
  enableSourcemap,
62
63
  enableV1,
63
64
  enableV2,
64
- enableVDom
65
+ enableVDom,
66
+ mpUploadOptions,
65
67
  };
66
68
  return config;
67
69
  };
@@ -74,7 +76,9 @@ const createCompiler = (startConfig) => {
74
76
  const mppackSetting = new xhs_mp_pack_1.MPPackSetting(project, startConfig);
75
77
  const compilerOptions = {
76
78
  project: project,
77
- packSetting: mppackSetting
79
+ packSetting: mppackSetting,
80
+ // 注入apm模块
81
+ reporter: (0, apm_1.createReporter)(project, startConfig.mpUploadOptions)
78
82
  };
79
83
  const webpackConfig = (0, presets_1.createConfigs)(compilerOptions);
80
84
  webpackConfig.forEach(config => {
@@ -6,9 +6,10 @@ class InjectorPlugin {
6
6
  this.options = options;
7
7
  }
8
8
  apply(compiler) {
9
- const { project, packSetting } = this.options;
9
+ const { project, packSetting, reporter } = this.options;
10
10
  compiler.project = project;
11
11
  compiler.packSetting = packSetting;
12
+ compiler.reporter = reporter;
12
13
  }
13
14
  }
14
15
  exports.InjectorPlugin = InjectorPlugin;
@@ -1,5 +1,6 @@
1
1
  import type { MPPackSetting } from "xhs-mp-pack";
2
2
  import type { Project } from "xhs-mp-project";
3
+ import { FeatureReporter } from 'xhs-mp-utils';
3
4
  export declare enum ENV {
4
5
  development = 0,
5
6
  production = 1
@@ -84,8 +85,9 @@ export interface ICompilerConfig {
84
85
  enableV1?: boolean;
85
86
  enableV2?: boolean;
86
87
  enableVDom?: boolean;
88
+ mpUploadOptions?: any;
87
89
  }
88
- type CliPick = 'projectPath' | 'distDir' | 'watch' | 'tsConfigPath' | 'extJsonPath' | 'cacheDirectory' | 'compressJs' | 'enableSourcemap' | 'enableV1' | 'enableV2' | 'enableVDom';
90
+ type CliPick = 'projectPath' | 'distDir' | 'watch' | 'tsConfigPath' | 'extJsonPath' | 'cacheDirectory' | 'compressJs' | 'enableSourcemap' | 'enableV1' | 'enableV2' | 'enableVDom' | 'mpUploadOptions';
89
91
  export interface IStart extends Pick<ICompilerConfig, CliPick> {
90
92
  packMode?: 'webpack' | 'mp-pack';
91
93
  /**
@@ -114,5 +116,6 @@ export type IPresetOptions = {
114
116
  project: Project;
115
117
  packSetting: MPPackSetting;
116
118
  otherSetting?: any;
119
+ reporter?: FeatureReporter.IReporter;
117
120
  };
118
121
  export {};
@@ -0,0 +1,18 @@
1
+ import { APM, FeatureReporter } from 'xhs-mp-utils';
2
+ import { Project } from 'xhs-mp-project';
3
+ import type { ICompileAndZipOptions } from '../..';
4
+ /**
5
+ * 上报过程中,除去 WebComponent 因为标签种类有很多需要上报多次之外,其余的项目维度上均只上报一次
6
+ */
7
+ declare class Reporter implements FeatureReporter.IReporter {
8
+ private apm;
9
+ constructor(apm: APM);
10
+ reportThirdFramework(name: FeatureReporter.ThirdFramework): void;
11
+ reportMlFeature(feature: FeatureReporter.ML): void;
12
+ reportMlWebComponent(value: string): void;
13
+ reportJSONComponent(name: FeatureReporter.JSONComponent): void;
14
+ reportStyleIsolation(value: string): void;
15
+ reportJSONApp(name: FeatureReporter.JSONApp): void;
16
+ }
17
+ export declare function createReporter(project: Project, option: ICompileAndZipOptions): Reporter | undefined;
18
+ export {};
@@ -0,0 +1,181 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.createReporter = void 0;
10
+ const xhs_mp_utils_1 = require("xhs-mp-utils");
11
+ const pkg = require('../../../package.json');
12
+ function debug(...data) {
13
+ return;
14
+ // return process.send?.(JSON.stringify({ method: 'debugger', data }))
15
+ }
16
+ const cacheMap = {};
17
+ function once(target, key, descriptor) {
18
+ const originalMethod = descriptor.value;
19
+ descriptor.value = function (...args) {
20
+ try {
21
+ const argsStr = JSON.stringify(args);
22
+ if (cacheMap[key + argsStr])
23
+ return;
24
+ cacheMap[key + argsStr] = true;
25
+ debug(key, ...args);
26
+ return originalMethod.apply(this, args);
27
+ }
28
+ catch (error) {
29
+ console.error(`call ${key} error`, error.message);
30
+ }
31
+ };
32
+ return descriptor;
33
+ }
34
+ // const APMKEY = 'test_cli_apm'
35
+ const APMKEY = 'xhsmp_compiler_basic_ability';
36
+ /**
37
+ * 上报过程中,除去 WebComponent 因为标签种类有很多需要上报多次之外,其余的项目维度上均只上报一次
38
+ */
39
+ class Reporter {
40
+ constructor(apm) {
41
+ this.apm = apm;
42
+ }
43
+ reportThirdFramework(name) {
44
+ var _a, _b;
45
+ (_b = (_a = this.apm) === null || _a === void 0 ? void 0 : _a.report) === null || _b === void 0 ? void 0 : _b.call(_a, APMKEY, {
46
+ type: xhs_mp_utils_1.FeatureReporter.MODULE.thirdFramework,
47
+ name,
48
+ });
49
+ }
50
+ reportMlFeature(feature) {
51
+ var _a, _b;
52
+ (_b = (_a = this.apm) === null || _a === void 0 ? void 0 : _a.report) === null || _b === void 0 ? void 0 : _b.call(_a, APMKEY, {
53
+ type: xhs_mp_utils_1.FeatureReporter.MODULE.ml,
54
+ name: feature
55
+ });
56
+ }
57
+ reportMlWebComponent(value) {
58
+ var _a, _b;
59
+ (_b = (_a = this.apm) === null || _a === void 0 ? void 0 : _a.report) === null || _b === void 0 ? void 0 : _b.call(_a, APMKEY, {
60
+ type: xhs_mp_utils_1.FeatureReporter.MODULE.ml,
61
+ name: xhs_mp_utils_1.FeatureReporter.ML.WebComponent,
62
+ value,
63
+ });
64
+ }
65
+ // 在reporter 创建阶段即可根据传入project信息进行上报
66
+ reportJSONComponent(name) {
67
+ var _a, _b;
68
+ (_b = (_a = this.apm) === null || _a === void 0 ? void 0 : _a.report) === null || _b === void 0 ? void 0 : _b.call(_a, APMKEY, {
69
+ type: xhs_mp_utils_1.FeatureReporter.MODULE.componentJSON,
70
+ name,
71
+ });
72
+ }
73
+ reportStyleIsolation(value) {
74
+ var _a, _b;
75
+ (_b = (_a = this.apm) === null || _a === void 0 ? void 0 : _a.report) === null || _b === void 0 ? void 0 : _b.call(_a, APMKEY, {
76
+ type: xhs_mp_utils_1.FeatureReporter.MODULE.componentJSON,
77
+ name: xhs_mp_utils_1.FeatureReporter.JSONComponent.styleIsolation,
78
+ value,
79
+ });
80
+ }
81
+ reportJSONApp(name) {
82
+ var _a, _b;
83
+ (_b = (_a = this.apm) === null || _a === void 0 ? void 0 : _a.report) === null || _b === void 0 ? void 0 : _b.call(_a, APMKEY, {
84
+ type: xhs_mp_utils_1.FeatureReporter.MODULE.appJSON,
85
+ name,
86
+ });
87
+ }
88
+ }
89
+ __decorate([
90
+ once
91
+ ], Reporter.prototype, "reportThirdFramework", null);
92
+ __decorate([
93
+ once
94
+ ], Reporter.prototype, "reportMlFeature", null);
95
+ __decorate([
96
+ once
97
+ ], Reporter.prototype, "reportMlWebComponent", null);
98
+ __decorate([
99
+ once
100
+ ], Reporter.prototype, "reportJSONComponent", null);
101
+ __decorate([
102
+ once
103
+ ], Reporter.prototype, "reportStyleIsolation", null);
104
+ __decorate([
105
+ once
106
+ ], Reporter.prototype, "reportJSONApp", null);
107
+ function isObject(t) {
108
+ return t != null && typeof t === 'object';
109
+ }
110
+ function isEmpty(t) {
111
+ if (Array.isArray(t)) {
112
+ return t.length === 0;
113
+ }
114
+ if (isObject(t)) {
115
+ return Object.keys(t).length === 0;
116
+ }
117
+ return true;
118
+ }
119
+ function createReporter(project, option) {
120
+ var _a, _b, _c;
121
+ try {
122
+ const app_id = option === null || option === void 0 ? void 0 : option.upload_app_id;
123
+ if (!app_id) {
124
+ return;
125
+ }
126
+ const apm = new xhs_mp_utils_1.APM(`mp-compiler-${pkg.version}`);
127
+ (_a = apm === null || apm === void 0 ? void 0 : apm.setContext) === null || _a === void 0 ? void 0 : _a.call(apm, app_id, {
128
+ app_id,
129
+ app_name: app_id,
130
+ third_name: app_id,
131
+ page_key: '0',
132
+ app_env: 0,
133
+ app_type: 0,
134
+ bundle_version: ((_b = option === null || option === void 0 ? void 0 : option.pkgInfo) === null || _b === void 0 ? void 0 : _b.version) || '',
135
+ base_version: ((_c = project.projectJsonContent) === null || _c === void 0 ? void 0 : _c.libVersion) || ''
136
+ });
137
+ const reporter = new Reporter(apm);
138
+ function reportJSON() {
139
+ var _a, _b, _c, _d;
140
+ // 分包
141
+ if ((_a = project.getSubPackages()) === null || _a === void 0 ? void 0 : _a.length) {
142
+ reporter.reportJSONApp(xhs_mp_utils_1.FeatureReporter.JSONApp.subPackages);
143
+ }
144
+ // tabbar
145
+ if (!isEmpty((_b = project.appJSON) === null || _b === void 0 ? void 0 : _b.tabBar)) {
146
+ reporter.reportJSONApp(xhs_mp_utils_1.FeatureReporter.JSONApp.tabBar);
147
+ }
148
+ // 按需注入
149
+ if ((_c = project.appJSON) === null || _c === void 0 ? void 0 : _c.lazyCodeLoading) {
150
+ reporter.reportJSONApp(xhs_mp_utils_1.FeatureReporter.JSONApp.lazyCodeLoading);
151
+ }
152
+ // 预载
153
+ if (!isEmpty((_d = project.appJSON) === null || _d === void 0 ? void 0 : _d.preloadRule)) {
154
+ reporter.reportJSONApp(xhs_mp_utils_1.FeatureReporter.JSONApp.preloadRule);
155
+ }
156
+ const componentsMap = project.getComponentsMap();
157
+ if (!isEmpty(componentsMap)) {
158
+ // debug(componentsMap)
159
+ for (let key in componentsMap) {
160
+ const style = componentsMap[key].styleIsolation;
161
+ // 样式隔离类型
162
+ if (style) {
163
+ reporter.reportStyleIsolation(style);
164
+ }
165
+ // 占位组件
166
+ const componentPlaceholder = componentsMap[key].componentPlaceholder;
167
+ if (!isEmpty(componentPlaceholder)) {
168
+ reporter.reportJSONComponent(xhs_mp_utils_1.FeatureReporter.JSONComponent.componentPlaceholder);
169
+ }
170
+ }
171
+ }
172
+ }
173
+ // 延时上报
174
+ setTimeout(reportJSON);
175
+ return reporter;
176
+ }
177
+ catch (error) {
178
+ debug('[create reporter error]');
179
+ }
180
+ }
181
+ exports.createReporter = createReporter;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xhs-mp-compiler-cli",
3
- "version": "1.2.0",
3
+ "version": "1.3.1",
4
4
  "description": "xhs mp command tool.",
5
5
  "preferGlobal": true,
6
6
  "category": "esm",
@@ -82,11 +82,11 @@
82
82
  "webpack-bundle-analyzer": "^4.4.0",
83
83
  "webpack-chain": "^6.5.1",
84
84
  "webpack-sources": "^3.2.2",
85
- "xhs-mp-compiler-ml-loader": "1.2.0",
86
- "xhs-mp-compiler-utils": "1.2.0",
87
- "xhs-mp-pack": "^1.2.0",
88
- "xhs-mp-project": "^1.2.0",
89
- "xhs-mp-utils": "^1.2.0"
85
+ "xhs-mp-compiler-ml-loader": "1.3.1",
86
+ "xhs-mp-compiler-utils": "1.2.1",
87
+ "xhs-mp-pack": "^1.2.1",
88
+ "xhs-mp-project": "^1.2.1",
89
+ "xhs-mp-utils": "^1.3.1"
90
90
  },
91
91
  "devDependencies": {
92
92
  "@types/babel__generator": "7.6.3",