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

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 (110) hide show
  1. package/dist/bin/xhs-mp-cli-build.d.ts +2 -0
  2. package/dist/bin/xhs-mp-cli-build.js +40 -0
  3. package/dist/bin/xhs-mp-cli-dev.d.ts +2 -0
  4. package/dist/bin/xhs-mp-cli-dev.js +81 -0
  5. package/dist/bin/xhs-mp-cli.d.ts +2 -0
  6. package/dist/bin/xhs-mp-cli.js +16 -0
  7. package/dist/build/index.d.ts +3 -0
  8. package/dist/build/index.js +22 -0
  9. package/dist/dev-server/index.d.ts +3 -0
  10. package/dist/dev-server/index.js +62 -0
  11. package/dist/dev-server/lib/ensurePort.d.ts +2 -0
  12. package/dist/dev-server/lib/ensurePort.js +32 -0
  13. package/dist/dev-server/lib/openBrowser.d.ts +2 -0
  14. package/dist/dev-server/lib/openBrowser.js +23 -0
  15. package/dist/dev-server/lib/openChrome.applescript +78 -0
  16. package/dist/globalData/SjsModule.d.ts +32 -0
  17. package/dist/globalData/SjsModule.js +34 -0
  18. package/dist/index.d.ts +11 -0
  19. package/dist/index.js +114 -0
  20. package/dist/types/index.d.ts +106 -0
  21. package/dist/types/index.js +13 -0
  22. package/dist/utils/asyncWebpack.d.ts +3 -0
  23. package/dist/utils/asyncWebpack.js +38 -0
  24. package/dist/utils/combineExtJson.d.ts +3 -0
  25. package/dist/utils/combineExtJson.js +74 -0
  26. package/dist/utils/common.d.ts +7 -0
  27. package/dist/utils/common.js +35 -0
  28. package/dist/utils/depenedHelper.d.ts +31 -0
  29. package/dist/utils/depenedHelper.js +89 -0
  30. package/dist/utils/getCssSuffixName.d.ts +1 -0
  31. package/dist/utils/getCssSuffixName.js +9 -0
  32. package/dist/utils/getMlSuffixName.d.ts +1 -0
  33. package/dist/utils/getMlSuffixName.js +6 -0
  34. package/dist/utils/getPrefixName.d.ts +1 -0
  35. package/dist/utils/getPrefixName.js +6 -0
  36. package/dist/utils/projectConfig.d.ts +1 -0
  37. package/dist/utils/projectConfig.js +20 -0
  38. package/dist/utils/transformAppJson.d.ts +16 -0
  39. package/dist/utils/transformAppJson.js +199 -0
  40. package/dist/utils/validateAppJson.d.ts +4 -0
  41. package/dist/utils/validateAppJson.js +111 -0
  42. package/dist/webpack-config/index.d.ts +4 -0
  43. package/dist/webpack-config/index.js +41 -0
  44. package/dist/webpack-config/lib/const.d.ts +13 -0
  45. package/dist/webpack-config/lib/const.js +15 -0
  46. package/dist/webpack-config/lib/css.d.ts +2 -0
  47. package/dist/webpack-config/lib/css.js +41 -0
  48. package/dist/webpack-config/lib/postcssRpx2Vw.d.ts +12 -0
  49. package/dist/webpack-config/lib/postcssRpx2Vw.js +35 -0
  50. package/dist/webpack-config/lib/tagTransformMap.d.ts +35 -0
  51. package/dist/webpack-config/lib/tagTransformMap.js +38 -0
  52. package/dist/webpack-config/lib/utils.d.ts +5 -0
  53. package/dist/webpack-config/lib/utils.js +37 -0
  54. package/dist/webpack-config/loaders/component-entry-loader.d.ts +1 -0
  55. package/dist/webpack-config/loaders/component-entry-loader.js +31 -0
  56. package/dist/webpack-config/loaders/component-entry-requirements-loader.d.ts +1 -0
  57. package/dist/webpack-config/loaders/component-entry-requirements-loader.js +24 -0
  58. package/dist/webpack-config/loaders/mini-style-loader.d.ts +1 -0
  59. package/dist/webpack-config/loaders/mini-style-loader.js +69 -0
  60. package/dist/webpack-config/loaders/render-entry-loader.d.ts +1 -0
  61. package/dist/webpack-config/loaders/render-entry-loader.js +50 -0
  62. package/dist/webpack-config/loaders/service-entry-loader.d.ts +1 -0
  63. package/dist/webpack-config/loaders/service-entry-loader.js +51 -0
  64. package/dist/webpack-config/loaders/sjs-loader/index.d.ts +12 -0
  65. package/dist/webpack-config/loaders/sjs-loader/index.js +609 -0
  66. package/dist/webpack-config/loaders/sjs-loader/scope.d.ts +36 -0
  67. package/dist/webpack-config/loaders/sjs-loader/scope.js +122 -0
  68. package/dist/webpack-config/loaders/sjs-loader/tranform.d.ts +2 -0
  69. package/dist/webpack-config/loaders/sjs-loader/tranform.js +31 -0
  70. package/dist/webpack-config/loaders/utils.d.ts +13 -0
  71. package/dist/webpack-config/loaders/utils.js +41 -0
  72. package/dist/webpack-config/loaders/wxss-loader.d.ts +1 -0
  73. package/dist/webpack-config/loaders/wxss-loader.js +76 -0
  74. package/dist/webpack-config/plugins/VueLoaderPlugin.d.ts +19 -0
  75. package/dist/webpack-config/plugins/VueLoaderPlugin.js +205 -0
  76. package/dist/webpack-config/plugins/XhsMpErrorTransformPlugin.d.ts +8 -0
  77. package/dist/webpack-config/plugins/XhsMpErrorTransformPlugin.js +40 -0
  78. package/dist/webpack-config/plugins/XhsMpJsonPlugin.d.ts +36 -0
  79. package/dist/webpack-config/plugins/XhsMpJsonPlugin.js +71 -0
  80. package/dist/webpack-config/presets/babel.d.ts +8 -0
  81. package/dist/webpack-config/presets/babel.js +57 -0
  82. package/dist/webpack-config/presets/common.d.ts +4 -0
  83. package/dist/webpack-config/presets/common.js +40 -0
  84. package/dist/webpack-config/presets/mp/MpEntryPlugin.d.ts +23 -0
  85. package/dist/webpack-config/presets/mp/MpEntryPlugin.js +87 -0
  86. package/dist/webpack-config/presets/mp/index.d.ts +4 -0
  87. package/dist/webpack-config/presets/mp/index.js +48 -0
  88. package/dist/webpack-config/presets/prod.d.ts +4 -0
  89. package/dist/webpack-config/presets/prod.js +83 -0
  90. package/dist/webpack-config/presets/render/ComponentEntryPlugin.d.ts +18 -0
  91. package/dist/webpack-config/presets/render/ComponentEntryPlugin.js +77 -0
  92. package/dist/webpack-config/presets/render/RenderChunkPlugin.d.ts +5 -0
  93. package/dist/webpack-config/presets/render/RenderChunkPlugin.js +50 -0
  94. package/dist/webpack-config/presets/render/RenderEntryPlugin.d.ts +14 -0
  95. package/dist/webpack-config/presets/render/RenderEntryPlugin.js +42 -0
  96. package/dist/webpack-config/presets/render/SjsEntryPlugin.d.ts +31 -0
  97. package/dist/webpack-config/presets/render/SjsEntryPlugin.js +160 -0
  98. package/dist/webpack-config/presets/render/index.d.ts +4 -0
  99. package/dist/webpack-config/presets/render/index.js +62 -0
  100. package/dist/webpack-config/presets/service/ServiceEntryPlugin.d.ts +8 -0
  101. package/dist/webpack-config/presets/service/ServiceEntryPlugin.js +36 -0
  102. package/dist/webpack-config/presets/service/XhsMpServiceChunkPlugin.d.ts +6 -0
  103. package/dist/webpack-config/presets/service/XhsMpServiceChunkPlugin.js +46 -0
  104. package/dist/webpack-config/presets/service/XhsResolveLimitPlugin.d.ts +8 -0
  105. package/dist/webpack-config/presets/service/XhsResolveLimitPlugin.js +65 -0
  106. package/dist/webpack-config/presets/service/XhsV2AssetsPlugin.d.ts +5 -0
  107. package/dist/webpack-config/presets/service/XhsV2AssetsPlugin.js +27 -0
  108. package/dist/webpack-config/presets/service/index.d.ts +4 -0
  109. package/dist/webpack-config/presets/service/index.js +39 -0
  110. package/package.json +2 -2
@@ -0,0 +1,71 @@
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 tapable_1 = require("tapable");
8
+ const transformAppJson_1 = __importDefault(require("../../utils/transformAppJson"));
9
+ const normalizePath = path_1.default.normalize;
10
+ const hooks = {
11
+ collected: new tapable_1.SyncHook(['xhsData']),
12
+ };
13
+ const getHooks = () => hooks;
14
+ /**
15
+ * 为 compiler 添加 xhsData 数据
16
+ */
17
+ class XhsMpJsonPlugin {
18
+ constructor(options) {
19
+ const { mpDir, miniprogramDir, extJsonPath } = options;
20
+ this.options = options;
21
+ const { legacyAppJSON, appJSON } = (0, transformAppJson_1.default)(mpDir, miniprogramDir, extJsonPath);
22
+ this.appJSON = appJSON;
23
+ this.legacyAppJSON = legacyAppJSON;
24
+ this.shouldUpdateAppJson = 'first';
25
+ this.jsonDependencies = new Set([normalizePath(`${miniprogramDir}/app.json`)]);
26
+ if (extJsonPath) {
27
+ this.jsonDependencies.add(normalizePath(`${extJsonPath}/ext.json`));
28
+ }
29
+ }
30
+ apply(compiler) {
31
+ const hooks = getHooks();
32
+ const { mpDir, miniprogramDir, extJsonPath } = this.options;
33
+ compiler.xhsData = {
34
+ mpDir,
35
+ miniprogramDir,
36
+ };
37
+ this.updateAppJson(compiler);
38
+ // 仅在首次、或 json 文件变更时更新 app.json
39
+ compiler.hooks.watchRun.tap('XhsMpJsonPlugin', comp => {
40
+ this.shouldUpdateAppJson =
41
+ this.shouldUpdateAppJson === 'first' ||
42
+ Array.from(comp.modifiedFiles || []).some(files => files.endsWith('.json'));
43
+ });
44
+ hooks.collected.call({ xhsData: compiler.xhsData });
45
+ compiler.hooks.compilation.tap('XhsMpJsonPlugin', compilation => {
46
+ const { legacyAppJSON, appJSON } = (0, transformAppJson_1.default)(mpDir, miniprogramDir, extJsonPath, compilation);
47
+ this.appJSON = appJSON;
48
+ this.legacyAppJSON = legacyAppJSON;
49
+ this.updateAppJson(compiler);
50
+ hooks.collected.call({ xhsData: compiler.xhsData, compilation });
51
+ });
52
+ }
53
+ /**
54
+ * 1. 收集所有的 json 依赖
55
+ * 2. 为各个 loader 更新 appJson
56
+ */
57
+ updateAppJson(compiler) {
58
+ const { appJSON, legacyAppJSON } = this;
59
+ for (const { path } of legacyAppJSON.pages) {
60
+ this.jsonDependencies.add(normalizePath(`${this.options.miniprogramDir}/${path}.json`));
61
+ }
62
+ for (const path of Object.keys(legacyAppJSON.componentsMap)) {
63
+ this.jsonDependencies.add(normalizePath(`${this.options.miniprogramDir}/${path}.json`));
64
+ }
65
+ compiler.xhsData.jsonDependencies = Array.from(this.jsonDependencies);
66
+ compiler.xhsData.appJSON = appJSON;
67
+ compiler.xhsData.legacyAppJSON = legacyAppJSON;
68
+ }
69
+ }
70
+ XhsMpJsonPlugin.getHooks = getHooks;
71
+ exports.default = XhsMpJsonPlugin;
@@ -0,0 +1,8 @@
1
+ import Config from 'webpack-chain';
2
+ import { WebpackPresetOptions } from '../../types';
3
+ /**
4
+ * babel 仅关注语法转换,polyfill 在 js-core 中已经内置
5
+ * 目前 polyfill 仅支持到 'ios_saf >= 10', 'chrome >= 83'。语法构建中,会默认支持。
6
+ */
7
+ declare const getBaseConfig: (chain: Config, options: WebpackPresetOptions) => void;
8
+ export default getBaseConfig;
@@ -0,0 +1,57 @@
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 const_1 = __importDefault(require("../lib/const"));
7
+ const types_1 = require("../../types");
8
+ /**
9
+ * babel 仅关注语法转换,polyfill 在 js-core 中已经内置
10
+ * 目前 polyfill 仅支持到 'ios_saf >= 10', 'chrome >= 83'。语法构建中,会默认支持。
11
+ */
12
+ const getBaseConfig = (chain, options) => {
13
+ const swcTargets = options.env === types_1.ENV.development ? const_1.default.DEV_TARGETS : const_1.default.PROD_TARGETS;
14
+ chain.module
15
+ .rule('js')
16
+ .test(/\.js$/)
17
+ .exclude.add(/node_modules/)
18
+ .end()
19
+ .use('js-loader')
20
+ .loader(require.resolve('swc-loader'))
21
+ .options({
22
+ jsc: { externalHelpers: true },
23
+ env: {
24
+ targets: swcTargets,
25
+ coreJS: 3,
26
+ }
27
+ });
28
+ chain.module
29
+ .rule('ts')
30
+ .test(/\.ts$/)
31
+ .exclude.add(/node_modules/)
32
+ .end()
33
+ .use('ts-loader')
34
+ .loader(require.resolve('swc-loader'))
35
+ .options({
36
+ jsc: {
37
+ externalHelpers: true,
38
+ parser: { syntax: 'typescript' }
39
+ },
40
+ env: {
41
+ targets: swcTargets,
42
+ coreJS: 3
43
+ }
44
+ });
45
+ if (options.tsConfigPath) {
46
+ chain.resolve.extensions.add('.ts');
47
+ chain
48
+ .plugin('ts-checker')
49
+ .use(require.resolve('fork-ts-checker-webpack-plugin'), [{
50
+ async: true,
51
+ typescript: {
52
+ configFile: options.tsConfigPath,
53
+ },
54
+ }]);
55
+ }
56
+ };
57
+ exports.default = getBaseConfig;
@@ -0,0 +1,4 @@
1
+ import Config from 'webpack-chain';
2
+ import { WebpackPresetOptions } from '../../types';
3
+ declare const presetCommon: (chain: Config, options: WebpackPresetOptions) => void;
4
+ export default presetCommon;
@@ -0,0 +1,40 @@
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
+ chain.merge({
10
+ stats: 'errors-only',
11
+ output: {
12
+ pathinfo: false,
13
+ },
14
+ resolve: {
15
+ roots: [options.miniprogramDir],
16
+ extensions: ['.js'],
17
+ modules: ['node_modules', path_1.default.join(__dirname, '../../../node_modules/')],
18
+ },
19
+ resolveLoader: {
20
+ modules: [path_1.default.join(__dirname, '../loaders')],
21
+ extensions: ['.js', '.ts'],
22
+ },
23
+ });
24
+ chain.watch(!!options.watch);
25
+ chain.mode('development');
26
+ chain.devtool(options.enableSourcemap ? 'source-map' : false);
27
+ // @ts-ignore
28
+ chain.target(['web', 'es5']);
29
+ chain.cache(options.env === types_1.ENV.production
30
+ ? false
31
+ : {
32
+ type: 'filesystem',
33
+ cacheDirectory: options.cacheDirectory || path_1.default.join(__dirname, '../../../node_modules/.cache'),
34
+ version: options.miniprogramDir,
35
+ buildDependencies: {
36
+ config: [path_1.default.join(options.miniprogramDir, 'app.json')],
37
+ },
38
+ });
39
+ };
40
+ exports.default = presetCommon;
@@ -0,0 +1,23 @@
1
+ import { Compiler } from 'webpack';
2
+ import { RawSource } from 'webpack-sources';
3
+ interface MpEntryPluginOptions {
4
+ enableV1?: boolean;
5
+ enableV2?: boolean;
6
+ enableVDom?: boolean;
7
+ miniprogramDir?: string;
8
+ }
9
+ /**
10
+ * 用来添加 app.json、app.css
11
+ */
12
+ declare class MpEntryPlugin {
13
+ enableV1?: boolean;
14
+ enableV2?: boolean;
15
+ enableVDom?: boolean;
16
+ miniprogramDir?: string;
17
+ entriesCache: Map<string, boolean>;
18
+ constructor(options: MpEntryPluginOptions);
19
+ apply(compiler: Compiler): void;
20
+ getAppJson(appJSON: any): Record<string, RawSource>;
21
+ makeEntry(compiler: Compiler, name: string, url: string): void;
22
+ }
23
+ export default MpEntryPlugin;
@@ -0,0 +1,87 @@
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 fs_extra_1 = __importDefault(require("fs-extra"));
7
+ const lodash_1 = require("lodash");
8
+ const webpack_1 = require("webpack");
9
+ const webpack_sources_1 = require("webpack-sources");
10
+ const getCssSuffixName_1 = __importDefault(require("../../../utils/getCssSuffixName"));
11
+ /**
12
+ * 用来添加 app.json、app.css
13
+ */
14
+ class MpEntryPlugin {
15
+ constructor(options) {
16
+ const { enableV1, enableV2, enableVDom, miniprogramDir } = options;
17
+ this.enableV1 = enableV1;
18
+ this.enableV2 = enableV2;
19
+ this.enableVDom = enableVDom;
20
+ this.miniprogramDir = miniprogramDir;
21
+ this.entriesCache = new Map();
22
+ }
23
+ apply(compiler) {
24
+ // app.css
25
+ if (fs_extra_1.default.existsSync(`${this.miniprogramDir}/app.${(0, getCssSuffixName_1.default)()}`)) {
26
+ this.makeEntry(compiler, "app_css", `${this.miniprogramDir}/app.${(0, getCssSuffixName_1.default)()}`);
27
+ }
28
+ // 在资源优化阶段处理app.json
29
+ compiler.hooks.compilation.tap(MpEntryPlugin.name, compilation => {
30
+ // @ts-ignore
31
+ compilation.hooks.processAssets.tap({
32
+ name: MpEntryPlugin.name,
33
+ stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_COMPATIBILITY, // see below for more stages
34
+ }, (assets) => {
35
+ // @ts-ignore
36
+ const { appJSON, legacyAppJSON } = compiler.xhsData;
37
+ if (assets) {
38
+ Object.assign(assets, this.getAppJson((0, lodash_1.cloneDeep)(appJSON)));
39
+ // 兼容服务端,始终创建 v0/app.json
40
+ assets['v0/app.json'] = new webpack_sources_1.RawSource(JSON.stringify(legacyAppJSON));
41
+ }
42
+ });
43
+ });
44
+ }
45
+ getAppJson(appJSON) {
46
+ Reflect.deleteProperty(appJSON, 'lazyCodeLoading');
47
+ const assets = {};
48
+ if (this.enableVDom) {
49
+ appJSON.xhsVersionStrategy.enableVDom = true;
50
+ appJSON.usingFramework = '2.0';
51
+ }
52
+ if (this.enableV1) {
53
+ const appJsonSource = new webpack_sources_1.RawSource(JSON.stringify(appJSON));
54
+ assets['v1/app.json'] = appJsonSource;
55
+ for (const subpackage of appJSON.subPackages) {
56
+ if (subpackage.independent) {
57
+ assets[`v1/${subpackage.root}/app.json`] = appJsonSource;
58
+ }
59
+ }
60
+ }
61
+ if (this.enableV2) {
62
+ // v2包 按需加载特殊处理
63
+ let appJsonSourceV2 = (0, lodash_1.cloneDeep)(appJSON);
64
+ appJsonSourceV2.xhsVersionStrategy.lazyCodeLoading = true;
65
+ appJsonSourceV2.lazyCodeLoading = 'requiredComponents';
66
+ appJsonSourceV2 = new webpack_sources_1.RawSource(JSON.stringify(appJsonSourceV2));
67
+ assets['v2/app.json'] = appJsonSourceV2;
68
+ // 独立分包的 app.json
69
+ for (const subpackage of appJSON.subPackages) {
70
+ if (subpackage.independent) {
71
+ assets[`v2/${subpackage.root}/app.json`] = appJsonSourceV2;
72
+ }
73
+ }
74
+ }
75
+ return assets;
76
+ }
77
+ makeEntry(compiler, name, url) {
78
+ if (this.entriesCache.get(name))
79
+ return;
80
+ const entryOption = webpack_1.EntryOptionPlugin.entryDescriptionToOptions(compiler, name, {
81
+ import: [url],
82
+ });
83
+ new webpack_1.EntryPlugin(compiler.context, url, entryOption).apply(compiler);
84
+ this.entriesCache.set(name, true);
85
+ }
86
+ }
87
+ exports.default = MpEntryPlugin;
@@ -0,0 +1,4 @@
1
+ import Config from 'webpack-chain';
2
+ import { WebpackPresetOptions } from '../../../types';
3
+ declare const presetMp: (chain: Config, options: WebpackPresetOptions) => void;
4
+ export default presetMp;
@@ -0,0 +1,48 @@
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 copy_webpack_plugin_1 = __importDefault(require("copy-webpack-plugin"));
7
+ const XhsMpJsonPlugin_1 = __importDefault(require("../../plugins/XhsMpJsonPlugin"));
8
+ const MpEntryPlugin_1 = __importDefault(require("./MpEntryPlugin"));
9
+ const css_1 = __importDefault(require("../../lib/css"));
10
+ const getCssSuffixName_1 = __importDefault(require("../../../utils/getCssSuffixName"));
11
+ const ASSETS_EXTS = 'png,jpg,jpeg,gif,svg,webp,cer,mp3,aac,m4a,mp4,wav,ogg,silk,wasm,br,plist';
12
+ const presetMp = (chain, options) => {
13
+ const { extJsonPath, enableV1, enableV2, enableVDom, mpDir, miniprogramDir } = options;
14
+ const cssReg = new RegExp(`.${(0, getCssSuffixName_1.default)()}$`);
15
+ chain.merge({
16
+ output: {
17
+ path: options.distDir,
18
+ },
19
+ module: {
20
+ rule: {
21
+ [(0, getCssSuffixName_1.default)()]: {
22
+ test: cssReg,
23
+ use: Object.assign({ 'mini-style-loader': {
24
+ loader: require.resolve('../../loaders/mini-style-loader'),
25
+ options: {
26
+ enableV1,
27
+ enableV2,
28
+ miniprogramDir,
29
+ independent: true,
30
+ }
31
+ } }, (0, css_1.default)(options, ['postcss-loader', 'wxss-loader'])),
32
+ },
33
+ },
34
+ },
35
+ });
36
+ const copyPatterns = [enableV1 && 'v1', enableV2 && 'v2'].filter(Boolean).map(i => ({
37
+ noErrorOnMissing: true,
38
+ from: `**/*.{${ASSETS_EXTS}}`,
39
+ to: i,
40
+ context: miniprogramDir,
41
+ globOptions: { gitignore: true },
42
+ }));
43
+ chain.plugin('CopyPlugin').use(copy_webpack_plugin_1.default, [{ patterns: copyPatterns }]);
44
+ // app.json app.css
45
+ chain.plugin('MpEntryPlugin').use(MpEntryPlugin_1.default, [{ enableV1, enableV2, enableVDom, miniprogramDir }]);
46
+ chain.plugin('XhsMpJsonPlugin').use(XhsMpJsonPlugin_1.default, [{ mpDir, miniprogramDir, extJsonPath }]);
47
+ };
48
+ exports.default = presetMp;
@@ -0,0 +1,4 @@
1
+ import Config from 'webpack-chain';
2
+ import { CliConfig } from '../../types';
3
+ declare const getProdConfig: (chain: Config, options: CliConfig) => void;
4
+ export default getProdConfig;
@@ -0,0 +1,83 @@
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 const_1 = __importDefault(require("../lib/const"));
7
+ // import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer'
8
+ const getProdConfig = (chain, options) => {
9
+ chain.mode('production');
10
+ chain.devtool(false);
11
+ if (!options.noJs) {
12
+ // 生产环境使用 babel-loader
13
+ chain.module
14
+ .rule('js')
15
+ .use('js-loader')
16
+ .loader(require.resolve('babel-loader'))
17
+ .options({
18
+ configFile: false,
19
+ babelrc: false,
20
+ sourceType: 'unambiguous',
21
+ presets: [
22
+ [
23
+ require.resolve('@babel/preset-env'),
24
+ {
25
+ useBuiltIns: false,
26
+ corejs: {
27
+ version: 3,
28
+ proposals: true,
29
+ },
30
+ shippedProposals: true,
31
+ targets: const_1.default.PROD_BROWSERSLIST,
32
+ },
33
+ ],
34
+ ],
35
+ plugins: [[require.resolve('@babel/plugin-transform-runtime'), { corejs: false }]],
36
+ });
37
+ chain.module
38
+ .rule('ts')
39
+ .use('ts-loader')
40
+ .loader(require.resolve('babel-loader'))
41
+ .options({
42
+ sourceType: 'unambiguous',
43
+ presets: [
44
+ [require.resolve('@babel/preset-typescript'), {
45
+ allExtensions: true,
46
+ isTSX: true,
47
+ }],
48
+ [
49
+ require.resolve('@babel/preset-env'),
50
+ {
51
+ useBuiltIns: false,
52
+ corejs: {
53
+ version: 3,
54
+ proposals: true,
55
+ },
56
+ shippedProposals: true,
57
+ targets: const_1.default.PROD_BROWSERSLIST,
58
+ },
59
+ ],
60
+ ],
61
+ plugins: [[require.resolve('@babel/plugin-transform-runtime'), { corejs: false }]],
62
+ });
63
+ chain.optimization.minimizer('js').use(require.resolve('terser-webpack-plugin'), [
64
+ {
65
+ terserOptions: {
66
+ safari10: true,
67
+ compress: { drop_console: false },
68
+ },
69
+ },
70
+ ]);
71
+ }
72
+ chain.optimization.minimize(!!options.compressJs);
73
+ // mini-style-loader 中已压缩
74
+ // chain.optimization.minimizer('css').use(require.resolve('css-minimizer-webpack-plugin'), [
75
+ // {
76
+ // minimizerOptions: {
77
+ // preset: ['default', { normalizeUrl: false }],
78
+ // },
79
+ // },
80
+ // ])
81
+ // chain.plugin('abc').use(BundleAnalyzerPlugin)
82
+ };
83
+ exports.default = getProdConfig;
@@ -0,0 +1,18 @@
1
+ import { Compiler } from 'webpack';
2
+ interface IRenderEntryPluginOptions {
3
+ enableV1?: boolean;
4
+ enableV2?: boolean;
5
+ }
6
+ declare class RenderEntryPlugin {
7
+ enableV1?: boolean;
8
+ enableV2?: boolean;
9
+ entriesCache: Map<string, boolean>;
10
+ subPackageRoots: string[];
11
+ constructor(options: IRenderEntryPluginOptions);
12
+ isFileInMainPkg(filePath: any): boolean;
13
+ isFileInSubpackage(filePath: any): boolean;
14
+ apply(compiler: Compiler): void;
15
+ makeGlobalEntry(compiler: Compiler, filename: string, url: string): void;
16
+ makeUmdEntry(compiler: Compiler, filename: string, url: string, libraryName: string): void;
17
+ }
18
+ export default RenderEntryPlugin;
@@ -0,0 +1,77 @@
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 webpack_1 = require("webpack");
7
+ const XhsMpJsonPlugin_1 = __importDefault(require("../../plugins/XhsMpJsonPlugin"));
8
+ class RenderEntryPlugin {
9
+ constructor(options) {
10
+ const { enableV1, enableV2 } = options;
11
+ this.enableV1 = enableV1;
12
+ this.enableV2 = enableV2;
13
+ this.entriesCache = new Map();
14
+ this.subPackageRoots = [];
15
+ }
16
+ isFileInMainPkg(filePath) {
17
+ return this.subPackageRoots.every(root => !filePath.startsWith(`${root}/`));
18
+ }
19
+ isFileInSubpackage(filePath) {
20
+ return this.subPackageRoots.some(root => filePath.startsWith(`${root}/`));
21
+ }
22
+ apply(compiler) {
23
+ new webpack_1.library.EnableLibraryPlugin('global').apply(compiler);
24
+ const hooks = XhsMpJsonPlugin_1.default.getHooks();
25
+ hooks.collected.tap('RenderEntryPlugin', ({ xhsData }) => {
26
+ const { appJSON } = xhsData;
27
+ this.subPackageRoots = appJSON.subPackages.map(pkg => pkg.root);
28
+ if (this.enableV2) {
29
+ new webpack_1.library.EnableLibraryPlugin('global').apply(compiler);
30
+ Object.keys(appJSON.componentsMap).forEach(compPath => {
31
+ if (this.isFileInMainPkg(compPath)) {
32
+ this.makeGlobalEntry(compiler, `v2/${compPath}.js`, `component-entry-requirements-loader?compPath=${compPath}&pkg=main!`);
33
+ }
34
+ if (this.isFileInSubpackage(compPath)) {
35
+ const root = this.subPackageRoots.find(root => compPath.startsWith(`${root}/`));
36
+ this.makeGlobalEntry(compiler, `v2/${root}/${compPath}.js`, `component-entry-requirements-loader?compPath=${compPath}&pkg=sub&root=${root}!`);
37
+ }
38
+ });
39
+ }
40
+ /**
41
+ * 继续构建 v1 的包,向前兼容
42
+ */
43
+ if (this.enableV1) {
44
+ new webpack_1.library.EnableLibraryPlugin('umd').apply(compiler);
45
+ // components
46
+ this.makeUmdEntry(compiler, 'v1/components.js', 'component-entry-loader?pkg=main!', 'XHS_COMPONENTS');
47
+ for (const subPackage of appJSON.subPackages) {
48
+ const { root } = subPackage;
49
+ // subPackages 的 components
50
+ this.makeUmdEntry(compiler, `v1/${root}/components.js`, `component-entry-loader?pkg=sub&root=${root}!`, `XHS_COMPONENTS_${root}`);
51
+ }
52
+ }
53
+ });
54
+ }
55
+ makeGlobalEntry(compiler, filename, url) {
56
+ if (this.entriesCache.get(filename))
57
+ return;
58
+ const entryOption = webpack_1.EntryOptionPlugin.entryDescriptionToOptions(compiler, filename, {
59
+ import: [url],
60
+ filename,
61
+ });
62
+ new webpack_1.EntryPlugin(compiler.context, url, entryOption).apply(compiler);
63
+ this.entriesCache.set(filename, true);
64
+ }
65
+ makeUmdEntry(compiler, filename, url, libraryName) {
66
+ if (this.entriesCache.get(filename))
67
+ return;
68
+ const entryOption = webpack_1.EntryOptionPlugin.entryDescriptionToOptions(compiler, filename, {
69
+ import: [url],
70
+ filename,
71
+ library: { name: libraryName, type: 'umd' },
72
+ });
73
+ new webpack_1.EntryPlugin(compiler.context, url, entryOption).apply(compiler);
74
+ this.entriesCache.set(filename, true);
75
+ }
76
+ }
77
+ exports.default = RenderEntryPlugin;
@@ -0,0 +1,5 @@
1
+ import { Compiler } from 'webpack';
2
+ declare class RenderChunkPlugin {
3
+ apply(compiler: Compiler): void;
4
+ }
5
+ export default RenderChunkPlugin;
@@ -0,0 +1,50 @@
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 injectCSS = `
6
+ globalThis.__XHS_IS_WEBVIEW = (globalThis.localStorage && document && document.getElementsByTagName)
7
+ globalThis.__XHS_APPEND_CSS = function(cssText){
8
+ if (!globalThis.__XHS_IS_WEBVIEW) return;
9
+ var headTags = document.getElementsByTagName('head')
10
+ var head = document.head || (headTags && headTags[0])
11
+ var cssTextNode = document.createTextNode(String(cssText))
12
+ var styleNode = document.createElement('style')
13
+ styleNode.appendChild(cssTextNode)
14
+ head.appendChild(styleNode)
15
+ };
16
+ `;
17
+ // 注入css跟webpack runtime
18
+ class RenderChunkPlugin {
19
+ apply(compiler) {
20
+ compiler.hooks.compilation.tap(RenderChunkPlugin.name, compilation => {
21
+ compilation.hooks.processAssets.tap({
22
+ name: RenderChunkPlugin.name,
23
+ stage: webpack_1.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
24
+ }, (assets) => {
25
+ var _a, _b;
26
+ // 注入css逻辑
27
+ const injectCode = injectCSS;
28
+ // @ts-ignore
29
+ const appJSON = compiler.xhsData.appJSON;
30
+ const pathMap = [
31
+ (_a = appJSON.pages) === null || _a === void 0 ? void 0 : _a.map(({ path }) => path),
32
+ (_b = appJSON.subPackages) === null || _b === void 0 ? void 0 : _b.map(sub => { var _a; return (_a = sub.pages) === null || _a === void 0 ? void 0 : _a.map(({ path }) => path); })
33
+ ].flat(4).filter(Boolean)
34
+ .reduce((ac, path) => {
35
+ ac[`v1/${path}.render.js`] = true;
36
+ ac[`v2/${path}.render.js`] = true;
37
+ return ac;
38
+ }, {});
39
+ // 将runtime 塞到render.js中去
40
+ Object.keys(assets).forEach(key => {
41
+ if (!pathMap[key])
42
+ return;
43
+ // @ts-ignore
44
+ compilation.updateAsset(key, new webpack_sources_1.ConcatSource(injectCode, assets[key]));
45
+ });
46
+ });
47
+ });
48
+ }
49
+ }
50
+ exports.default = RenderChunkPlugin;
@@ -0,0 +1,14 @@
1
+ import { Compiler } from 'webpack';
2
+ interface IRenderEntryPluginOptions {
3
+ enableV1?: boolean;
4
+ enableV2?: boolean;
5
+ }
6
+ declare class RenderEntryPlugin {
7
+ enableV1?: boolean;
8
+ enableV2?: boolean;
9
+ entriesCache: Map<string, boolean>;
10
+ constructor(options: IRenderEntryPluginOptions);
11
+ apply(compiler: Compiler): void;
12
+ makeEntry(compiler: Compiler, name: string, url: string): void;
13
+ }
14
+ export default RenderEntryPlugin;
@@ -0,0 +1,42 @@
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 webpack_1 = require("webpack");
7
+ const XhsMpJsonPlugin_1 = __importDefault(require("../../plugins/XhsMpJsonPlugin"));
8
+ class RenderEntryPlugin {
9
+ constructor(options) {
10
+ const { enableV1, enableV2 } = options;
11
+ this.enableV1 = enableV1;
12
+ this.enableV2 = enableV2;
13
+ this.entriesCache = new Map();
14
+ }
15
+ apply(compiler) {
16
+ const hooks = XhsMpJsonPlugin_1.default.getHooks();
17
+ hooks.collected.tap('RenderEntryPlugin', ({ xhsData }) => {
18
+ const { appJSON } = xhsData;
19
+ new webpack_1.library.EnableLibraryPlugin('umd').apply(compiler);
20
+ for (const { path } of appJSON.pages) {
21
+ this.enableV1 && this.makeEntry(compiler, `v1/${path}`, `render-entry-loader?mode=v1&path=${path}!`);
22
+ this.enableV2 && this.makeEntry(compiler, `v2/${path}`, `render-entry-loader?mode=v2&path=${path}!`);
23
+ }
24
+ for (const subPackage of appJSON.subPackages) {
25
+ const { pages } = subPackage;
26
+ for (const { path } of pages) {
27
+ // subPackages 的 pages
28
+ this.enableV1 && this.makeEntry(compiler, `v1/${path}`, `render-entry-loader?mode=v1&path=${path}!`);
29
+ this.enableV2 && this.makeEntry(compiler, `v2/${path}`, `render-entry-loader?mode=v2&path=${path}!`);
30
+ }
31
+ }
32
+ });
33
+ }
34
+ makeEntry(compiler, name, url) {
35
+ if (this.entriesCache.get(name))
36
+ return;
37
+ const entryOption = webpack_1.EntryOptionPlugin.entryDescriptionToOptions(compiler, name, { import: [url] });
38
+ new webpack_1.EntryPlugin(compiler.context, url, entryOption).apply(compiler);
39
+ this.entriesCache.set(name, true);
40
+ }
41
+ }
42
+ exports.default = RenderEntryPlugin;