xhs-mp-compiler-cli 1.9.8 → 1.9.10

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.
@@ -10,17 +10,43 @@
10
10
  "use strict";
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  const packs_1 = require("../../../../packs");
13
- const injectCSS = `
14
- globalThis.__XHS_IS_WEBVIEW = (globalThis.localStorage && globalThis.document && globalThis.document.querySelectorAll)
15
- globalThis.__XHS_APPEND_CSS = function(cssText, src){
16
- if (!globalThis.__XHS_IS_WEBVIEW) return;
17
- var headTags = document.getElementsByTagName('head')
18
- var head = document.head || (headTags && headTags[0])
19
- var cssTextNode = document.createTextNode(String(cssText))
20
- var styleNode = document.createElement('style')
21
- styleNode.setAttribute('xhs:path', src)
13
+ /**
14
+ * 注入变量不可以使用 let const var 变量形式
15
+ * 因为2.0 架构下 render.js 和 component.js 运行在逻辑层(只有一个环境) 会导致变量冲突
16
+ */
17
+ const common = `
18
+ globalThis.__XHS_WEBVIEW = (globalThis.localStorage && globalThis.document && globalThis.document.querySelectorAll);
19
+ `;
20
+ const injectLink = `
21
+ ${common}
22
+ globalThis.__XHS_ADD_LINK = function(src){
23
+ if (!globalThis.__XHS_WEBVIEW) return;
24
+ const host = globalThis.SmallApp && globalThis.SmallApp.__BOOTSTRAP_HOST__
25
+ const link = document.createElement('link')
26
+ link.href = host + "/" + src
27
+ link.type = 'text/css'
28
+ link.rel = 'stylesheet'
29
+ link.setAttribute('xhs-p', src)
30
+ document.head.appendChild(link);
31
+ };
32
+ `;
33
+ /**
34
+ * 2.0模式下 render.js 和 component.js 运行在逻辑层
35
+ * 目前 2.0 各项功能已经稳定 降级可能性不大
36
+ * 2.0中原先css打包了两份 一份是 .css 文件 一份是 在render.js中注入的style标签
37
+ * 现在2.0 打包 render.js 中改为使用link标签注入,这样降级到 1.0 时仍然正常运行
38
+ * 且不会使得包体积变大
39
+ * 2.0 后面量起来后,可全部采用加载link方式打包,不必区分1.0 2.0
40
+ */
41
+ const injectStyle = `
42
+ ${common}
43
+ globalThis.__XHS_ADD_STYLE = function(style, src){
44
+ if (!globalThis.__XHS_WEBVIEW) return;
45
+ const cssTextNode = document.createTextNode(String(style))
46
+ const styleNode = document.createElement('style')
47
+ styleNode.setAttribute('xhs-p', src)
22
48
  styleNode.appendChild(cssTextNode)
23
- head.appendChild(styleNode)
49
+ document.head.appendChild(styleNode)
24
50
  };
25
51
  `;
26
52
  // 注入css跟webpack runtime
@@ -35,9 +61,9 @@ globalThis.__XHS_APPEND_CSS = function(cssText, src){
35
61
  stage: packs_1.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS
36
62
  }, assets => {
37
63
  var _a, _b;
38
- const { project } = this.options;
64
+ const { project, packSetting } = this.options;
39
65
  // 注入css逻辑
40
- const injectCode = injectCSS;
66
+ const injectCode = packSetting.enableVDom ? injectLink : injectStyle;
41
67
  const appJSON = project.appJSON;
42
68
  const pathMap = [
43
69
  (_a = appJSON.pages) === null || _a === void 0 ? void 0 : _a.map(({ path }) => path),
@@ -67,8 +67,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
67
67
  return `export default void 0;`;
68
68
  }
69
69
  // 判断浏览器环境,注入css添加逻辑
70
- return new packs_1.RawSource(`globalThis.__XHS_IS_WEBVIEW && globalThis.__XHS_APPEND_CSS(${JSON.stringify(code)}, '${shortPath}');` +
71
- `export default ${JSON.stringify(shortPath)};`).source();
70
+ if (enableVDom) {
71
+ return new packs_1.RawSource(`globalThis.__XHS_ADD_LINK(${JSON.stringify(shortPath)});` +
72
+ `export default ${JSON.stringify(shortPath)};`).source();
73
+ }
74
+ else {
75
+ return new packs_1.RawSource(`globalThis.__XHS_ADD_STYLE(${JSON.stringify(code)}, ${JSON.stringify(shortPath)});` +
76
+ `export default ${JSON.stringify(shortPath)};`).source();
77
+ }
72
78
  };
73
79
  if (packSetting.compressCss) {
74
80
  // 压缩css
@@ -21,9 +21,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
21
21
  __setModuleDefault(result, mod);
22
22
  return result;
23
23
  };
24
- var __importDefault = (this && this.__importDefault) || function (mod) {
25
- return (mod && mod.__esModule) ? mod : { "default": mod };
26
- };
27
24
  (function (factory) {
28
25
  if (typeof module === "object" && typeof module.exports === "object") {
29
26
  var v = factory(require, exports);
@@ -35,12 +32,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
35
32
  })(function (require, exports) {
36
33
  "use strict";
37
34
  Object.defineProperty(exports, "__esModule", { value: true });
38
- const fs_extra_1 = __importDefault(require("fs-extra"));
35
+ const fs_extra_1 = __importStar(require("fs-extra"));
39
36
  const path_1 = __importStar(require("path"));
40
37
  const xhs_mp_pack_1 = require("xhs-mp-pack");
41
38
  const loader_utils_1 = require("../../utils/loader-utils");
42
39
  const getSuffixName_1 = require("../../utils/getSuffixName");
43
- function genComponentLoaderScript(project, packSetting, context, opts) {
40
+ function genComponentLoaderScript(project, context, opts) {
44
41
  const { pkg, root, mode, compPath } = opts;
45
42
  const { projectMiniprogramPath, jsonDependencies = [] } = project;
46
43
  const componentsMap = project.getComponentsMap();
@@ -77,24 +74,23 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
77
74
  ;${entries.join('\n ')}
78
75
  `;
79
76
  }
80
- const genRenderLoaderScript = (project, packSetting, context, opts) => {
77
+ const genRenderLoaderScript = (project, context, opts) => {
81
78
  const { projectMiniprogramPath, appJSON, jsonDependencies } = project;
82
- const { enableVDom } = packSetting;
83
79
  const { pkg, mode } = opts;
84
80
  const isFull = pkg === 'full';
85
- const appCssPath = (0, path_1.resolve)(projectMiniprogramPath, 'app.css');
86
81
  for (const file of jsonDependencies) {
87
82
  context.addDependency(file);
88
83
  }
84
+ const appCssPath = (0, path_1.resolve)(projectMiniprogramPath, 'app.css');
85
+ const appCssExist = (0, fs_extra_1.existsSync)(appCssPath);
89
86
  const resourcePath = (0, loader_utils_1.formatPath)(context.resourcePath);
90
87
  const path = (0, loader_utils_1.formatPath)(resourcePath.replace(`${projectMiniprogramPath}/`, '').replace(`.${(0, getSuffixName_1.getMlSuffixName)()}`, ''));
91
88
  const subPackage = (appJSON.subPackages || []).find(subPack => !!subPack.pages.find(page => page.path === path));
92
89
  const enableLazyCodeLoading = mode === 'v2';
93
90
  // 资源文件
94
- const { resourceList, appCssCode } = (0, loader_utils_1.getResourceUris)({
95
- appCssPath,
91
+ const resources = (0, loader_utils_1.getResourceUris)({
92
+ appCssExist,
96
93
  subPackage,
97
- enableVDom,
98
94
  enableLazyCodeLoading
99
95
  });
100
96
  // 开启了按需注入,注入依赖组件
@@ -112,18 +108,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
112
108
  depends.push(uri);
113
109
  }
114
110
  return `
115
- ${appCssCode};
116
111
  import Component from '${resourcePath}';
117
- globalThis.__render.mount(Component, '${path}', ${JSON.stringify(resourceList)}, ${JSON.stringify(depends)})
112
+ globalThis.__render.mount(Component, '${path}', ${JSON.stringify(resources)}, ${JSON.stringify(depends)})
118
113
  `;
119
114
  }
120
115
  return `
121
- ${appCssCode}
122
116
  import Component from '${resourcePath}';
123
- globalThis.__render.mount(Component, '${path}', ${JSON.stringify(resourceList)})
117
+ globalThis.__render.mount(Component, '${path}', ${JSON.stringify(resources)})
124
118
  `;
125
119
  };
126
- function genServiceLoderScript(project, packSetting, context, opts) {
120
+ function genServiceLoderScript(project, context, opts) {
127
121
  const { appJSON, legacyAppJSON, projectMiniprogramPath, jsonDependencies } = project;
128
122
  const { pkg, root } = opts;
129
123
  const modules = [];
@@ -205,7 +199,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
205
199
  if (!handler) {
206
200
  throw new Error(`not found loader handler of ${type}`);
207
201
  }
208
- return handler(this._compiler.project, this._compiler.packSetting, this, options);
202
+ return handler(this._compiler.project, this, options);
209
203
  }
210
204
  module.exports = entryLoader;
211
205
  });
@@ -4,14 +4,10 @@ export declare const getShortPath: (projectMiniprogramPath: string, rootContext:
4
4
  * 根据是否分包获取资源路径
5
5
  */
6
6
  export declare const getResourceUris: (options: {
7
- appCssPath: string;
8
- enableVDom: boolean;
7
+ appCssExist: boolean;
9
8
  enableLazyCodeLoading: boolean;
10
9
  subPackage: any;
11
- }) => {
12
- appCssCode: string;
13
- resourceList: string[];
14
- };
10
+ }) => string[];
15
11
  /**
16
12
  * @NOTE
17
13
  * Create a request and handle the conversion of esModule modules
@@ -7,7 +7,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
7
7
  if (v !== undefined) module.exports = v;
8
8
  }
9
9
  else if (typeof define === "function" && define.amd) {
10
- define(["require", "exports", "path", "./common", "./getSuffixName", "fs-extra"], factory);
10
+ define(["require", "exports", "path", "./common", "./getSuffixName"], factory);
11
11
  }
12
12
  })(function (require, exports) {
13
13
  "use strict";
@@ -17,7 +17,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
17
17
  const path_1 = __importDefault(require("path"));
18
18
  const common_1 = require("./common");
19
19
  const getSuffixName_1 = require("./getSuffixName");
20
- const fs_extra_1 = __importDefault(require("fs-extra"));
21
20
  const formatPath = (pathStr = '') => pathStr.split(path_1.default.sep).join(path_1.default.posix.sep);
22
21
  exports.formatPath = formatPath;
23
22
  const getShortPath = (projectMiniprogramPath, rootContext) => {
@@ -35,26 +34,12 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
35
34
  */
36
35
  const getResourceUris = (options) => {
37
36
  const resourceList = [];
38
- const { appCssPath, enableVDom, enableLazyCodeLoading, subPackage } = options;
39
- const appCssExist = fs_extra_1.default.existsSync(appCssPath);
37
+ const { appCssExist, enableLazyCodeLoading, subPackage } = options;
40
38
  // sjs 无法识别是否引用 所以一律push
41
- // 加载顺序 主包sjs 分包sjs 主包components.js 分包components.js
42
- let appCssCode = '';
43
- const genAppCss = () => {
44
- if (appCssExist) {
45
- // 运行时v2架构下,还是传递给运行时控制
46
- if (enableVDom) {
47
- resourceList.push(`app.css`);
48
- }
49
- else {
50
- // 运行时v1架构下,需要放在appCssCode,避免页面样式优先级高于app.css
51
- appCssCode = `import '${(0, exports.formatPath)(appCssPath)}'`;
52
- }
53
- }
54
- };
39
+ // 加载顺序 app.css 主包sjs 分包sjs 主包components.js 分包components.js
55
40
  if (!subPackage) {
56
41
  // 主包
57
- genAppCss();
42
+ appCssExist && resourceList.push(`app.css`);
58
43
  resourceList.push('sjs.js');
59
44
  !enableLazyCodeLoading && resourceList.push(`components.js`);
60
45
  }
@@ -64,14 +49,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
64
49
  !enableLazyCodeLoading && resourceList.push(`${subPackage === null || subPackage === void 0 ? void 0 : subPackage.root}/components.js`);
65
50
  }
66
51
  else if (subPackage) {
67
- // 普通分包, 自己的components.js,主包components.js
68
- genAppCss();
52
+ // 普通分包, 自己的components.js,主包components.js app.css
53
+ appCssExist && resourceList.push(`app.css`);
69
54
  resourceList.push('sjs.js');
70
55
  resourceList.push(`${subPackage === null || subPackage === void 0 ? void 0 : subPackage.root}/sjs.js`);
71
56
  !enableLazyCodeLoading && resourceList.push(`components.js`);
72
57
  !enableLazyCodeLoading && resourceList.push(`${subPackage === null || subPackage === void 0 ? void 0 : subPackage.root}/components.js`);
73
58
  }
74
- return { appCssCode, resourceList };
59
+ return resourceList;
75
60
  };
76
61
  exports.getResourceUris = getResourceUris;
77
62
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xhs-mp-compiler-cli",
3
- "version": "1.9.8",
3
+ "version": "1.9.10",
4
4
  "description": "xhs mp command tool.",
5
5
  "preferGlobal": true,
6
6
  "category": "esm",
@@ -94,13 +94,13 @@
94
94
  "webpack-chain": "^6.5.1",
95
95
  "webpack-sources": "^3.2.2",
96
96
  "xhs-mp-workerpool": "^9.1.3",
97
- "xhs-mp-compiler-ml-loader": "3.0.8",
98
- "xhs-mp-compiler-utils": "1.9.8",
99
- "xhs-mp-pack": "1.9.8",
100
- "xhs-mp-project": "1.9.8",
101
- "xhs-mp-utils": "1.9.8",
102
- "xhs-mp-shared-fs": "1.4.8",
103
- "xhs-mp-compiler-sjs-loader": "1.9.8",
97
+ "xhs-mp-compiler-ml-loader": "3.0.10",
98
+ "xhs-mp-compiler-utils": "1.9.10",
99
+ "xhs-mp-pack": "1.9.10",
100
+ "xhs-mp-project": "1.9.10",
101
+ "xhs-mp-utils": "1.9.10",
102
+ "xhs-mp-shared-fs": "1.4.10",
103
+ "xhs-mp-compiler-sjs-loader": "1.9.10",
104
104
  "yauzl": "^2.10.0"
105
105
  },
106
106
  "devDependencies": {