xhs-mp-compiler-cli 1.0.2 → 1.1.0-beta.0

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.
Files changed (150) hide show
  1. package/dist/bin/xhs-mp-cli-build.js +3 -3
  2. package/dist/bin/xhs-mp-cli-dev.js +9 -31
  3. package/dist/compiler.d.ts +76 -0
  4. package/dist/compiler.js +361 -0
  5. package/dist/compilerCP.js +175 -0
  6. package/dist/config/constant.config.d.ts +7 -0
  7. package/dist/config/constant.config.js +10 -0
  8. package/dist/dev-server/index.d.ts +2 -2
  9. package/dist/dev-server/index.js +7 -5
  10. package/dist/index.d.ts +1 -11
  11. package/dist/index.js +3 -112
  12. package/dist/packs/index.d.ts +14 -0
  13. package/dist/packs/index.js +31 -0
  14. package/dist/packs/mp-pack/index.d.ts +3 -0
  15. package/dist/packs/mp-pack/index.js +32 -0
  16. package/dist/packs/webpack/index.d.ts +3 -0
  17. package/dist/packs/webpack/index.js +92 -0
  18. package/dist/packs/webpack/webpack.d.ts +21 -0
  19. package/dist/packs/webpack/webpack.js +79 -0
  20. package/dist/{webpack-config/presets → presets}/babel.d.ts +3 -3
  21. package/dist/{webpack-config/presets → presets}/babel.js +13 -11
  22. package/dist/presets/common.d.ts +4 -0
  23. package/dist/presets/common.js +46 -0
  24. package/dist/presets/configs/minigame/assets/assetsEntryPlugin.d.ts +10 -0
  25. package/dist/presets/configs/minigame/assets/assetsEntryPlugin.js +46 -0
  26. package/dist/presets/configs/minigame/assets/index.d.ts +3 -0
  27. package/dist/presets/configs/minigame/assets/index.js +65 -0
  28. package/dist/presets/configs/minigame/service/index.d.ts +3 -0
  29. package/dist/presets/configs/minigame/service/index.js +34 -0
  30. package/dist/{webpack-config/presets/service/XhsResolveLimitPlugin.d.ts → presets/configs/minigame/service/resolveLimitPlugin.d.ts} +3 -3
  31. package/dist/presets/configs/minigame/service/resolveLimitPlugin.js +35 -0
  32. package/dist/presets/configs/minigame/service/serviceChunkPlugin.d.ts +9 -0
  33. package/dist/presets/configs/minigame/service/serviceChunkPlugin.js +69 -0
  34. package/dist/{webpack-config/presets/service/ServiceEntryPlugin.d.ts → presets/configs/minigame/service/serviceEntryPlugin.d.ts} +3 -1
  35. package/dist/presets/configs/minigame/service/serviceEntryPlugin.js +32 -0
  36. package/dist/presets/configs/miniprogram/assets/assetsEntryPlugin.d.ts +12 -0
  37. package/dist/presets/configs/miniprogram/assets/assetsEntryPlugin.js +132 -0
  38. package/dist/presets/configs/miniprogram/assets/index.d.ts +4 -0
  39. package/dist/presets/configs/miniprogram/assets/index.js +47 -0
  40. package/dist/presets/configs/miniprogram/render/component.d.ts +3 -0
  41. package/dist/presets/configs/miniprogram/render/component.js +47 -0
  42. package/dist/presets/configs/miniprogram/render/index.d.ts +5 -0
  43. package/dist/presets/configs/miniprogram/render/index.js +70 -0
  44. package/dist/presets/configs/miniprogram/render/render.d.ts +3 -0
  45. package/dist/presets/configs/miniprogram/render/render.js +49 -0
  46. package/dist/presets/configs/miniprogram/render/renderChunkPlugin.d.ts +8 -0
  47. package/dist/{webpack-config/presets/render/RenderChunkPlugin.js → presets/configs/miniprogram/render/renderChunkPlugin.js} +13 -8
  48. package/dist/presets/configs/miniprogram/render/sjsEntryPlugin.d.ts +15 -0
  49. package/dist/{webpack-config/presets/render/SjsEntryPlugin.js → presets/configs/miniprogram/render/sjsEntryPlugin.js} +24 -21
  50. package/dist/presets/configs/miniprogram/service/index.d.ts +5 -0
  51. package/dist/presets/configs/miniprogram/service/index.js +72 -0
  52. package/dist/presets/configs/miniprogram/service/resolveLimitPlugin.d.ts +8 -0
  53. package/dist/{webpack-config/presets/service/XhsResolveLimitPlugin.js → presets/configs/miniprogram/service/resolveLimitPlugin.js} +11 -10
  54. package/dist/presets/configs/miniprogram/service/serviceChunkPlugin.d.ts +9 -0
  55. package/dist/presets/configs/miniprogram/service/serviceChunkPlugin.js +65 -0
  56. package/dist/presets/index.d.ts +2 -0
  57. package/dist/presets/index.js +95 -0
  58. package/dist/presets/loaders/mg-entry-loader.js +27 -0
  59. package/dist/{webpack-config → presets}/loaders/mini-style-loader.js +13 -10
  60. package/dist/presets/loaders/mp-entry-loader.js +196 -0
  61. package/dist/{webpack-config → presets}/loaders/sjs-loader/index.js +0 -4
  62. package/dist/{webpack-config → presets}/loaders/wxss-loader.js +7 -6
  63. package/dist/presets/plugins/InjectorPlugin.d.ts +7 -0
  64. package/dist/presets/plugins/InjectorPlugin.js +14 -0
  65. package/dist/presets/prod.d.ts +4 -0
  66. package/dist/presets/prod.js +80 -0
  67. package/dist/types/index.d.ts +18 -6
  68. package/dist/utils/common.d.ts +2 -1
  69. package/dist/utils/common.js +7 -6
  70. package/dist/utils/const.d.ts +27 -0
  71. package/dist/utils/const.js +47 -0
  72. package/dist/{webpack-config/lib → utils}/css.js +6 -6
  73. package/dist/utils/getSuffixName.d.ts +3 -0
  74. package/dist/utils/getSuffixName.js +18 -0
  75. package/dist/{webpack-config/loaders/utils.d.ts → utils/loader-utils.d.ts} +2 -0
  76. package/dist/{webpack-config/loaders/utils.js → utils/loader-utils.js} +19 -1
  77. package/dist/utils/projectConfig.d.ts +1 -1
  78. package/dist/utils/projectConfig.js +2 -2
  79. package/dist/{webpack-config/lib → utils}/utils.js +2 -2
  80. package/package.json +10 -9
  81. package/dist/build/index.d.ts +0 -3
  82. package/dist/build/index.js +0 -22
  83. package/dist/globalData/SjsModule.d.ts +0 -32
  84. package/dist/globalData/SjsModule.js +0 -34
  85. package/dist/utils/combineExtJson.d.ts +0 -3
  86. package/dist/utils/combineExtJson.js +0 -74
  87. package/dist/utils/getCssSuffixName.d.ts +0 -1
  88. package/dist/utils/getCssSuffixName.js +0 -9
  89. package/dist/utils/getMlSuffixName.d.ts +0 -1
  90. package/dist/utils/getMlSuffixName.js +0 -6
  91. package/dist/utils/getPrefixName.d.ts +0 -1
  92. package/dist/utils/getPrefixName.js +0 -6
  93. package/dist/utils/transformAppJson.d.ts +0 -16
  94. package/dist/utils/transformAppJson.js +0 -199
  95. package/dist/utils/validateAppJson.d.ts +0 -4
  96. package/dist/utils/validateAppJson.js +0 -111
  97. package/dist/webpack-config/index.d.ts +0 -4
  98. package/dist/webpack-config/index.js +0 -41
  99. package/dist/webpack-config/lib/const.d.ts +0 -13
  100. package/dist/webpack-config/lib/const.js +0 -15
  101. package/dist/webpack-config/loaders/component-entry-loader.js +0 -31
  102. package/dist/webpack-config/loaders/component-entry-requirements-loader.js +0 -24
  103. package/dist/webpack-config/loaders/render-entry-loader.js +0 -50
  104. package/dist/webpack-config/loaders/service-entry-loader.d.ts +0 -1
  105. package/dist/webpack-config/loaders/service-entry-loader.js +0 -51
  106. package/dist/webpack-config/plugins/VueLoaderPlugin.d.ts +0 -19
  107. package/dist/webpack-config/plugins/VueLoaderPlugin.js +0 -205
  108. package/dist/webpack-config/plugins/XhsMpErrorTransformPlugin.d.ts +0 -8
  109. package/dist/webpack-config/plugins/XhsMpErrorTransformPlugin.js +0 -40
  110. package/dist/webpack-config/plugins/XhsMpJsonPlugin.d.ts +0 -36
  111. package/dist/webpack-config/plugins/XhsMpJsonPlugin.js +0 -71
  112. package/dist/webpack-config/presets/common.d.ts +0 -4
  113. package/dist/webpack-config/presets/common.js +0 -40
  114. package/dist/webpack-config/presets/mp/MpEntryPlugin.d.ts +0 -23
  115. package/dist/webpack-config/presets/mp/MpEntryPlugin.js +0 -87
  116. package/dist/webpack-config/presets/mp/index.d.ts +0 -4
  117. package/dist/webpack-config/presets/mp/index.js +0 -48
  118. package/dist/webpack-config/presets/prod.d.ts +0 -4
  119. package/dist/webpack-config/presets/prod.js +0 -83
  120. package/dist/webpack-config/presets/render/ComponentEntryPlugin.d.ts +0 -18
  121. package/dist/webpack-config/presets/render/ComponentEntryPlugin.js +0 -77
  122. package/dist/webpack-config/presets/render/RenderChunkPlugin.d.ts +0 -5
  123. package/dist/webpack-config/presets/render/RenderEntryPlugin.d.ts +0 -14
  124. package/dist/webpack-config/presets/render/RenderEntryPlugin.js +0 -42
  125. package/dist/webpack-config/presets/render/SjsEntryPlugin.d.ts +0 -31
  126. package/dist/webpack-config/presets/render/index.d.ts +0 -4
  127. package/dist/webpack-config/presets/render/index.js +0 -62
  128. package/dist/webpack-config/presets/service/ServiceEntryPlugin.js +0 -36
  129. package/dist/webpack-config/presets/service/XhsMpServiceChunkPlugin.d.ts +0 -6
  130. package/dist/webpack-config/presets/service/XhsMpServiceChunkPlugin.js +0 -46
  131. package/dist/webpack-config/presets/service/XhsV2AssetsPlugin.d.ts +0 -5
  132. package/dist/webpack-config/presets/service/XhsV2AssetsPlugin.js +0 -27
  133. package/dist/webpack-config/presets/service/index.d.ts +0 -4
  134. package/dist/webpack-config/presets/service/index.js +0 -39
  135. /package/dist/{webpack-config/loaders/component-entry-loader.d.ts → compilerCP.d.ts} +0 -0
  136. /package/dist/{webpack-config/loaders/component-entry-requirements-loader.d.ts → presets/loaders/mg-entry-loader.d.ts} +0 -0
  137. /package/dist/{webpack-config → presets}/loaders/mini-style-loader.d.ts +0 -0
  138. /package/dist/{webpack-config/loaders/render-entry-loader.d.ts → presets/loaders/mp-entry-loader.d.ts} +0 -0
  139. /package/dist/{webpack-config → presets}/loaders/sjs-loader/index.d.ts +0 -0
  140. /package/dist/{webpack-config → presets}/loaders/sjs-loader/scope.d.ts +0 -0
  141. /package/dist/{webpack-config → presets}/loaders/sjs-loader/scope.js +0 -0
  142. /package/dist/{webpack-config → presets}/loaders/sjs-loader/tranform.d.ts +0 -0
  143. /package/dist/{webpack-config → presets}/loaders/sjs-loader/tranform.js +0 -0
  144. /package/dist/{webpack-config → presets}/loaders/wxss-loader.d.ts +0 -0
  145. /package/dist/{webpack-config/lib → utils}/css.d.ts +0 -0
  146. /package/dist/{webpack-config/lib → utils}/postcssRpx2Vw.d.ts +0 -0
  147. /package/dist/{webpack-config/lib → utils}/postcssRpx2Vw.js +0 -0
  148. /package/dist/{webpack-config/lib → utils}/tagTransformMap.d.ts +0 -0
  149. /package/dist/{webpack-config/lib → utils}/tagTransformMap.js +0 -0
  150. /package/dist/{webpack-config/lib → utils}/utils.d.ts +0 -0
@@ -0,0 +1,175 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const _1 = require("./");
13
+ let globalConfig = JSON.parse(process.argv[2]);
14
+ let hadWatchBuilt = false;
15
+ let projectCompiler;
16
+ /* 发送消息给父进程 */
17
+ const sendToMaster = (method, data = {}) => {
18
+ var _a;
19
+ (_a = process.send) === null || _a === void 0 ? void 0 : _a.call(process, JSON.stringify(Object.assign({ method }, data)));
20
+ };
21
+ const tryGC = () => {
22
+ if (global.gc) {
23
+ global.gc();
24
+ }
25
+ };
26
+ function formatWebpackMessages(err, stats) {
27
+ if (err) {
28
+ return { errors: [err.message], warnings: [] };
29
+ }
30
+ const json = stats.toJson({ warnings: true, errors: true, all: false });
31
+ const formattedErrors = json.errors.map(item => item.message);
32
+ const formattedWarnings = json.warnings.map(item => item.message);
33
+ const result = { errors: formattedErrors, warnings: formattedWarnings };
34
+ return result;
35
+ }
36
+ const handlers = {
37
+ runWatch: (msg) => __awaiter(void 0, void 0, void 0, function* () {
38
+ const { uuid, data } = msg;
39
+ if (!projectCompiler) {
40
+ projectCompiler = (0, _1.createCompiler)(data);
41
+ }
42
+ sendToMaster('run watch start');
43
+ const { err, stats } = (yield projectCompiler.watch({
44
+ scene: data.scene
45
+ }, (err, stats) => {
46
+ var _a;
47
+ console.log('file change...');
48
+ // 修改文件时构建
49
+ const hasError = err || ((_a = stats === null || stats === void 0 ? void 0 : stats.hasErrors) === null || _a === void 0 ? void 0 : _a.call(stats));
50
+ if (hasError) {
51
+ sendToMaster('emitCompileStats', {
52
+ stats: formatWebpackMessages(err, stats)
53
+ });
54
+ return;
55
+ }
56
+ sendToMaster('compileFinishWhenFileChange');
57
+ }));
58
+ console.log('it is watching...');
59
+ const hasError = err || stats.hasErrors();
60
+ if (hasError) {
61
+ sendToMaster('callback', {
62
+ uuid,
63
+ success: false,
64
+ stats: formatWebpackMessages(err, stats)
65
+ });
66
+ }
67
+ else {
68
+ hadWatchBuilt = true;
69
+ sendToMaster('callback', {
70
+ uuid,
71
+ success: true
72
+ });
73
+ }
74
+ }),
75
+ changeCompileOptions(msg) {
76
+ const { data } = msg;
77
+ projectCompiler === null || projectCompiler === void 0 ? void 0 : projectCompiler.changeCompileOptions(data);
78
+ },
79
+ activatePkgs: (msg) => __awaiter(void 0, void 0, void 0, function* () {
80
+ var _a, _b, _c;
81
+ const { uuid, data: { compilePkgs } } = msg;
82
+ console.log('run activate start', compilePkgs);
83
+ const { err, stats } = (yield ((_b = (_a = projectCompiler).activatePkgs) === null || _b === void 0 ? void 0 : _b.call(_a, compilePkgs)));
84
+ console.log('run activate finish', err, stats);
85
+ const hasError = err || ((_c = stats === null || stats === void 0 ? void 0 : stats.hasErrors) === null || _c === void 0 ? void 0 : _c.call(stats));
86
+ if (hasError) {
87
+ sendToMaster('callback', {
88
+ uuid,
89
+ success: false,
90
+ stats: formatWebpackMessages(err, stats)
91
+ });
92
+ }
93
+ else {
94
+ hadWatchBuilt = true;
95
+ sendToMaster('callback', {
96
+ uuid,
97
+ success: true
98
+ });
99
+ }
100
+ }),
101
+ runBuild: msg => {
102
+ console.log('sp run build', msg);
103
+ const { uuid, data } = msg;
104
+ if (!projectCompiler) {
105
+ projectCompiler = (0, _1.createCompiler)(data);
106
+ }
107
+ projectCompiler.on('progress', progress => {
108
+ sendToMaster('compilePercent', {
109
+ percentage: progress
110
+ });
111
+ });
112
+ projectCompiler.build((err, stats) => {
113
+ var _a;
114
+ const hasError = err || ((_a = stats === null || stats === void 0 ? void 0 : stats.hasErrors) === null || _a === void 0 ? void 0 : _a.call(stats));
115
+ console.log('sp run build finish', stats);
116
+ if (hasError) {
117
+ const statsInfo = formatWebpackMessages(err, stats);
118
+ sendToMaster('callback', {
119
+ uuid,
120
+ success: false,
121
+ stats: statsInfo
122
+ });
123
+ return;
124
+ }
125
+ sendToMaster('callback', {
126
+ uuid,
127
+ success: true
128
+ });
129
+ tryGC();
130
+ });
131
+ },
132
+ runTask: (msg) => {
133
+ if (msg) {
134
+ globalConfig = Object.assign(Object.assign({}, globalConfig), msg);
135
+ }
136
+ const { uuid, data } = globalConfig;
137
+ if (data.action === 'dev') {
138
+ handlers.runWatch({ uuid, data });
139
+ }
140
+ else {
141
+ handlers.runBuild({ uuid, data });
142
+ }
143
+ },
144
+ close() {
145
+ var _a;
146
+ console.log('close');
147
+ (_a = projectCompiler === null || projectCompiler === void 0 ? void 0 : projectCompiler.close) === null || _a === void 0 ? void 0 : _a.call(projectCompiler, () => { });
148
+ projectCompiler = null;
149
+ }
150
+ };
151
+ process.on('message', (message) => {
152
+ let objData = {};
153
+ try {
154
+ objData = JSON.parse(message);
155
+ }
156
+ catch (error) {
157
+ // do something
158
+ }
159
+ const { uuid, method, data = {} } = objData;
160
+ if (!handlers[method]) {
161
+ return;
162
+ }
163
+ handlers[method](objData);
164
+ });
165
+ process.on('uncaughtException', error => {
166
+ console.error(error);
167
+ sendToMaster('emitCompileStats', {
168
+ stats: {
169
+ errors: [error.message]
170
+ }
171
+ });
172
+ });
173
+ process.on('disconnect', () => {
174
+ process.exit(-1);
175
+ });
@@ -0,0 +1,7 @@
1
+ export declare const MAIN_PKG_ROOT = "xhs-main";
2
+ export declare const COMPILER_TYPE: {
3
+ dev: string;
4
+ build: string;
5
+ legacy_dev: string;
6
+ legacy_build: string;
7
+ };
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.COMPILER_TYPE = exports.MAIN_PKG_ROOT = void 0;
4
+ exports.MAIN_PKG_ROOT = 'xhs-main';
5
+ exports.COMPILER_TYPE = {
6
+ 'dev': 'dev',
7
+ 'build': 'build',
8
+ 'legacy_dev': 'legacy_dev',
9
+ 'legacy_build': 'legacy_build'
10
+ };
@@ -1,3 +1,3 @@
1
- import { CliConfig } from '../types';
2
- declare const devServer: (config: CliConfig) => Promise<void>;
1
+ import { IPresetOptions } from '../types';
2
+ declare const devServer: (config: IPresetOptions) => Promise<void>;
3
3
  export default devServer;
@@ -16,22 +16,24 @@ const path_1 = __importDefault(require("path"));
16
16
  const webpack_1 = __importDefault(require("webpack"));
17
17
  const ensurePort_1 = __importDefault(require("./lib/ensurePort"));
18
18
  const openBrowser_1 = __importDefault(require("./lib/openBrowser"));
19
- const webpack_config_1 = __importDefault(require("../webpack-config"));
19
+ const presets_1 = require("../presets");
20
20
  const devServer = (config) => __awaiter(void 0, void 0, void 0, function* () {
21
- const webpackConfig = (0, webpack_config_1.default)(config);
22
- const compiler = (0, webpack_1.default)(webpackConfig);
21
+ const { packSetting } = config;
22
+ const { devServerPort, distDir } = packSetting;
23
+ const webpackConfigs = (0, presets_1.createConfigs)(config);
24
+ const compiler = (0, webpack_1.default)(webpackConfigs);
23
25
  const host = 'localhost';
24
26
  const devServerConfig = {
25
27
  open: false,
26
28
  historyApiFallback: true,
27
- port: (config === null || config === void 0 ? void 0 : config.port) || 1388,
29
+ port: devServerPort || 1388,
28
30
  hot: false,
29
31
  static: [
30
32
  // emulator entry 地址
31
33
  path_1.default.resolve('node_modules/@xhs/mp-simulator/dist'),
32
34
  path_1.default.resolve('node_modules/@xhs/mp-js-core/dist'),
33
35
  ...JSON.parse(process.env.XHS_MP_LIB || '[]'), // 自定义地址
34
- config.distDir,
36
+ distDir,
35
37
  ],
36
38
  client: {
37
39
  logging: 'error',
package/dist/index.d.ts CHANGED
@@ -1,11 +1 @@
1
- import webpack from 'webpack';
2
- import { IStart } from './types';
3
- declare const start: (startConfig: IStart) => Promise<unknown>;
4
- export default start;
5
- export declare function nodeStart(startConfig: IStart): webpack.MultiCompiler;
6
- export declare const utils: {
7
- transformAppJson: (mpDir: string, miniprogramDir: string, extJsonPath?: string | undefined, compilation?: webpack.Compilation | undefined) => {
8
- legacyAppJSON: any;
9
- appJSON: import("./types").AppJSON;
10
- };
11
- };
1
+ export { createCompiler } from './packs';
package/dist/index.js CHANGED
@@ -1,114 +1,5 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
2
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.utils = exports.nodeStart = void 0;
16
- const path_1 = __importDefault(require("path"));
17
- const webpack_1 = __importDefault(require("webpack"));
18
- const fs_extra_1 = require("fs-extra");
19
- const dev_server_1 = __importDefault(require("./dev-server"));
20
- const build_1 = __importDefault(require("./build"));
21
- const webpack_config_1 = __importDefault(require("./webpack-config"));
22
- const common_1 = require("./utils/common");
23
- const types_1 = require("./types");
24
- const projectConfig_1 = require("./utils/projectConfig");
25
- const transformAppJson_1 = __importDefault(require("./utils/transformAppJson"));
26
- // 防御编程
27
- const handleVersion = (startConfig) => {
28
- let { action, enableV1 = true, enableV2 = false, enableVDom = false } = startConfig;
29
- // 正式环境必须有v1 并且不允许并行编译
30
- if (action === 'build') {
31
- enableV1 = true;
32
- }
33
- if (!enableV1 && !enableV2) {
34
- enableV1 = true;
35
- enableV2 = false;
36
- }
37
- return {
38
- enableV1,
39
- enableV2,
40
- enableVDom,
41
- };
42
- };
43
- const start = (startConfig) => __awaiter(void 0, void 0, void 0, function* () {
44
- const { mpDir, action, callback, platform, extJsonPath, port } = startConfig;
45
- process.env.__platform = platform;
46
- const { enableV1, enableV2, enableVDom } = handleVersion(startConfig);
47
- const projectJson = (0, projectConfig_1.getProjectConfig)(mpDir);
48
- let miniprogramDir = path_1.default.join(mpDir, projectJson.miniprogramRoot || '');
49
- miniprogramDir = miniprogramDir.endsWith('/') ? miniprogramDir.slice(0, -1) : miniprogramDir;
50
- const config = {
51
- mpDir: (0, common_1.toUnixPath)(mpDir),
52
- miniprogramDir: (0, common_1.toUnixPath)(miniprogramDir).replace(/\/+$/, ""),
53
- extJsonPath,
54
- distDir: startConfig.distDir || path_1.default.join(process.cwd(), 'dist'),
55
- env: action === 'dev' ? types_1.ENV.development : types_1.ENV.production,
56
- watch: action === 'dev' || startConfig.watch,
57
- tsConfigPath: (0, common_1.getTsConfigPath)(mpDir),
58
- compressJs: false,
59
- enableSourcemap: true,
60
- port,
61
- enableV1,
62
- enableV2,
63
- enableVDom,
64
- };
65
- if (action === 'dev') {
66
- return (0, dev_server_1.default)(config);
67
- }
68
- return (0, build_1.default)(config, callback);
69
- });
70
- exports.default = start;
71
- function nodeStart(startConfig) {
72
- const { mpDir, action, tsConfigPath, platform, extJsonPath, compressJs = false, enableSourcemap = false, } = startConfig;
73
- process.env.__platform = platform;
74
- const { enableV1, enableV2, enableVDom } = handleVersion(startConfig);
75
- const distDir = startConfig.distDir || path_1.default.join(process.cwd(), 'dist');
76
- try {
77
- if ((0, fs_extra_1.existsSync)(distDir)) { // eslint-disable-line
78
- (0, fs_extra_1.rmSync)(distDir, { recursive: true });
79
- }
80
- }
81
- catch (error) {
82
- // noop
83
- }
84
- const projectJson = (0, projectConfig_1.getProjectConfig)(mpDir);
85
- let miniprogramDir = path_1.default.join(mpDir, projectJson.miniprogramRoot || '');
86
- miniprogramDir = miniprogramDir.endsWith('/') ? miniprogramDir.slice(0, -1) : miniprogramDir;
87
- const config = {
88
- mpDir: (0, common_1.toUnixPath)(mpDir),
89
- miniprogramDir: (0, common_1.toUnixPath)(miniprogramDir).replace(/\/+$/, ""),
90
- extJsonPath,
91
- distDir,
92
- env: action === 'dev' ? types_1.ENV.development : types_1.ENV.production,
93
- tsConfigPath: tsConfigPath || (0, common_1.getTsConfigPath)(mpDir),
94
- cacheDirectory: startConfig.cacheDirectory,
95
- compressJs,
96
- enableSourcemap,
97
- enableV1,
98
- enableV2,
99
- enableVDom,
100
- };
101
- const webpackConfig = (0, webpack_config_1.default)(config);
102
- webpackConfig.forEach(config => {
103
- var _a, _b, _c, _d;
104
- /* 修改在ide里的node_modules的路径 */
105
- (_b = (_a = config.resolve) === null || _a === void 0 ? void 0 : _a.modules) === null || _b === void 0 ? void 0 : _b.push(path_1.default.resolve(__dirname, '../../../'));
106
- // cli的node_modules路径
107
- (_d = (_c = config.resolve) === null || _c === void 0 ? void 0 : _c.modules) === null || _d === void 0 ? void 0 : _d.push(path_1.default.resolve(__dirname, '../../'));
108
- });
109
- return (0, webpack_1.default)(webpackConfig);
110
- }
111
- exports.nodeStart = nodeStart;
112
- exports.utils = {
113
- transformAppJson: transformAppJson_1.default,
114
- };
3
+ exports.createCompiler = void 0;
4
+ var packs_1 = require("./packs");
5
+ Object.defineProperty(exports, "createCompiler", { enumerable: true, get: function () { return packs_1.createCompiler; } });
@@ -0,0 +1,14 @@
1
+ import { Compilation, RawSource, CopyPatterns, Config, ConcatSource, MPPackSetting } from 'xhs-mp-pack';
2
+ import { Configuration, WebpackError, library, LoaderContext } from 'webpack';
3
+ import type { Project } from 'xhs-mp-project';
4
+ import { IStart } from '../types';
5
+ export type ICompilation = any;
6
+ export type ICompiler = any;
7
+ export type ILoaderContext = LoaderContext<any> & {
8
+ _compiler: {
9
+ project: Project;
10
+ packSetting: MPPackSetting;
11
+ };
12
+ };
13
+ export { Compilation, RawSource, ConcatSource, CopyPatterns, Config, Configuration, WebpackError, library, LoaderContext };
14
+ export declare function createCompiler(startConfig: IStart): ICompiler;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createCompiler = exports.library = exports.WebpackError = exports.Config = exports.CopyPatterns = exports.ConcatSource = exports.RawSource = exports.Compilation = void 0;
7
+ /**
8
+ * 抹平差异:抹平webpack和mp-pack的差异
9
+ * 统一收口,所有对webpack和mp-pack的调用都走这个出口,不可用直接调用
10
+ */
11
+ const webpack_1 = require("./webpack");
12
+ const mp_pack_1 = require("./mp-pack");
13
+ const xhs_mp_pack_1 = require("xhs-mp-pack");
14
+ Object.defineProperty(exports, "Compilation", { enumerable: true, get: function () { return xhs_mp_pack_1.Compilation; } });
15
+ Object.defineProperty(exports, "RawSource", { enumerable: true, get: function () { return xhs_mp_pack_1.RawSource; } });
16
+ Object.defineProperty(exports, "CopyPatterns", { enumerable: true, get: function () { return xhs_mp_pack_1.CopyPatterns; } });
17
+ Object.defineProperty(exports, "Config", { enumerable: true, get: function () { return xhs_mp_pack_1.Config; } });
18
+ Object.defineProperty(exports, "ConcatSource", { enumerable: true, get: function () { return xhs_mp_pack_1.ConcatSource; } });
19
+ const webpack_2 = require("webpack");
20
+ Object.defineProperty(exports, "WebpackError", { enumerable: true, get: function () { return webpack_2.WebpackError; } });
21
+ Object.defineProperty(exports, "library", { enumerable: true, get: function () { return webpack_2.library; } });
22
+ const const_1 = __importDefault(require("../utils/const"));
23
+ const { PACK_MODE } = const_1.default;
24
+ function createCompiler(startConfig) {
25
+ const { packMode = PACK_MODE.MPPACK } = startConfig;
26
+ if (packMode === PACK_MODE.WEBPACK) {
27
+ return (0, webpack_1.createCompiler)(startConfig);
28
+ }
29
+ return (0, mp_pack_1.createCompiler)(startConfig);
30
+ }
31
+ exports.createCompiler = createCompiler;
@@ -0,0 +1,3 @@
1
+ import { IStart } from '../../types';
2
+ import { MPPack } from 'xhs-mp-pack';
3
+ export declare const createCompiler: (startConfig: IStart) => Promise<void> | MPPack;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createCompiler = void 0;
7
+ const presets_1 = require("../../presets");
8
+ const path_1 = __importDefault(require("path"));
9
+ const xhs_mp_pack_1 = require("xhs-mp-pack");
10
+ const dev_server_1 = __importDefault(require("../../dev-server"));
11
+ const createCompiler = (startConfig) => {
12
+ const { action, enableDevServer } = startConfig;
13
+ const mppack = new xhs_mp_pack_1.MPPack(startConfig);
14
+ const compilerOptions = {
15
+ project: mppack.project,
16
+ packSetting: mppack.packSetting
17
+ };
18
+ const mpPackConfigs = (0, presets_1.createConfigs)(compilerOptions);
19
+ mpPackConfigs.forEach(config => {
20
+ var _a, _b, _c, _d;
21
+ /* 修改在ide里的node_modules的路径 */
22
+ (_b = (_a = config.resolve) === null || _a === void 0 ? void 0 : _a.modules) === null || _b === void 0 ? void 0 : _b.push(path_1.default.resolve(__dirname, '../../../'));
23
+ // cli的node_modules路径
24
+ (_d = (_c = config.resolve) === null || _c === void 0 ? void 0 : _c.modules) === null || _d === void 0 ? void 0 : _d.push(path_1.default.resolve(__dirname, '../../'));
25
+ });
26
+ if (action === 'dev' && enableDevServer) {
27
+ return (0, dev_server_1.default)(compilerOptions);
28
+ }
29
+ mppack.createCompiler(mpPackConfigs);
30
+ return mppack;
31
+ };
32
+ exports.createCompiler = createCompiler;
@@ -0,0 +1,3 @@
1
+ import { IStart } from '../../types';
2
+ import { WebPack } from './webpack';
3
+ export declare const createCompiler: (startConfig: IStart) => Promise<void> | WebPack;
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.createCompiler = void 0;
7
+ const path_1 = __importDefault(require("path"));
8
+ const fs_extra_1 = __importDefault(require("fs-extra"));
9
+ const dev_server_1 = __importDefault(require("../../dev-server"));
10
+ const projectConfig_1 = require("../../utils/projectConfig");
11
+ const common_1 = require("../../utils/common");
12
+ const types_1 = require("../../types");
13
+ const presets_1 = require("../../presets");
14
+ const xhs_mp_project_1 = require("xhs-mp-project");
15
+ const xhs_mp_pack_1 = require("xhs-mp-pack");
16
+ const webpack_1 = require("./webpack");
17
+ // 防御编程
18
+ const handleVersion = (startConfig) => {
19
+ let { action, enableV1 = true, enableV2 = false, enableVDom = false } = startConfig;
20
+ // 正式环境必须有v1 并且不允许并行编译
21
+ if (action === 'build') {
22
+ enableV1 = true;
23
+ }
24
+ if (!enableV1 && !enableV2) {
25
+ enableV1 = true;
26
+ enableV2 = false;
27
+ }
28
+ return {
29
+ enableV1,
30
+ enableV2,
31
+ enableVDom
32
+ };
33
+ };
34
+ const formatCompilerConfig = startConfig => {
35
+ const { projectPath, action, tsConfigPath, platform, extJsonPath, compressJs = false, enableSourcemap = false } = startConfig;
36
+ const { enableV1, enableV2, enableVDom } = handleVersion(startConfig);
37
+ const distDir = startConfig.distDir || path_1.default.join(process.cwd(), 'dist');
38
+ process.env.__platform = platform;
39
+ try {
40
+ if (fs_extra_1.default.existsSync(distDir)) {
41
+ // eslint-disable-line
42
+ fs_extra_1.default.rmSync(distDir, { recursive: true });
43
+ }
44
+ }
45
+ catch (error) {
46
+ // noop
47
+ }
48
+ const projectJson = (0, projectConfig_1.getProjectConfig)(projectPath);
49
+ let miniprogramDir = path_1.default.join(projectPath, projectJson.miniprogramRoot || '');
50
+ miniprogramDir = miniprogramDir.endsWith('/') ? miniprogramDir.slice(0, -1) : miniprogramDir;
51
+ const config = {
52
+ projectPath: (0, common_1.toUnixPath)(projectPath),
53
+ miniprogramDir: (0, common_1.toUnixPath)(miniprogramDir).replace(/\/+$/, ''),
54
+ extJsonPath,
55
+ distDir,
56
+ action,
57
+ env: action === 'dev' ? types_1.ENV.development : types_1.ENV.production,
58
+ tsConfigPath: tsConfigPath || (0, common_1.getTsConfigPath)(projectPath),
59
+ cacheDirectory: startConfig.cacheDirectory,
60
+ compressJs,
61
+ enableSourcemap,
62
+ enableV1,
63
+ enableV2,
64
+ enableVDom
65
+ };
66
+ return config;
67
+ };
68
+ const createCompiler = (startConfig) => {
69
+ const { projectPath, action, enableDevServer, callback } = startConfig;
70
+ startConfig = formatCompilerConfig(startConfig);
71
+ const project = new xhs_mp_project_1.Project({
72
+ projectPath,
73
+ });
74
+ const mppackSetting = new xhs_mp_pack_1.MPPackSetting(project, startConfig);
75
+ const compilerOptions = {
76
+ project: project,
77
+ packSetting: mppackSetting
78
+ };
79
+ const webpackConfig = (0, presets_1.createConfigs)(compilerOptions);
80
+ webpackConfig.forEach(config => {
81
+ var _a, _b, _c, _d;
82
+ /* 修改在ide里的node_modules的路径 */
83
+ (_b = (_a = config.resolve) === null || _a === void 0 ? void 0 : _a.modules) === null || _b === void 0 ? void 0 : _b.push(path_1.default.resolve(__dirname, '../../../../'));
84
+ // cli的node_modules路径
85
+ (_d = (_c = config.resolve) === null || _c === void 0 ? void 0 : _c.modules) === null || _d === void 0 ? void 0 : _d.push(path_1.default.resolve(__dirname, '../../../'));
86
+ });
87
+ if (action === 'dev' && enableDevServer) {
88
+ return (0, dev_server_1.default)(compilerOptions);
89
+ }
90
+ return new webpack_1.WebPack(webpackConfig, compilerOptions);
91
+ };
92
+ exports.createCompiler = createCompiler;
@@ -0,0 +1,21 @@
1
+ import { AbstractMPPack, MPPackSetting } from 'xhs-mp-pack';
2
+ import { Watching } from 'webpack';
3
+ import { IPresetOptions } from '../../types';
4
+ import { Project } from 'xhs-mp-project';
5
+ export declare class WebPack extends AbstractMPPack {
6
+ callbacks: any[];
7
+ compiler: any;
8
+ watcher: Watching | null;
9
+ project: Project;
10
+ packSetting: MPPackSetting;
11
+ hadWatch: boolean;
12
+ constructor(configs: any, options: IPresetOptions);
13
+ handleProgress: (progress: any) => void;
14
+ get hooks(): any;
15
+ watch(config: any, callback?: any): Promise<unknown>;
16
+ changeCompileOptions(config: any): void;
17
+ activatePkgs(compilePkgs: any): void;
18
+ reCompilePkgs(config: any, callback?: any): Promise<void>;
19
+ build(callback?: any): void;
20
+ close(callback: any): void;
21
+ }
@@ -0,0 +1,79 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.WebPack = void 0;
13
+ const xhs_mp_pack_1 = require("xhs-mp-pack");
14
+ const webpack_1 = require("webpack");
15
+ const watchOptions = {
16
+ aggregateTimeout: 300,
17
+ poll: false,
18
+ ignored: ['**/node_modules', 'project.config.json', 'project.private.config.json']
19
+ };
20
+ // 对webpack进行一层包装,和mp-pack对齐
21
+ class WebPack extends xhs_mp_pack_1.AbstractMPPack {
22
+ constructor(configs, options) {
23
+ super();
24
+ this.callbacks = [];
25
+ this.compiler = null;
26
+ this.watcher = null;
27
+ this.hadWatch = false;
28
+ this.handleProgress = progress => {
29
+ this.emit('progress', progress);
30
+ };
31
+ this.project = options.project;
32
+ this.packSetting = options.packSetting;
33
+ this.compiler = (0, webpack_1.webpack)(configs);
34
+ new webpack_1.ProgressPlugin(this.handleProgress).apply(this.compiler);
35
+ }
36
+ get hooks() {
37
+ return this.compiler.hooks;
38
+ }
39
+ watch(config, callback) {
40
+ return new Promise(resolve => {
41
+ if (this.watcher) {
42
+ this.watcher.close(() => { });
43
+ this.watcher = null;
44
+ }
45
+ if (config.scene === 'reWatch') {
46
+ this.hadWatch = false;
47
+ }
48
+ this.watcher = this.compiler.watch(Object.assign(Object.assign({}, watchOptions), config), (err, stats) => {
49
+ if (!this.hadWatch) {
50
+ this.hadWatch = true;
51
+ resolve({
52
+ err, stats
53
+ });
54
+ }
55
+ else {
56
+ callback === null || callback === void 0 ? void 0 : callback(err, stats);
57
+ }
58
+ });
59
+ });
60
+ }
61
+ changeCompileOptions(config) {
62
+ // 空实现
63
+ }
64
+ activatePkgs(compilePkgs) {
65
+ // 空实现
66
+ }
67
+ reCompilePkgs(config, callback) {
68
+ return __awaiter(this, void 0, void 0, function* () {
69
+ // 空实现
70
+ });
71
+ }
72
+ build(callback) {
73
+ this.compiler.run(callback);
74
+ }
75
+ close(callback) {
76
+ this.compiler.close(callback);
77
+ }
78
+ }
79
+ exports.WebPack = WebPack;
@@ -1,8 +1,8 @@
1
- import Config from 'webpack-chain';
2
- import { WebpackPresetOptions } from '../../types';
1
+ import { Config } from '../packs';
2
+ import { IPresetOptions } from '../types';
3
3
  /**
4
4
  * babel 仅关注语法转换,polyfill 在 js-core 中已经内置
5
5
  * 目前 polyfill 仅支持到 'ios_saf >= 10', 'chrome >= 83'。语法构建中,会默认支持。
6
6
  */
7
- declare const getBaseConfig: (chain: Config, options: WebpackPresetOptions) => void;
7
+ declare const getBaseConfig: (chain: Config, options: IPresetOptions) => void;
8
8
  export default getBaseConfig;