xhs-mp-compiler-cli 1.0.2 → 1.1.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
@@ -3,14 +3,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- const const_1 = __importDefault(require("../lib/const"));
7
- const types_1 = require("../../types");
6
+ const const_1 = __importDefault(require("../utils/const"));
7
+ const types_1 = require("../types");
8
8
  /**
9
9
  * babel 仅关注语法转换,polyfill 在 js-core 中已经内置
10
10
  * 目前 polyfill 仅支持到 'ios_saf >= 10', 'chrome >= 83'。语法构建中,会默认支持。
11
11
  */
12
12
  const getBaseConfig = (chain, options) => {
13
- const swcTargets = options.env === types_1.ENV.development ? const_1.default.DEV_TARGETS : const_1.default.PROD_TARGETS;
13
+ const { packSetting } = options;
14
+ const { env, tsConfigPath } = packSetting;
15
+ const swcTargets = env === types_1.ENV.development ? const_1.default.DEV_TARGETS : const_1.default.PROD_TARGETS;
14
16
  chain.module
15
17
  .rule('js')
16
18
  .test(/\.js$/)
@@ -22,7 +24,7 @@ const getBaseConfig = (chain, options) => {
22
24
  jsc: { externalHelpers: true },
23
25
  env: {
24
26
  targets: swcTargets,
25
- coreJS: 3,
27
+ coreJS: 3
26
28
  }
27
29
  });
28
30
  chain.module
@@ -42,16 +44,16 @@ const getBaseConfig = (chain, options) => {
42
44
  coreJS: 3
43
45
  }
44
46
  });
45
- if (options.tsConfigPath) {
47
+ if (tsConfigPath) {
46
48
  chain.resolve.extensions.add('.ts');
47
- chain
48
- .plugin('ts-checker')
49
- .use(require.resolve('fork-ts-checker-webpack-plugin'), [{
49
+ chain.plugin('ts-checker').use(require.resolve('fork-ts-checker-webpack-plugin'), [
50
+ {
50
51
  async: true,
51
52
  typescript: {
52
- configFile: options.tsConfigPath,
53
- },
54
- }]);
53
+ configFile: tsConfigPath
54
+ }
55
+ }
56
+ ]);
55
57
  }
56
58
  };
57
59
  exports.default = getBaseConfig;
@@ -0,0 +1,4 @@
1
+ import { Config } from '../packs';
2
+ import { IPresetOptions } from '../types';
3
+ declare const presetCommon: (chain: Config, options: IPresetOptions) => void;
4
+ export default presetCommon;
@@ -0,0 +1,46 @@
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
+ const path_1 = __importDefault(require("path"));
7
+ const types_1 = require("../types");
8
+ const presetCommon = (chain, options) => {
9
+ const { packSetting, project } = options;
10
+ const { env, watch, enableSourcemap, cacheDirectory } = packSetting;
11
+ chain.merge({
12
+ stats: 'errors-only',
13
+ output: {
14
+ pathinfo: false,
15
+ },
16
+ resolve: {
17
+ roots: [project.miniprogramDir],
18
+ extensions: ['.js'],
19
+ modules: [
20
+ path_1.default.join(project.projectPath, 'node_modules'),
21
+ project.projectPath,
22
+ path_1.default.join(__dirname, '../../node_modules/')
23
+ ],
24
+ },
25
+ resolveLoader: {
26
+ modules: [path_1.default.join(__dirname, './loaders')],
27
+ extensions: ['.js', '.ts'],
28
+ },
29
+ });
30
+ chain.watch(!!watch);
31
+ chain.mode('development');
32
+ chain.devtool(enableSourcemap ? 'source-map' : false);
33
+ // @ts-ignore
34
+ chain.target(['web', 'es5']);
35
+ chain.cache(env === types_1.ENV.production
36
+ ? false
37
+ : {
38
+ type: 'filesystem',
39
+ cacheDirectory: cacheDirectory || path_1.default.join(__dirname, '../../node_modules/.cache'),
40
+ version: project.miniprogramDir,
41
+ buildDependencies: {
42
+ config: [path_1.default.join(project.miniprogramDir, 'app.json')],
43
+ },
44
+ });
45
+ };
46
+ exports.default = presetCommon;
@@ -0,0 +1,10 @@
1
+ import { ICompiler, RawSource } from '../../../../packs';
2
+ import { IPresetOptions } from '../../../../types';
3
+ declare class MpEntryPlugin {
4
+ options: IPresetOptions;
5
+ constructor(options: IPresetOptions);
6
+ apply(compiler: ICompiler): void;
7
+ getAppJson(appJSON: any): Record<string, RawSource>;
8
+ getLegacyAppJSON(appJSON: any): RawSource;
9
+ }
10
+ export default MpEntryPlugin;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const packs_1 = require("../../../../packs");
4
+ const lodash_1 = require("lodash");
5
+ const common_1 = require("../../../../utils/common");
6
+ const xhs_mp_project_1 = require("xhs-mp-project");
7
+ class MpEntryPlugin {
8
+ constructor(options) {
9
+ this.options = options;
10
+ }
11
+ apply(compiler) {
12
+ // 在资源优化阶段处理app.json
13
+ compiler.hooks.compilation.tap(MpEntryPlugin.name, compilation => {
14
+ // @ts-ignore
15
+ compilation.hooks.processAssets.tap({
16
+ name: MpEntryPlugin.name,
17
+ stage: packs_1.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY, // see below for more stages
18
+ }, (assets) => {
19
+ // @ts-ignore
20
+ const { appJSON } = this.options.project;
21
+ if (assets) {
22
+ Object.assign(assets, this.getAppJson((0, lodash_1.cloneDeep)(appJSON)));
23
+ // 兼容服务端,始终创建 v0/app.json
24
+ assets['v0/app.json'] = this.getLegacyAppJSON((0, lodash_1.cloneDeep)(appJSON));
25
+ }
26
+ });
27
+ });
28
+ }
29
+ getAppJson(appJSON) {
30
+ const assets = {};
31
+ const appJsonSource = new packs_1.RawSource(JSON.stringify(xhs_mp_project_1.utils.pureAppJSON(appJSON)));
32
+ assets['v1/app.json'] = appJsonSource;
33
+ for (const subpackage of appJSON.subPackages) {
34
+ if (subpackage.independent) {
35
+ assets[(0, common_1.unixJoin)("v1", subpackage.dirname, "app.json")] = appJsonSource;
36
+ }
37
+ }
38
+ return assets;
39
+ }
40
+ getLegacyAppJSON(appJSON) {
41
+ // 兼容的整包,将 subPackages 合并到 pages 内
42
+ appJSON = xhs_mp_project_1.utils.pureAppJSON(appJSON, 'v0');
43
+ return new packs_1.RawSource(JSON.stringify(appJSON));
44
+ }
45
+ }
46
+ exports.default = MpEntryPlugin;
@@ -0,0 +1,3 @@
1
+ import { Config } from '../../../../packs';
2
+ import { IPresetOptions } from '../../../../types';
3
+ export declare const presetAssets: (chain: Config, options: IPresetOptions) => void;
@@ -0,0 +1,65 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.presetAssets = void 0;
16
+ const path_1 = __importDefault(require("path"));
17
+ const assetsEntryPlugin_1 = __importDefault(require("./assetsEntryPlugin"));
18
+ const copy_webpack_plugin_1 = __importDefault(require("copy-webpack-plugin"));
19
+ const const_1 = __importDefault(require("../../../../utils/const"));
20
+ const InjectorPlugin_1 = require("../../../plugins/InjectorPlugin");
21
+ const ASSETS_EXTS = const_1.default.ASSETS_EXTS
22
+ .map(i => `.${i}`);
23
+ const presetAssets = (chain, options) => {
24
+ const { packSetting, project } = options;
25
+ const { distDir } = packSetting;
26
+ chain.merge({
27
+ output: {
28
+ path: distDir,
29
+ },
30
+ });
31
+ const projectJsons = [
32
+ 'game.json',
33
+ 'package.json',
34
+ 'package-lock.json',
35
+ 'pkgInfo.json',
36
+ 'project.config.json',
37
+ 'project.private.config.json',
38
+ ].map(i => path_1.default.resolve(project.projectPath, i));
39
+ const copyPatterns = [
40
+ {
41
+ noErrorOnMissing: true,
42
+ from: `${project.projectPath}/**/*`,
43
+ filter: (resourcePath) => __awaiter(void 0, void 0, void 0, function* () {
44
+ if (resourcePath.includes('node_modules')) {
45
+ return false;
46
+ }
47
+ const extname = path_1.default.extname(resourcePath);
48
+ if (ASSETS_EXTS.includes(extname)) {
49
+ return true;
50
+ }
51
+ if (extname === '.json' && !projectJsons.includes(resourcePath)) {
52
+ return true;
53
+ }
54
+ return false;
55
+ }),
56
+ to: 'v1',
57
+ context: project.projectPath,
58
+ globOptions: { gitignore: true },
59
+ },
60
+ ];
61
+ chain.plugin('InjectorPlugin').use(InjectorPlugin_1.InjectorPlugin, [options]);
62
+ chain.plugin('CopyPlugin').use(copy_webpack_plugin_1.default, [{ patterns: copyPatterns }]);
63
+ chain.plugin('AssetsEntryPlugin').use(assetsEntryPlugin_1.default, [options]);
64
+ };
65
+ exports.presetAssets = presetAssets;
@@ -0,0 +1,3 @@
1
+ import { Config } from '../../../../packs';
2
+ import { IPresetOptions } from '../../../../types';
3
+ export declare const presetService: (chain: Config, options: IPresetOptions) => void;
@@ -0,0 +1,34 @@
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.presetService = void 0;
7
+ const serviceEntryPlugin_1 = __importDefault(require("./serviceEntryPlugin"));
8
+ const serviceChunkPlugin_1 = __importDefault(require("./serviceChunkPlugin"));
9
+ const resolveLimitPlugin_1 = __importDefault(require("./resolveLimitPlugin"));
10
+ const InjectorPlugin_1 = require("../../../plugins/InjectorPlugin");
11
+ const presetService = (chain, options) => {
12
+ const { packSetting } = options;
13
+ const { distDir } = packSetting;
14
+ chain.merge({
15
+ output: {
16
+ path: distDir,
17
+ globalObject: 'globalThis'
18
+ }
19
+ });
20
+ chain.plugin('InjectorPlugin').use(InjectorPlugin_1.InjectorPlugin, [options]);
21
+ chain.resolve.plugin('ResolveLimitPlugin').use(resolveLimitPlugin_1.default, [options]);
22
+ chain.plugin('ServiceEntryPlugin').use(serviceEntryPlugin_1.default, [options]);
23
+ chain.plugin('ServiceChunkPlugin').use(serviceChunkPlugin_1.default, [options]);
24
+ chain.optimization
25
+ .runtimeChunk({
26
+ name: 'runtime'
27
+ })
28
+ .splitChunks({
29
+ chunks: chunk => !chunk.name.startsWith('v0/'),
30
+ minSize: 0,
31
+ name: 'service-common'
32
+ });
33
+ };
34
+ exports.presetService = presetService;
@@ -1,8 +1,8 @@
1
1
  import { Resolver } from 'enhanced-resolve';
2
- import { CliConfig } from '../../../types';
2
+ import { IPresetOptions } from '../../../../types';
3
3
  declare class XhsResolveLimitPlugin {
4
- miniprogramDir: string;
5
- constructor(options: CliConfig);
4
+ options: IPresetOptions;
5
+ constructor(options: IPresetOptions);
6
6
  apply(resolver: Resolver): void;
7
7
  }
8
8
  export default XhsResolveLimitPlugin;
@@ -0,0 +1,35 @@
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 fs_extra_1 = require("fs-extra");
13
+ class XhsResolveLimitPlugin {
14
+ constructor(options) {
15
+ this.options = options;
16
+ }
17
+ apply(resolver) {
18
+ const target1 = resolver.ensureHook("resolve");
19
+ resolver.getHook('file')
20
+ .tapAsync('XhsResolveLimitPlugin', (request, contextResolver, callback) => __awaiter(this, void 0, void 0, function* () {
21
+ var _a;
22
+ // @ts-ignore
23
+ const issuer = ((_a = request.context) === null || _a === void 0 ? void 0 : _a.issuer) || '';
24
+ const target = request.path || '';
25
+ if ((target.includes('node_modules') && (0, fs_extra_1.existsSync)(target))) {
26
+ return callback();
27
+ }
28
+ // console.log(`issuer: ${issuer}`, `target: ${target}`)
29
+ // return resolver.doResolve(target1, request, null, contextResolver, callback);
30
+ // console.log(issuer, target)
31
+ return callback();
32
+ }));
33
+ }
34
+ }
35
+ exports.default = XhsResolveLimitPlugin;
@@ -0,0 +1,9 @@
1
+ import { Compiler } from 'webpack';
2
+ import { IPresetOptions } from '../../../../types';
3
+ declare class ServiceChunkPlugin {
4
+ options: IPresetOptions;
5
+ constructor(options: IPresetOptions);
6
+ apply(compiler: Compiler): void;
7
+ updateService(compiler: any, compilation: any, runtimeCode: any, commonSource: any): void;
8
+ }
9
+ export default ServiceChunkPlugin;
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const webpack_1 = require("webpack");
4
+ const webpack_sources_1 = require("webpack-sources");
5
+ const common_1 = require("../../../../utils/common");
6
+ class ServiceChunkPlugin {
7
+ constructor(options) {
8
+ this.options = options;
9
+ }
10
+ apply(compiler) {
11
+ compiler.hooks.compilation.tap('ServiceChunkPlugin', compilation => {
12
+ compilation.hooks.processAssets.tap({
13
+ name: 'ServiceChunkPlugin',
14
+ stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
15
+ }, () => {
16
+ var _a, _b;
17
+ let runtimeCode = (_a = compilation.getAsset('runtime.js')) === null || _a === void 0 ? void 0 : _a.source.source();
18
+ // 当没有引用相同 js 场景时, commonChunk 将是 undefined
19
+ const commonSource = ((_b = compilation.getAsset('service-common.js')) === null || _b === void 0 ? void 0 : _b.source) || '';
20
+ runtimeCode = `
21
+ if (!globalThis.__XHS_RUNTIME_REGISTERED__) {
22
+ globalThis.__XHS_RUNTIME_REGISTERED__ = true;
23
+ ${runtimeCode}
24
+ }`;
25
+ this.updateService(compiler, compilation, runtimeCode, commonSource);
26
+ compilation.deleteAsset('runtime.js');
27
+ compilation.deleteAsset('service-common.js');
28
+ });
29
+ compilation.hooks.processAssets.tap({
30
+ name: 'ServiceChunkPlugin1',
31
+ stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE,
32
+ }, (assets) => {
33
+ var _a;
34
+ const { project } = this.options;
35
+ // service层注入gameGlobal
36
+ const services = [
37
+ "v1/service.js",
38
+ // @ts-ignore
39
+ ...(_a = project.appJSON.subPackages) === null || _a === void 0 ? void 0 : _a.map(i => (0, common_1.unixJoin)('v1', `${i.entry}.js`))
40
+ ];
41
+ services.forEach(servicePath => {
42
+ if (servicePath in assets) {
43
+ // @ts-ignore
44
+ compilation.updateAsset(servicePath, new webpack_sources_1.ConcatSource(`with(GameGlobal) {\n"use strict";\n`, assets[servicePath], '\n}'));
45
+ }
46
+ });
47
+ });
48
+ });
49
+ }
50
+ updateService(compiler, compilation, runtimeCode, commonSource) {
51
+ var _a;
52
+ const { project } = this.options;
53
+ // 独立分包
54
+ const independent = (_a = project.appJSON.subPackages) === null || _a === void 0 ? void 0 : _a.filter(sub => sub.independent);
55
+ const services = [
56
+ "v1/service.js",
57
+ ...independent === null || independent === void 0 ? void 0 : independent.map(i => (0, common_1.unixJoin)('v1', `${i.entry}.js`))
58
+ ];
59
+ services.forEach(servicePath => {
60
+ var _a;
61
+ const serviceCode = (_a = compilation.getAsset(servicePath)) === null || _a === void 0 ? void 0 : _a.source;
62
+ if (serviceCode) {
63
+ // @ts-ignore
64
+ compilation.updateAsset(servicePath, new webpack_sources_1.ConcatSource(runtimeCode, commonSource, serviceCode));
65
+ }
66
+ });
67
+ }
68
+ }
69
+ exports.default = ServiceChunkPlugin;
@@ -1,7 +1,9 @@
1
1
  import { Compiler } from 'webpack';
2
+ import { IPresetOptions } from '../../../../types';
2
3
  declare class ServiceEntryPlugin {
3
4
  entriesCache: Map<string, boolean>;
4
- constructor();
5
+ options: IPresetOptions;
6
+ constructor(options: IPresetOptions);
5
7
  apply(compiler: Compiler): void;
6
8
  makeUmdEntry(compiler: Compiler, filename: string, url: string, libraryName: string): void;
7
9
  }
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const webpack_1 = require("webpack");
4
+ const common_1 = require("../../../../utils/common");
5
+ class ServiceEntryPlugin {
6
+ constructor(options) {
7
+ this.options = options;
8
+ this.entriesCache = new Map();
9
+ }
10
+ apply(compiler) {
11
+ new webpack_1.library.EnableLibraryPlugin('umd').apply(compiler);
12
+ const { project } = this.options;
13
+ const { appJSON } = project;
14
+ const subPackages = appJSON.subPackages || [];
15
+ this.makeUmdEntry(compiler, 'v1/service.js', `mg-entry-loader?entry=${common_1.DEFAULT_ENTRY}!`, `XHS_SERVICE_${common_1.DEFAULT_ENTRY}`);
16
+ for (const { entry } of subPackages) {
17
+ this.makeUmdEntry(compiler, (0, common_1.unixJoin)('v1', `${entry}.js`), `mg-entry-loader?entry=${entry}!`, `XHS_SERVICE_${entry}`);
18
+ }
19
+ }
20
+ makeUmdEntry(compiler, filename, url, libraryName) {
21
+ if (this.entriesCache.get(filename))
22
+ return;
23
+ const entryOption = webpack_1.EntryOptionPlugin.entryDescriptionToOptions(compiler, filename, {
24
+ import: [url],
25
+ filename,
26
+ library: { name: libraryName, type: 'umd' }
27
+ });
28
+ new webpack_1.EntryPlugin(compiler.context, url, entryOption).apply(compiler);
29
+ this.entriesCache.set(filename, true);
30
+ }
31
+ }
32
+ exports.default = ServiceEntryPlugin;
@@ -0,0 +1,12 @@
1
+ import { IPresetOptions } from '../../../../types';
2
+ import { RawSource, CopyPatterns, ICompiler } from '../../../../packs';
3
+ declare class XhsAssetsPlugin {
4
+ options: IPresetOptions;
5
+ copyPatterns: CopyPatterns;
6
+ constructor(options: IPresetOptions);
7
+ apply(compiler: ICompiler): void;
8
+ emitAppJson(compilation: any, appJSON: any): void;
9
+ emitLegacyAppJSON(compilation: any, legacyAppJSON: any): void;
10
+ emitJson(compilation: any, path: string, json: RawSource): void;
11
+ }
12
+ export default XhsAssetsPlugin;
@@ -0,0 +1,132 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const packs_1 = require("../../../../packs");
16
+ const const_1 = __importDefault(require("../../../../utils/const"));
17
+ class XhsAssetsPlugin {
18
+ constructor(options) {
19
+ this.options = options;
20
+ this.copyPatterns = new packs_1.CopyPatterns();
21
+ }
22
+ apply(compiler) {
23
+ compiler.hooks.compilation.tap('XhsMpAssetsPlugin', compilation => {
24
+ const { project, packSetting } = this.options;
25
+ const { appJSON, legacyAppJSON } = project;
26
+ const usingPackageType = packSetting.usingPackageType;
27
+ // app.json
28
+ if (compilation) {
29
+ this.emitAppJson(compilation, JSON.parse(JSON.stringify(appJSON)));
30
+ // 兼容服务端,始终创建 v0/app.json
31
+ this.emitLegacyAppJSON(compilation, legacyAppJSON);
32
+ }
33
+ // app.css 复用
34
+ compilation.hooks.processAssets.tap({
35
+ name: 'XhsMpAssetsCommonPlugin',
36
+ stage: packs_1.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS
37
+ }, assets => {
38
+ // 删除因为 entry 是 app.(wxss|css) 而产生的的 js 文件
39
+ for (const key of Object.keys(assets)) {
40
+ if (key === `${usingPackageType}/${const_1.default.ENTRY_CSS_NAME}.js` ||
41
+ key === `${usingPackageType}/${const_1.default.ENTRY_CSS_NAME}.js.map`) {
42
+ Reflect.deleteProperty(assets, key);
43
+ }
44
+ }
45
+ // 复用app.css,copy v2到v1
46
+ if (packSetting.usingPackageType === 'v2' && packSetting.enableV1) {
47
+ if (assets[`${usingPackageType}/app.css`]) {
48
+ assets['v1/app.css'] = assets[`${usingPackageType}/app.css`];
49
+ }
50
+ }
51
+ });
52
+ // 图片等资源
53
+ const cache = compilation.getCache('CopyWebpackPlugin');
54
+ const logger = compilation.getLogger('XhsMpAssetsCopyPlugin');
55
+ compilation.hooks.processAssets.tapAsync({
56
+ name: 'XhsMpAssetsCopyPlugin',
57
+ stage: packs_1.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS
58
+ }, (assets, callback) => __awaiter(this, void 0, void 0, function* () {
59
+ const patterns = [];
60
+ if (packSetting.enableV2) {
61
+ patterns.push({
62
+ noErrorOnMissing: true,
63
+ from: `**/*.{${const_1.default.ASSETS_EXTS}}`,
64
+ to: 'v2',
65
+ context: project.projectMiniprogramPath,
66
+ globOptions: { gitignore: true }
67
+ });
68
+ }
69
+ if (packSetting.enableV1) {
70
+ patterns.push({
71
+ noErrorOnMissing: true,
72
+ from: `**/*.{${const_1.default.ASSETS_EXTS}}`,
73
+ to: 'v1',
74
+ context: project.projectMiniprogramPath,
75
+ globOptions: { gitignore: true }
76
+ });
77
+ }
78
+ yield this.copyPatterns.copy({
79
+ compiler,
80
+ compilation,
81
+ cache,
82
+ logger,
83
+ patterns
84
+ });
85
+ callback();
86
+ }));
87
+ });
88
+ }
89
+ emitAppJson(compilation, appJSON) {
90
+ const { project, packSetting } = this.options;
91
+ const { compilePkgs = [const_1.default.MAIN_PKG] } = packSetting;
92
+ // const { mainPkg, subPkgs } = packSetting.getCompilePkgsForEntries()
93
+ // 先删除,用户配置的默认不生效
94
+ Reflect.deleteProperty(appJSON, 'lazyCodeLoading');
95
+ if (packSetting.enableVDom) {
96
+ appJSON.xhsVersionStrategy.enableVDom = true;
97
+ appJSON.usingFramework = '2.0';
98
+ }
99
+ if (packSetting.enableV2) {
100
+ appJSON.xhsVersionStrategy.lazyCodeLoading = true;
101
+ appJSON.lazyCodeLoading = 'requiredComponents';
102
+ }
103
+ const appJsonSource = new packs_1.RawSource(JSON.stringify(appJSON));
104
+ // app.json合并了普通分包的json内容,每次都emit
105
+ if (packSetting.enableV2) {
106
+ this.emitJson(compilation, 'v2/app.json', appJsonSource);
107
+ }
108
+ if (packSetting.enableV1) {
109
+ this.emitJson(compilation, 'v1/app.json', appJsonSource);
110
+ }
111
+ // 独立分包的 app.json
112
+ const subPkgs = project.getSubPackages().filter(pkg => compilePkgs.includes(pkg.root));
113
+ subPkgs.forEach(subPkg => {
114
+ if (subPkg === null || subPkg === void 0 ? void 0 : subPkg.independent) {
115
+ if (packSetting.enableV2) {
116
+ this.emitJson(compilation, `v2/${subPkg.root}/app.json`, appJsonSource);
117
+ }
118
+ if (packSetting.enableV1) {
119
+ this.emitJson(compilation, `v1/${subPkg.root}/app.json`, appJsonSource);
120
+ }
121
+ }
122
+ });
123
+ }
124
+ emitLegacyAppJSON(compilation, legacyAppJSON) {
125
+ this.emitJson(compilation, 'v0/app.json', new packs_1.RawSource(JSON.stringify(legacyAppJSON)));
126
+ }
127
+ emitJson(compilation, path, json) {
128
+ // @ts-ignore
129
+ compilation.emitAsset(path, json);
130
+ }
131
+ }
132
+ exports.default = XhsAssetsPlugin;
@@ -0,0 +1,4 @@
1
+ import type { IPresetOptions } from '../../../../types';
2
+ declare const getAssetsEntry: (options: IPresetOptions) => () => {};
3
+ declare const presetAssets: (chain: any, options: IPresetOptions) => void;
4
+ export { getAssetsEntry, presetAssets };
@@ -0,0 +1,47 @@
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.presetAssets = exports.getAssetsEntry = void 0;
7
+ const css_1 = __importDefault(require("../../../../utils/css"));
8
+ const getSuffixName_1 = require("../../../../utils/getSuffixName");
9
+ const assetsEntryPlugin_1 = __importDefault(require("./assetsEntryPlugin"));
10
+ const const_1 = __importDefault(require("../../../../utils/const"));
11
+ const InjectorPlugin_1 = require("../../../plugins/InjectorPlugin");
12
+ const getAssetsEntry = (options) => () => {
13
+ const { project, packSetting } = options;
14
+ const entry = {};
15
+ entry[`${packSetting.usingPackageType}/${const_1.default.ENTRY_CSS_NAME}`] = `${project.projectMiniprogramPath}/app.${(0, getSuffixName_1.getCssSuffixName)()}`;
16
+ return entry;
17
+ };
18
+ exports.getAssetsEntry = getAssetsEntry;
19
+ const presetAssets = (chain, options) => {
20
+ const { packSetting, project } = options;
21
+ const { distDir, enableV1, enableV2 } = packSetting;
22
+ const { miniprogramDir } = project;
23
+ const cssReg = new RegExp(`.${(0, getSuffixName_1.getCssSuffixName)()}$`);
24
+ chain.merge({
25
+ output: {
26
+ path: distDir
27
+ },
28
+ // cache: {
29
+ // type: 'filesystem',
30
+ // cacheDirectory: packSetting.cacheDirectory,
31
+ // },
32
+ module: {
33
+ rule: {
34
+ [(0, getSuffixName_1.getCssSuffixName)()]: {
35
+ test: cssReg,
36
+ use: Object.assign({ 'mini-style-loader': {
37
+ loader: require.resolve('../../../loaders/mini-style-loader'),
38
+ options: Object.assign(Object.assign({}, options), { independent: true })
39
+ } }, (0, css_1.default)(options, ['css-loader', 'postcss-loader', 'wxss-loader']))
40
+ }
41
+ }
42
+ }
43
+ });
44
+ chain.plugin('InjectorPlugin').use(InjectorPlugin_1.InjectorPlugin, [options]);
45
+ chain.plugin('XhsMpAssetsPlugin').use(assetsEntryPlugin_1.default, [options]);
46
+ };
47
+ exports.presetAssets = presetAssets;
@@ -0,0 +1,3 @@
1
+ import { IPresetOptions } from '../../../../types';
2
+ declare const getComponentEntry: (options: IPresetOptions) => {};
3
+ export { getComponentEntry };