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
@@ -1,205 +0,0 @@
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
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
7
- // @ts-nocheck
8
- const querystring_1 = __importDefault(require("querystring"));
9
- const getMlSuffixName_1 = __importDefault(require("../../utils/getMlSuffixName"));
10
- const id = 'vue-loader-plugin';
11
- const NS = 'vue-loader';
12
- const NormalModule = require('webpack/lib/NormalModule');
13
- const BasicEffectRulePlugin = require('webpack/lib/rules/BasicEffectRulePlugin');
14
- const BasicMatcherRulePlugin = require('webpack/lib/rules/BasicMatcherRulePlugin');
15
- const UseEffectRulePlugin = require('webpack/lib/rules/UseEffectRulePlugin');
16
- const RuleSetCompiler = require('webpack/lib/rules/RuleSetCompiler');
17
- const ObjectMatcherRulePlugin = require('webpack/lib/rules/ObjectMatcherRulePlugin');
18
- const objectMatcherRulePlugins = [];
19
- objectMatcherRulePlugins.push(new ObjectMatcherRulePlugin('assert', 'assertions'), new ObjectMatcherRulePlugin('descriptionData'));
20
- const ruleSetCompiler = new RuleSetCompiler([
21
- new BasicMatcherRulePlugin('test', 'resource'),
22
- new BasicMatcherRulePlugin('mimetype'),
23
- new BasicMatcherRulePlugin('dependency'),
24
- new BasicMatcherRulePlugin('include', 'resource'),
25
- new BasicMatcherRulePlugin('exclude', 'resource', true),
26
- new BasicMatcherRulePlugin('conditions'),
27
- new BasicMatcherRulePlugin('resource'),
28
- new BasicMatcherRulePlugin('resourceQuery'),
29
- new BasicMatcherRulePlugin('resourceFragment'),
30
- new BasicMatcherRulePlugin('realResource'),
31
- new BasicMatcherRulePlugin('issuer'),
32
- new BasicMatcherRulePlugin('compiler'),
33
- ...objectMatcherRulePlugins,
34
- new BasicEffectRulePlugin('type'),
35
- new BasicEffectRulePlugin('sideEffects'),
36
- new BasicEffectRulePlugin('parser'),
37
- new BasicEffectRulePlugin('resolve'),
38
- new BasicEffectRulePlugin('generator'),
39
- new UseEffectRulePlugin(),
40
- ]);
41
- let uid = 0;
42
- const matcherCache = new WeakMap();
43
- function match(rule, fakeFile) {
44
- let ruleSet = matcherCache.get(rule);
45
- if (!ruleSet) {
46
- // skip the `include` check when locating the vue rule
47
- const clonedRawRule = Object.assign({}, rule);
48
- delete clonedRawRule.include;
49
- ruleSet = ruleSetCompiler.compile([clonedRawRule]);
50
- matcherCache.set(rule, ruleSet);
51
- }
52
- return ruleSet.exec({
53
- resource: fakeFile,
54
- });
55
- }
56
- function cloneRule(rawRule, refs, ruleCheck, ruleResource) {
57
- const compiledRule = ruleSetCompiler.compileRule(`clonedRuleSet-${++uid}`, rawRule, refs);
58
- // do not process rule with enforce
59
- if (!rawRule.enforce) {
60
- const ruleUse = compiledRule.effects
61
- .filter(effect => effect.type === 'use')
62
- .map((effect) => effect.value);
63
- // fix conflict with config.loader and config.options when using config.use
64
- delete rawRule.loader;
65
- delete rawRule.options;
66
- rawRule.use = ruleUse;
67
- }
68
- let currentResource;
69
- const res = Object.assign(Object.assign({}, rawRule), { resource: (resources) => {
70
- currentResource = resources;
71
- return true;
72
- }, resourceQuery: (query) => {
73
- if (!query) {
74
- return false;
75
- }
76
- const parsed = querystring_1.default.parse(query.slice(1));
77
- if (parsed.vue == null) {
78
- return false;
79
- }
80
- if (!ruleCheck(parsed, compiledRule)) {
81
- return false;
82
- }
83
- const fakeResourcePath = ruleResource(parsed, currentResource);
84
- for (const condition of compiledRule.conditions) {
85
- // add support for resourceQuery
86
- const request = condition.property === 'resourceQuery' ? query : fakeResourcePath;
87
- if (condition && !condition.fn(request)) {
88
- return false;
89
- }
90
- }
91
- return true;
92
- } });
93
- delete res.test;
94
- if (rawRule.rules) {
95
- res.rules = rawRule.rules.map(rule => cloneRule(rule, refs, ruleCheck, ruleResource));
96
- }
97
- if (rawRule.oneOf) {
98
- res.oneOf = rawRule.oneOf.map(rule => cloneRule(rule, refs, ruleCheck, ruleResource));
99
- }
100
- return res;
101
- }
102
- const langBlockRuleCheck = (query, rule) => query.type === 'custom' || !rule.conditions.length || query.lang != null;
103
- const jsRuleCheck = (query) => query.type === 'template';
104
- const jsRuleResource = (query, resource) => `${resource}.js`;
105
- const langBlockRuleResource = (query, resource) => `${resource}.${query.lang}`;
106
- class VueLoaderPlugin {
107
- apply(compiler) {
108
- // add NS marker so that the loader can detect and report missing plugin
109
- compiler.hooks.compilation.tap(id, compilation => {
110
- NormalModule.getCompilationHooks(compilation).loader.tap(id, (loaderContext) => {
111
- loaderContext[NS] = true;
112
- });
113
- });
114
- const rules = compiler.options.module.rules;
115
- let rawVueRule;
116
- let vueRules = [];
117
- for (const rawRule of rules) {
118
- // skip rules with 'enforce'. eg. rule for eslint-loader
119
- if (rawRule.enforce) {
120
- continue;
121
- }
122
- vueRules = match(rawRule, `foo.${(0, getMlSuffixName_1.default)()}`);
123
- if (!vueRules.length) {
124
- vueRules = match(rawRule, 'foo.vue.html');
125
- }
126
- if (vueRules.length > 0) {
127
- if (rawRule.oneOf) {
128
- throw new Error(`[VueLoaderPlugin Error] vue-loader currently does not support vue rules with oneOf.`);
129
- }
130
- rawVueRule = rawRule;
131
- break;
132
- }
133
- }
134
- if (!vueRules.length) {
135
- throw new Error(`[VueLoaderPlugin Error] No matching rule for .vue files found.\n` +
136
- `Make sure there is at least one root-level rule that matches .vue or .vue.html files.`);
137
- }
138
- // get the normlized "use" for vue files
139
- const vueUse = vueRules.filter(rule => rule.type === 'use').map(rule => rule.value);
140
- // get vue-loader options
141
- const vueLoaderUseIndex = vueUse.findIndex(u =>
142
- // FIXME: this code logic is incorrect when project paths starts with `vue-loader-something`
143
- /^vue-loader|(\/|\\|@)vue-loader/.test(u.loader));
144
- if (vueLoaderUseIndex < 0) {
145
- throw new Error(`[VueLoaderPlugin Error] No matching use for vue-loader is found.\n` +
146
- `Make sure the rule matching .vue files include vue-loader in its use.`);
147
- }
148
- // make sure vue-loader options has a known ident so that we can share
149
- // options by reference in the template-loader by using a ref query like
150
- // template-loader??vue-loader-options
151
- const vueLoaderUse = vueUse[vueLoaderUseIndex];
152
- const vueLoaderOptions = (vueLoaderUse.options = vueLoaderUse.options || {});
153
- // for each user rule (expect the vue rule), create a cloned rule
154
- // that targets the corresponding language blocks in *.vue files.
155
- const refs = new Map();
156
- const clonedRules = rules
157
- .filter(r => r !== rawVueRule)
158
- .map(rawRule => cloneRule(rawRule, refs, langBlockRuleCheck, langBlockRuleResource));
159
- // fix conflict with config.loader and config.options when using config.use
160
- delete rawVueRule.loader;
161
- delete rawVueRule.options;
162
- rawVueRule.use = vueUse;
163
- // rule for template compiler
164
- const templateCompilerRule = {
165
- loader: require.resolve('vue-loader/dist/templateLoader'),
166
- resourceQuery: (query) => {
167
- if (!query) {
168
- return false;
169
- }
170
- const parsed = querystring_1.default.parse(query.slice(1));
171
- return parsed.vue != null && parsed.type === 'template';
172
- },
173
- options: vueLoaderOptions,
174
- };
175
- // for each rule that matches plain .js files, also create a clone and
176
- // match it against the compiled template code inside *.vue files, so that
177
- // compiled vue render functions receive the same treatment as user code
178
- // (mostly babel)
179
- const jsRulesForRenderFn = rules
180
- .filter(r => r !== rawVueRule && match(r, 'test.js').length > 0)
181
- .map(rawRule => cloneRule(rawRule, refs, jsRuleCheck, jsRuleResource));
182
- // global pitcher (responsible for injecting template compiler loader & CSS
183
- // post loader)
184
- const pitcher = {
185
- loader: require.resolve('vue-loader/dist/pitcher'),
186
- resourceQuery: (query) => {
187
- if (!query) {
188
- return false;
189
- }
190
- const parsed = querystring_1.default.parse(query.slice(1));
191
- return parsed.vue != null;
192
- },
193
- };
194
- // replace original rules
195
- compiler.options.module.rules = [
196
- pitcher,
197
- ...jsRulesForRenderFn,
198
- templateCompilerRule,
199
- ...clonedRules,
200
- ...rules,
201
- ];
202
- }
203
- }
204
- VueLoaderPlugin.NS = NS;
205
- exports.default = VueLoaderPlugin;
@@ -1,8 +0,0 @@
1
- import { Compiler } from 'webpack';
2
- /**
3
- * 将 VueCompilerError 转换为 xhsml 错误提示
4
- */
5
- declare class XhsMpErrorTransformPlugin {
6
- apply(compiler: Compiler): void;
7
- }
8
- export default XhsMpErrorTransformPlugin;
@@ -1,40 +0,0 @@
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 getMlSuffixName_1 = __importDefault(require("../../utils/getMlSuffixName"));
7
- /**
8
- * 将 VueCompilerError 转换为 xhsml 错误提示
9
- */
10
- class XhsMpErrorTransformPlugin {
11
- apply(compiler) {
12
- compiler.hooks.afterCompile.tap('XhsMpErrorTransformPlugin', (compilation) => {
13
- let vueCompilerErrors = [];
14
- const errorFiles = new Set();
15
- compilation.errors = compilation.errors.filter(err => {
16
- if (/VueCompilerError/.test(err.message)) {
17
- vueCompilerErrors.push(err);
18
- return false;
19
- }
20
- return true;
21
- });
22
- // 同一个文件只取最后一个报错
23
- vueCompilerErrors = vueCompilerErrors.reverse().reduce((acc, err) => {
24
- if (err.module) {
25
- const { context } = err.module;
26
- if (!errorFiles.has(context)) {
27
- err.stack = undefined;
28
- err.message = err.message.replace(/VueCompilerError/g, `[ ${(0, getMlSuffixName_1.default)()} 文件编译错误 ]`).replace(/at createCompilerError .*?/g, '');
29
- err.details = undefined;
30
- acc.push(err);
31
- errorFiles.add(context);
32
- }
33
- }
34
- return acc;
35
- }, []);
36
- compilation.errors.push(...vueCompilerErrors);
37
- });
38
- }
39
- }
40
- exports.default = XhsMpErrorTransformPlugin;
@@ -1,36 +0,0 @@
1
- import { Compiler } from 'webpack';
2
- import { AppJSON } from '../../types/index';
3
- interface IXhsMpJsonPluginOptions {
4
- mpDir: string;
5
- miniprogramDir: string;
6
- extJsonPath?: string;
7
- }
8
- export interface IXhsData {
9
- mpDir?: string;
10
- miniprogramDir?: string;
11
- appJSON?: AppJSON;
12
- legacyAppJSON?: AppJSON;
13
- jsonDependencies?: string[];
14
- }
15
- export type XhsCompiler = Compiler & {
16
- xhsData: IXhsData;
17
- };
18
- /**
19
- * 为 compiler 添加 xhsData 数据
20
- */
21
- declare class XhsMpJsonPlugin {
22
- options: IXhsMpJsonPluginOptions;
23
- appJSON: AppJSON;
24
- legacyAppJSON: AppJSON;
25
- shouldUpdateAppJson: 'first' | boolean;
26
- jsonDependencies: Set<string>;
27
- static getHooks: any;
28
- constructor(options: IXhsMpJsonPluginOptions);
29
- apply(compiler: XhsCompiler): void;
30
- /**
31
- * 1. 收集所有的 json 依赖
32
- * 2. 为各个 loader 更新 appJson
33
- */
34
- updateAppJson(compiler: XhsCompiler): void;
35
- }
36
- export default XhsMpJsonPlugin;
@@ -1,71 +0,0 @@
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;
@@ -1,4 +0,0 @@
1
- import Config from 'webpack-chain';
2
- import { WebpackPresetOptions } from '../../types';
3
- declare const presetCommon: (chain: Config, options: WebpackPresetOptions) => void;
4
- export default presetCommon;
@@ -1,40 +0,0 @@
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;
@@ -1,23 +0,0 @@
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;
@@ -1,87 +0,0 @@
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;
@@ -1,4 +0,0 @@
1
- import Config from 'webpack-chain';
2
- import { WebpackPresetOptions } from '../../../types';
3
- declare const presetMp: (chain: Config, options: WebpackPresetOptions) => void;
4
- export default presetMp;
@@ -1,48 +0,0 @@
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;
@@ -1,4 +0,0 @@
1
- import Config from 'webpack-chain';
2
- import { CliConfig } from '../../types';
3
- declare const getProdConfig: (chain: Config, options: CliConfig) => void;
4
- export default getProdConfig;
@@ -1,83 +0,0 @@
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;