xhs-mp-compiler-cli 2.0.0-beta.3 → 2.0.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 (94) hide show
  1. package/dist/bin/xhs-mp-cli-build.js +48 -34
  2. package/dist/bin/xhs-mp-cli-dev.js +44 -44
  3. package/dist/bin/xhs-mp-cli.js +22 -12
  4. package/dist/compiler.d.ts +21 -28
  5. package/dist/compiler.js +327 -327
  6. package/dist/compilerImpl.d.ts +19 -0
  7. package/dist/compilerImpl.js +173 -0
  8. package/dist/config/constant.config.d.ts +24 -0
  9. package/dist/config/constant.config.js +82 -30
  10. package/dist/config/dir.config.js +23 -13
  11. package/dist/index.d.ts +9 -9
  12. package/dist/index.js +454 -408
  13. package/dist/packs/index.d.ts +4 -4
  14. package/dist/packs/index.js +38 -30
  15. package/dist/packs/mp-pack/index.js +38 -30
  16. package/dist/packs/webpack/dev-server/index.js +61 -50
  17. package/dist/packs/webpack/dev-server/lib/ensurePort.js +33 -23
  18. package/dist/packs/webpack/dev-server/lib/openBrowser.js +28 -18
  19. package/dist/packs/webpack/index.d.ts +1 -1
  20. package/dist/packs/webpack/index.js +99 -90
  21. package/dist/packs/webpack/webpack.d.ts +2 -1
  22. package/dist/packs/webpack/webpack.js +81 -63
  23. package/dist/presets/babel.d.ts +0 -1
  24. package/dist/presets/babel.js +64 -58
  25. package/dist/presets/common.js +42 -45
  26. package/dist/presets/configs/minigame/assets/assetsEntryPlugin.js +50 -40
  27. package/dist/presets/configs/minigame/assets/index.js +62 -52
  28. package/dist/presets/configs/minigame/service/index.js +49 -30
  29. package/dist/presets/configs/minigame/service/resolveLimitPlugin.js +33 -23
  30. package/dist/presets/configs/minigame/service/serviceChunkPlugin.d.ts +2 -2
  31. package/dist/presets/configs/minigame/service/serviceChunkPlugin.js +69 -60
  32. package/dist/presets/configs/minigame/service/serviceEntryPlugin.d.ts +3 -3
  33. package/dist/presets/configs/minigame/service/serviceEntryPlugin.js +39 -29
  34. package/dist/presets/configs/miniprogram/assets/assetsEntryPlugin.js +120 -113
  35. package/dist/presets/configs/miniprogram/assets/index.js +45 -43
  36. package/dist/presets/configs/miniprogram/render/component.js +51 -41
  37. package/dist/presets/configs/miniprogram/render/index.d.ts +2 -3
  38. package/dist/presets/configs/miniprogram/render/index.js +85 -68
  39. package/dist/presets/configs/miniprogram/render/render.js +53 -43
  40. package/dist/presets/configs/miniprogram/render/renderChunkPlugin.js +84 -49
  41. package/dist/presets/configs/miniprogram/render/sjsEntryPlugin.d.ts +15 -0
  42. package/dist/presets/configs/miniprogram/render/sjsEntryPlugin.js +164 -126
  43. package/dist/presets/configs/miniprogram/service/index.js +82 -65
  44. package/dist/presets/configs/miniprogram/service/resolveLimitPlugin.js +60 -50
  45. package/dist/presets/configs/miniprogram/service/serviceChunkPlugin.js +93 -55
  46. package/dist/presets/index.js +100 -90
  47. package/dist/presets/loaders/mg-entry-loader.js +32 -22
  48. package/dist/presets/loaders/mini-style-loader.js +79 -55
  49. package/dist/presets/loaders/mp-entry-loader.js +149 -140
  50. package/dist/presets/loaders/wxss-loader.js +73 -63
  51. package/dist/presets/plugins/InjectorPlugin.js +23 -13
  52. package/dist/presets/plugins/SourceMapDevToolPlugin.d.ts +1 -0
  53. package/dist/presets/plugins/SourceMapDevToolPlugin.js +23 -0
  54. package/dist/presets/prod.js +88 -78
  55. package/dist/sharedFs.d.ts +2 -0
  56. package/dist/sharedFs.js +24 -0
  57. package/dist/types/index.d.ts +11 -37
  58. package/dist/types/index.js +23 -13
  59. package/dist/utils/{apm/index.d.ts → apm.d.ts} +5 -3
  60. package/dist/utils/apm.js +245 -0
  61. package/dist/utils/cache.d.ts +28 -0
  62. package/dist/utils/cache.js +49 -0
  63. package/dist/utils/common.js +42 -32
  64. package/dist/utils/css.d.ts +1 -1
  65. package/dist/utils/css.js +43 -35
  66. package/dist/utils/depenedHelper.js +95 -85
  67. package/dist/utils/file.js +39 -29
  68. package/dist/utils/loader-utils.js +63 -53
  69. package/dist/utils/postcssRpx2Vw.js +43 -33
  70. package/dist/utils/project.d.ts +3 -2
  71. package/dist/utils/project.js +129 -119
  72. package/dist/utils/projectConfig.js +24 -15
  73. package/dist/utils/utils.js +51 -42
  74. package/dist/utils/workerPool.d.ts +29 -0
  75. package/dist/utils/workerPool.js +125 -0
  76. package/dist/utils/zip.js +239 -229
  77. package/package.json +33 -14
  78. package/dist/compilerCP.d.ts +0 -1
  79. package/dist/compilerCP.js +0 -175
  80. package/dist/presets/loaders/sjs-loader/index.d.ts +0 -12
  81. package/dist/presets/loaders/sjs-loader/index.js +0 -605
  82. package/dist/presets/loaders/sjs-loader/scope.d.ts +0 -36
  83. package/dist/presets/loaders/sjs-loader/scope.js +0 -122
  84. package/dist/presets/loaders/sjs-loader/tranform.d.ts +0 -2
  85. package/dist/presets/loaders/sjs-loader/tranform.js +0 -31
  86. package/dist/utils/apm/index.js +0 -198
  87. package/dist/utils/asyncWebpack.d.ts +0 -3
  88. package/dist/utils/asyncWebpack.js +0 -38
  89. package/dist/utils/const.d.ts +0 -27
  90. package/dist/utils/const.js +0 -47
  91. package/dist/utils/getSuffixName.d.ts +0 -3
  92. package/dist/utils/getSuffixName.js +0 -18
  93. package/dist/utils/tagTransformMap.d.ts +0 -36
  94. package/dist/utils/tagTransformMap.js +0 -39
@@ -1,95 +1,105 @@
1
- "use strict";
2
1
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
2
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
3
  };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.createConfigs = void 0;
7
- const xhs_mp_pack_1 = require("xhs-mp-pack");
8
- const path_1 = __importDefault(require("path"));
9
- const common_1 = __importDefault(require("./common"));
10
- const babel_1 = __importDefault(require("./babel"));
11
- const prod_1 = __importDefault(require("./prod"));
12
- const assets_1 = require("./configs/miniprogram/assets");
13
- const service_1 = require("./configs/miniprogram/service");
14
- const render_1 = require("./configs/miniprogram/render");
15
- const assets_2 = require("./configs/minigame/assets");
16
- const service_2 = require("./configs/minigame/service");
17
- /**
18
- * 小程序编译配置
19
- */
20
- const createMPConfigs = (options) => {
21
- const { packSetting } = options;
22
- const { env } = packSetting;
23
- const assetsChain = new xhs_mp_pack_1.Config();
24
- const serviceChain = new xhs_mp_pack_1.Config();
25
- const renderChain = new xhs_mp_pack_1.Config();
26
- const assetsPresets = [assets_1.presetAssets, common_1.default];
27
- const servicePresets = [service_1.presetService, common_1.default, babel_1.default];
28
- const renderPresets = [render_1.presetRender, common_1.default, babel_1.default];
29
- if (env === xhs_mp_pack_1.ENV.production) {
30
- assetsPresets.push(prod_1.default);
31
- servicePresets.push(prod_1.default);
32
- renderPresets.push(prod_1.default);
4
+ (function (factory) {
5
+ if (typeof module === "object" && typeof module.exports === "object") {
6
+ var v = factory(require, exports);
7
+ if (v !== undefined) module.exports = v;
33
8
  }
34
- assetsPresets.forEach(preset => preset(assetsChain, Object.assign(Object.assign({}, options), { otherSetting: { noJs: true } })));
35
- servicePresets.forEach(preset => preset(serviceChain, options));
36
- renderPresets.forEach(preset => preset(renderChain, options));
37
- const assetsConfig = assetsChain.toConfig();
38
- const serviceConfig = serviceChain.toConfig();
39
- const renderConfig = renderChain.toConfig();
40
- assetsConfig.entry = (0, assets_1.getAssetsEntry)(options);
41
- serviceConfig.entry = (0, service_1.getServiceEntry)(options);
42
- renderConfig.entry = (0, render_1.getRenderEntry)(options);
43
- let configs = [renderConfig, serviceConfig, assetsConfig];
44
- configs = configs.map(options => {
45
- var _a, _b, _c, _d;
46
- // 查找ide根目录下的node_modules
47
- (_b = (_a = options.resolve) === null || _a === void 0 ? void 0 : _a.modules) === null || _b === void 0 ? void 0 : _b.push(path_1.default.resolve(__dirname, '../../../../../'));
48
- // cli的node_modules路径
49
- (_d = (_c = options.resolve) === null || _c === void 0 ? void 0 : _c.modules) === null || _d === void 0 ? void 0 : _d.push(path_1.default.resolve(__dirname, '../../../../'));
50
- return options;
51
- });
52
- return configs;
53
- };
54
- /**
55
- * 小游戏编译配置
56
- */
57
- const createMGConfigs = (options) => {
58
- const { packSetting } = options;
59
- const { env } = packSetting;
60
- const assetsChain = new xhs_mp_pack_1.Config();
61
- const serviceChain = new xhs_mp_pack_1.Config();
62
- const assetsPresets = [assets_2.presetAssets, common_1.default];
63
- const servicePresets = [service_2.presetService, common_1.default, babel_1.default];
64
- if (env === xhs_mp_pack_1.ENV.production) {
65
- assetsPresets.push(prod_1.default);
66
- servicePresets.push(prod_1.default);
67
- }
68
- assetsPresets.forEach(preset => preset(assetsChain, Object.assign(Object.assign({}, options), { otherSetting: { noJs: true } })));
69
- servicePresets.forEach(preset => preset(serviceChain, options));
70
- const assetsConfig = assetsChain.toConfig();
71
- const serviceConfig = serviceChain.toConfig();
72
- assetsConfig.entry = {};
73
- serviceConfig.entry = {};
74
- let configs = [serviceConfig, assetsConfig];
75
- configs = configs.map(options => {
76
- var _a, _b, _c, _d;
77
- // 查找ide根目录下的node_modules
78
- (_b = (_a = options.resolve) === null || _a === void 0 ? void 0 : _a.modules) === null || _b === void 0 ? void 0 : _b.push(path_1.default.resolve(__dirname, '../../../../../'));
79
- // cli的node_modules路径
80
- (_d = (_c = options.resolve) === null || _c === void 0 ? void 0 : _c.modules) === null || _d === void 0 ? void 0 : _d.push(path_1.default.resolve(__dirname, '../../../../'));
81
- return options;
82
- });
83
- return configs;
84
- };
85
- // 获取编译配置
86
- const createConfigs = (options) => {
87
- const { project } = options;
88
- if (project.appMode === 'miniprogram') {
89
- return createMPConfigs(options);
9
+ else if (typeof define === "function" && define.amd) {
10
+ define(["require", "exports", "xhs-mp-pack", "path", "./common", "./babel", "./prod", "./configs/miniprogram/assets", "./configs/miniprogram/service", "./configs/miniprogram/render", "./configs/minigame/assets", "./configs/minigame/service"], factory);
90
11
  }
91
- else {
92
- return createMGConfigs(options);
93
- }
94
- };
95
- exports.createConfigs = createConfigs;
12
+ })(function (require, exports) {
13
+ "use strict";
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.createConfigs = void 0;
16
+ const xhs_mp_pack_1 = require("xhs-mp-pack");
17
+ const path_1 = __importDefault(require("path"));
18
+ const common_1 = __importDefault(require("./common"));
19
+ const babel_1 = __importDefault(require("./babel"));
20
+ const prod_1 = __importDefault(require("./prod"));
21
+ const assets_1 = require("./configs/miniprogram/assets");
22
+ const service_1 = require("./configs/miniprogram/service");
23
+ const render_1 = require("./configs/miniprogram/render");
24
+ const assets_2 = require("./configs/minigame/assets");
25
+ const service_2 = require("./configs/minigame/service");
26
+ /**
27
+ * 小程序编译配置
28
+ */
29
+ const createMPConfigs = (options) => {
30
+ const { packSetting } = options;
31
+ const { env } = packSetting;
32
+ const assetsChain = new xhs_mp_pack_1.Config();
33
+ const serviceChain = new xhs_mp_pack_1.Config();
34
+ const renderChain = new xhs_mp_pack_1.Config();
35
+ const assetsPresets = [assets_1.presetAssets, common_1.default];
36
+ const servicePresets = [service_1.presetService, common_1.default, babel_1.default];
37
+ const renderPresets = [render_1.presetRender, common_1.default, babel_1.default];
38
+ if (env === xhs_mp_pack_1.ENV.production) {
39
+ assetsPresets.push(prod_1.default);
40
+ servicePresets.push(prod_1.default);
41
+ renderPresets.push(prod_1.default);
42
+ }
43
+ assetsPresets.forEach(preset => preset(assetsChain, Object.assign(Object.assign({}, options), { otherSetting: { noJs: true } })));
44
+ servicePresets.forEach(preset => preset(serviceChain, options));
45
+ renderPresets.forEach(preset => preset(renderChain, options));
46
+ const assetsConfig = assetsChain.toConfig();
47
+ const serviceConfig = serviceChain.toConfig();
48
+ const renderConfig = renderChain.toConfig();
49
+ assetsConfig.entry = (0, assets_1.getAssetsEntry)(options);
50
+ serviceConfig.entry = (0, service_1.getServiceEntry)(options);
51
+ renderConfig.entry = (0, render_1.getRenderEntry)(options);
52
+ let configs = [renderConfig, serviceConfig, assetsConfig];
53
+ configs = configs.map(options => {
54
+ var _a, _b, _c, _d;
55
+ // 查找ide根目录下的node_modules
56
+ (_b = (_a = options.resolve) === null || _a === void 0 ? void 0 : _a.modules) === null || _b === void 0 ? void 0 : _b.push(path_1.default.resolve(__dirname, '../../../../../'));
57
+ // cli的node_modules路径
58
+ (_d = (_c = options.resolve) === null || _c === void 0 ? void 0 : _c.modules) === null || _d === void 0 ? void 0 : _d.push(path_1.default.resolve(__dirname, '../../../../'));
59
+ return options;
60
+ });
61
+ return configs;
62
+ };
63
+ /**
64
+ * 小游戏编译配置
65
+ */
66
+ const createMGConfigs = (options) => {
67
+ const { packSetting } = options;
68
+ const { env } = packSetting;
69
+ const assetsChain = new xhs_mp_pack_1.Config();
70
+ const serviceChain = new xhs_mp_pack_1.Config();
71
+ const assetsPresets = [assets_2.presetAssets, common_1.default];
72
+ const servicePresets = [service_2.presetService, common_1.default, babel_1.default];
73
+ if (env === xhs_mp_pack_1.ENV.production) {
74
+ assetsPresets.push(prod_1.default);
75
+ servicePresets.push(prod_1.default);
76
+ }
77
+ assetsPresets.forEach(preset => preset(assetsChain, Object.assign(Object.assign({}, options), { otherSetting: { noJs: true } })));
78
+ servicePresets.forEach(preset => preset(serviceChain, options));
79
+ const assetsConfig = assetsChain.toConfig();
80
+ const serviceConfig = serviceChain.toConfig();
81
+ assetsConfig.entry = {};
82
+ serviceConfig.entry = {};
83
+ let configs = [serviceConfig, assetsConfig];
84
+ configs = configs.map(options => {
85
+ var _a, _b, _c, _d;
86
+ // 查找ide根目录下的node_modules
87
+ (_b = (_a = options.resolve) === null || _a === void 0 ? void 0 : _a.modules) === null || _b === void 0 ? void 0 : _b.push(path_1.default.resolve(__dirname, '../../../../../'));
88
+ // cli的node_modules路径
89
+ (_d = (_c = options.resolve) === null || _c === void 0 ? void 0 : _c.modules) === null || _d === void 0 ? void 0 : _d.push(path_1.default.resolve(__dirname, '../../../../'));
90
+ return options;
91
+ });
92
+ return configs;
93
+ };
94
+ // 获取编译配置
95
+ const createConfigs = (options) => {
96
+ const { project } = options;
97
+ if (project.appMode === 'miniprogram') {
98
+ return createMPConfigs(options);
99
+ }
100
+ else {
101
+ return createMGConfigs(options);
102
+ }
103
+ };
104
+ exports.createConfigs = createConfigs;
105
+ });
@@ -1,27 +1,37 @@
1
- "use strict";
2
1
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
2
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
3
  };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- const fs_extra_1 = __importDefault(require("fs-extra"));
7
- const common_1 = require("../../utils/common");
8
- const xhs_mp_project_1 = require("xhs-mp-project");
9
- function entryLoader() {
10
- // @ts-ignore
11
- const { appJSON, projectPath } = this._compiler.project;
12
- const { entry } = this.getOptions();
13
- const addAppJs = () => {
14
- const absolutePath = (0, common_1.unixJoin)(projectPath, entry);
15
- if (fs_extra_1.default.existsSync(`${absolutePath}.js`)) {
16
- return `require('${absolutePath}.js')`;
17
- }
18
- if (fs_extra_1.default.existsSync(`${absolutePath}.ts`)) {
19
- return `require('${absolutePath}.ts')`;
20
- }
21
- throw new Error(`${absolutePath}.(js|ts) 不存在`);
22
- };
23
- return `
4
+ (function (factory) {
5
+ if (typeof module === "object" && typeof module.exports === "object") {
6
+ var v = factory(require, exports);
7
+ if (v !== undefined) module.exports = v;
8
+ }
9
+ else if (typeof define === "function" && define.amd) {
10
+ define(["require", "exports", "fs-extra", "../../utils/common", "xhs-mp-project"], factory);
11
+ }
12
+ })(function (require, exports) {
13
+ "use strict";
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const fs_extra_1 = __importDefault(require("fs-extra"));
16
+ const common_1 = require("../../utils/common");
17
+ const xhs_mp_project_1 = require("xhs-mp-project");
18
+ function entryLoader() {
19
+ // @ts-ignore
20
+ const { appJSON, projectPath } = this._compiler.project;
21
+ const { entry } = this.getOptions();
22
+ const addAppJs = () => {
23
+ const absolutePath = (0, common_1.unixJoin)(projectPath, entry);
24
+ if (fs_extra_1.default.existsSync(`${absolutePath}.js`)) {
25
+ return `require('${absolutePath}.js')`;
26
+ }
27
+ if (fs_extra_1.default.existsSync(`${absolutePath}.ts`)) {
28
+ return `require('${absolutePath}.ts')`;
29
+ }
30
+ throw new Error(`${absolutePath}.(js|ts) 不存在`);
31
+ };
32
+ return `
24
33
  ;globalThis['__MP_APP_JSON__'] = ${JSON.stringify(xhs_mp_project_1.utils.pureAppJSON(appJSON))};
25
34
  ${addAppJs()}`;
26
- }
27
- module.exports = entryLoader;
35
+ }
36
+ module.exports = entryLoader;
37
+ });
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
2
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
3
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -11,62 +10,87 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
10
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
11
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
12
  };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- const packs_1 = require("../../packs");
16
- const cssnano_1 = __importDefault(require("cssnano"));
17
- const common_1 = require("../../utils/common");
18
- const CSS_MINIMIZER_OPTIONS = {
19
- preset: ['default', { normalizeUrl: false, calc: false }]
20
- };
21
- const minify = (source, name) => __awaiter(void 0, void 0, void 0, function* () {
22
- var _a, _b;
23
- const postcssOptions = {
24
- to: name,
25
- from: name
26
- };
27
- try {
28
- const result = yield cssnano_1.default.process(source, postcssOptions, CSS_MINIMIZER_OPTIONS);
29
- return {
30
- code: result.css,
31
- warnings: (_b = (_a = result.warnings()) === null || _a === void 0 ? void 0 : _a.map) === null || _b === void 0 ? void 0 : _b.call(_a, i => i.toString())
32
- };
13
+ (function (factory) {
14
+ if (typeof module === "object" && typeof module.exports === "object") {
15
+ var v = factory(require, exports);
16
+ if (v !== undefined) module.exports = v;
33
17
  }
34
- catch (error) {
35
- return {
36
- code: source
37
- };
18
+ else if (typeof define === "function" && define.amd) {
19
+ define(["require", "exports", "../../packs", "cssnano", "../../utils/common"], factory);
38
20
  }
39
- });
40
- function miniStyleLoader(source) {
41
- var _a;
42
- const callback = this.async();
43
- const { resourcePath } = this;
44
- // const {miniprogramDir, enableVDom, enableV1, enableV2, independent} = this.getOptions() as any
45
- const { project, packSetting, independent } = this.getOptions();
46
- const { miniprogramDir } = project;
47
- const { enableVDom, enableV1, enableV2 } = packSetting;
48
- const shortPath = (0, common_1.getShortPath)(miniprogramDir, resourcePath).replace(/\.wxss$/, '.css');
49
- source = (_a = source === null || source === void 0 ? void 0 : source.trim) === null || _a === void 0 ? void 0 : _a.call(source);
50
- function result(code) {
51
- if (!code) {
52
- return `export default void 0;`;
21
+ })(function (require, exports) {
22
+ "use strict";
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ const packs_1 = require("../../packs");
25
+ const cssnano_1 = __importDefault(require("cssnano"));
26
+ const common_1 = require("../../utils/common");
27
+ const CSS_MINIMIZER_OPTIONS = {
28
+ preset: ['default', { normalizeUrl: false, calc: false }]
29
+ };
30
+ const minify = (source, name) => __awaiter(void 0, void 0, void 0, function* () {
31
+ var _a, _b;
32
+ const postcssOptions = {
33
+ to: name,
34
+ from: name
35
+ };
36
+ try {
37
+ const result = yield cssnano_1.default.process(source, postcssOptions, CSS_MINIMIZER_OPTIONS);
38
+ return {
39
+ code: result.css,
40
+ warnings: (_b = (_a = result.warnings()) === null || _a === void 0 ? void 0 : _a.map) === null || _b === void 0 ? void 0 : _b.call(_a, i => i.toString())
41
+ };
53
42
  }
54
- // 判断浏览器环境,注入css添加逻辑
55
- return new packs_1.RawSource(`globalThis.__XHS_IS_WEBVIEW && globalThis.__XHS_APPEND_CSS(${JSON.stringify(code)});` +
56
- `export default ${JSON.stringify(shortPath)};`).source();
57
- }
58
- // 压缩css
59
- minify(source, shortPath)
60
- .then(({ code, warnings }) => {
61
- // vdom架构下emit出一份css文件
62
- if ((independent || enableVDom) && code) {
63
- enableV1 && this.emitFile(`v1/${shortPath}`, code);
64
- enableV2 && this.emitFile(`v2/${shortPath}`, code);
43
+ catch (error) {
44
+ return {
45
+ code: source
46
+ };
65
47
  }
66
- callback(null, result(code));
67
- })
68
- .catch(error => {
69
- callback(error);
70
48
  });
71
- }
72
- module.exports = miniStyleLoader;
49
+ function miniStyleLoader(source) {
50
+ var _a;
51
+ const callback = this.async();
52
+ const { resourcePath } = this;
53
+ const { project, packSetting, independentFile } = this.getOptions();
54
+ const { miniprogramDir } = project;
55
+ const { enableVDom, enableV1, enableV2 } = packSetting;
56
+ const shortPath = (0, common_1.getShortPath)(miniprogramDir, resourcePath).replace(/\.wxss$/, '.css');
57
+ source = (_a = source === null || source === void 0 ? void 0 : source.trim) === null || _a === void 0 ? void 0 : _a.call(source);
58
+ const emitFiles = (code) => {
59
+ // vdom架构下emit出一份css文件
60
+ if ((independentFile || enableVDom) && code) {
61
+ enableV1 && this.emitFile(`v1/${shortPath}`, code);
62
+ enableV2 && this.emitFile(`v2/${shortPath}`, code);
63
+ }
64
+ };
65
+ const result = (code) => {
66
+ if (!code) {
67
+ return `export default void 0;`;
68
+ }
69
+ // 判断浏览器环境,注入css添加逻辑
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
+ }
78
+ };
79
+ if (packSetting.compressCss) {
80
+ // 压缩css
81
+ minify(source, shortPath)
82
+ .then(({ code, warnings }) => {
83
+ emitFiles(code);
84
+ callback(null, result(code));
85
+ })
86
+ .catch(error => {
87
+ callback(error);
88
+ });
89
+ }
90
+ else {
91
+ emitFiles(source);
92
+ callback(null, result(source));
93
+ }
94
+ }
95
+ module.exports = miniStyleLoader;
96
+ });