@modern-js/module-tools 2.35.1 → 2.36.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 (225) hide show
  1. package/dist/build.js +43 -16
  2. package/dist/builder/build.d.ts +1 -10
  3. package/dist/builder/build.js +96 -208
  4. package/dist/builder/clear.d.ts +1 -4
  5. package/dist/builder/clear.js +32 -28
  6. package/dist/builder/copy.js +75 -60
  7. package/dist/builder/dts/index.js +27 -19
  8. package/dist/builder/dts/rollup.d.ts +6 -14
  9. package/dist/builder/dts/rollup.js +60 -37
  10. package/dist/builder/dts/tsc.d.ts +3 -2
  11. package/dist/builder/dts/tsc.js +50 -37
  12. package/dist/builder/esbuild/adapter.d.ts +3 -0
  13. package/dist/builder/esbuild/adapter.js +275 -0
  14. package/dist/builder/esbuild/hook.d.ts +4 -0
  15. package/dist/builder/esbuild/hook.js +103 -0
  16. package/dist/builder/esbuild/index.d.ts +35 -0
  17. package/dist/builder/esbuild/index.js +268 -0
  18. package/dist/builder/esbuild/resolve.d.ts +17 -0
  19. package/dist/builder/esbuild/resolve.js +125 -0
  20. package/dist/builder/esbuild/sourcemap.d.ts +16 -0
  21. package/dist/builder/esbuild/sourcemap.js +86 -0
  22. package/dist/builder/esbuild/transform.d.ts +7 -0
  23. package/dist/builder/esbuild/transform.js +45 -0
  24. package/dist/builder/esbuild/watch.d.ts +2 -0
  25. package/dist/builder/esbuild/watch.js +115 -0
  26. package/dist/builder/esbuild/write-file.d.ts +2 -0
  27. package/dist/builder/esbuild/write-file.js +89 -0
  28. package/dist/builder/feature/asset.d.ts +19 -0
  29. package/dist/builder/feature/asset.js +149 -0
  30. package/dist/builder/feature/format-cjs.d.ts +5 -0
  31. package/dist/builder/feature/format-cjs.js +53 -0
  32. package/dist/builder/feature/index.d.ts +2 -0
  33. package/dist/builder/feature/index.js +74 -0
  34. package/dist/builder/feature/json.d.ts +5 -0
  35. package/dist/builder/feature/json.js +47 -0
  36. package/dist/builder/feature/redirect.d.ts +5 -0
  37. package/dist/builder/feature/redirect.js +217 -0
  38. package/dist/builder/feature/style/index.d.ts +5 -0
  39. package/dist/builder/feature/style/index.js +94 -0
  40. package/dist/builder/feature/style/lessAliasPlugin.d.ts +12 -0
  41. package/dist/builder/feature/style/lessAliasPlugin.js +70 -0
  42. package/dist/builder/feature/style/lessRender.d.ts +2 -0
  43. package/dist/builder/feature/style/lessRender.js +64 -0
  44. package/dist/builder/feature/style/postcssTransformer.d.ts +6 -0
  45. package/dist/builder/feature/style/postcssTransformer.js +95 -0
  46. package/dist/builder/feature/style/postcssUrlPlugin.d.ts +6 -0
  47. package/dist/builder/feature/style/postcssUrlPlugin.js +58 -0
  48. package/dist/builder/feature/style/sassRender.d.ts +2 -0
  49. package/dist/builder/feature/style/sassRender.js +85 -0
  50. package/dist/builder/feature/style/transformStyle.d.ts +13 -0
  51. package/dist/builder/feature/style/transformStyle.js +101 -0
  52. package/dist/builder/feature/style/utils.d.ts +18 -0
  53. package/dist/builder/feature/style/utils.js +124 -0
  54. package/dist/builder/feature/swc.d.ts +9 -0
  55. package/dist/builder/feature/swc.js +170 -0
  56. package/dist/builder/feature/terser.d.ts +5 -0
  57. package/dist/builder/feature/terser.js +83 -0
  58. package/dist/builder/index.d.ts +1 -0
  59. package/dist/builder/index.js +59 -28
  60. package/dist/builder/platform.js +38 -23
  61. package/dist/cli.js +58 -29
  62. package/dist/command.js +62 -38
  63. package/dist/config/defineConfig.js +25 -17
  64. package/dist/config/legacySchema.js +35 -32
  65. package/dist/config/merge.d.ts +1 -0
  66. package/dist/config/merge.js +69 -36
  67. package/dist/config/normalize.d.ts +1 -0
  68. package/dist/config/normalize.js +63 -46
  69. package/dist/config/schema.d.ts +48 -0
  70. package/dist/config/schema.js +53 -34
  71. package/dist/config/transformLegacyConfig.js +67 -61
  72. package/dist/config/valid.d.ts +2 -2
  73. package/dist/config/valid.js +44 -21
  74. package/dist/constants/build.d.ts +5 -1
  75. package/dist/constants/build.js +88 -46
  76. package/dist/constants/color.js +27 -20
  77. package/dist/constants/dts.js +27 -20
  78. package/dist/constants/file.d.ts +2 -2
  79. package/dist/constants/file.js +48 -25
  80. package/dist/constants/legacy-preset.js +45 -32
  81. package/dist/constants/loader.d.ts +2 -0
  82. package/dist/constants/loader.js +53 -0
  83. package/dist/constants/log.d.ts +0 -8
  84. package/dist/constants/log.js +27 -41
  85. package/dist/constants/preset.js +55 -62
  86. package/dist/debug.d.ts +4 -0
  87. package/dist/debug.js +37 -0
  88. package/dist/dev.js +42 -25
  89. package/dist/error.js +49 -42
  90. package/dist/hooks/build.d.ts +1 -2
  91. package/dist/hooks/build.js +36 -28
  92. package/dist/hooks/dev.js +32 -17
  93. package/dist/hooks/index.d.ts +1 -1
  94. package/dist/hooks/index.js +31 -16
  95. package/dist/hooks/misc.d.ts +5 -2
  96. package/dist/hooks/misc.js +31 -12
  97. package/dist/index.d.ts +2 -1
  98. package/dist/index.js +39 -32
  99. package/dist/locale/en.js +32 -17
  100. package/dist/locale/index.d.ts +2 -1
  101. package/dist/locale/index.js +31 -24
  102. package/dist/locale/zh.js +32 -17
  103. package/dist/plugins.js +28 -13
  104. package/dist/types/color.js +15 -5
  105. package/dist/types/command.d.ts +3 -3
  106. package/dist/types/command.js +15 -5
  107. package/dist/types/config/copy.js +15 -5
  108. package/dist/types/config/dev.js +15 -5
  109. package/dist/types/config/index.d.ts +87 -52
  110. package/dist/types/config/index.js +24 -6
  111. package/dist/types/config/style.d.ts +58 -6
  112. package/dist/types/config/style.js +15 -5
  113. package/dist/types/context.js +15 -5
  114. package/dist/types/dts.d.ts +14 -3
  115. package/dist/types/dts.js +15 -5
  116. package/dist/types/esbuild.d.ts +107 -0
  117. package/dist/types/esbuild.js +32 -0
  118. package/dist/types/hooks.js +15 -5
  119. package/dist/types/index.d.ts +1 -0
  120. package/dist/types/index.js +32 -11
  121. package/dist/types/legacyConfig/index.js +15 -5
  122. package/dist/types/legacyConfig/output.d.ts +4 -4
  123. package/dist/types/legacyConfig/output.js +15 -5
  124. package/dist/types/legacyConfig/source.js +15 -5
  125. package/dist/types/legacyConfig/tools.js +15 -5
  126. package/dist/utils/assert.d.ts +5 -0
  127. package/dist/utils/assert.js +50 -0
  128. package/dist/utils/builder.d.ts +10 -14
  129. package/dist/utils/builder.js +59 -59
  130. package/dist/utils/common.d.ts +1 -1
  131. package/dist/utils/common.js +25 -17
  132. package/dist/utils/dts.d.ts +26 -9
  133. package/dist/utils/dts.js +98 -64
  134. package/dist/utils/hash.d.ts +2 -0
  135. package/dist/utils/hash.js +31 -0
  136. package/dist/utils/index.d.ts +11 -0
  137. package/dist/utils/index.js +42 -0
  138. package/dist/utils/input.js +47 -24
  139. package/dist/utils/log.d.ts +1 -2
  140. package/dist/utils/log.js +36 -20
  141. package/dist/utils/map.d.ts +13 -0
  142. package/dist/utils/map.js +58 -0
  143. package/dist/utils/onExit.js +35 -11
  144. package/dist/utils/print.d.ts +2 -2
  145. package/dist/utils/print.js +52 -49
  146. package/dist/utils/style.d.ts +5 -23
  147. package/dist/utils/style.js +57 -37
  148. package/dist/utils/tspath.js +114 -0
  149. package/package.json +64 -50
  150. package/vitest.config.ts +20 -0
  151. package/dist/build.js.map +0 -1
  152. package/dist/builder/build.js.map +0 -1
  153. package/dist/builder/clear.js.map +0 -1
  154. package/dist/builder/copy.js.map +0 -1
  155. package/dist/builder/dts/index.js.map +0 -1
  156. package/dist/builder/dts/rollup.js.map +0 -1
  157. package/dist/builder/dts/tsc.js.map +0 -1
  158. package/dist/builder/index.js.map +0 -1
  159. package/dist/builder/platform.js.map +0 -1
  160. package/dist/cli.js.map +0 -1
  161. package/dist/command.js.map +0 -1
  162. package/dist/config/defineConfig.js.map +0 -1
  163. package/dist/config/legacySchema.js.map +0 -1
  164. package/dist/config/merge.js.map +0 -1
  165. package/dist/config/normalize.js.map +0 -1
  166. package/dist/config/schema.js.map +0 -1
  167. package/dist/config/transformLegacyConfig.js.map +0 -1
  168. package/dist/config/valid.js.map +0 -1
  169. package/dist/constants/build.js.map +0 -1
  170. package/dist/constants/color.js.map +0 -1
  171. package/dist/constants/dts.js.map +0 -1
  172. package/dist/constants/file.js.map +0 -1
  173. package/dist/constants/legacy-preset.js.map +0 -1
  174. package/dist/constants/log.js.map +0 -1
  175. package/dist/constants/preset.js.map +0 -1
  176. package/dist/dev.js.map +0 -1
  177. package/dist/error.js.map +0 -1
  178. package/dist/hooks/build.js.map +0 -1
  179. package/dist/hooks/dev.js.map +0 -1
  180. package/dist/hooks/index.js.map +0 -1
  181. package/dist/hooks/misc.js.map +0 -1
  182. package/dist/index.js.map +0 -1
  183. package/dist/locale/en.js.map +0 -1
  184. package/dist/locale/index.js.map +0 -1
  185. package/dist/locale/zh.js.map +0 -1
  186. package/dist/plugins.js.map +0 -1
  187. package/dist/types/color.js.map +0 -1
  188. package/dist/types/command.js.map +0 -1
  189. package/dist/types/config/copy.js.map +0 -1
  190. package/dist/types/config/dev.js.map +0 -1
  191. package/dist/types/config/index.js.map +0 -1
  192. package/dist/types/config/style.js.map +0 -1
  193. package/dist/types/context.js.map +0 -1
  194. package/dist/types/dts.js.map +0 -1
  195. package/dist/types/hooks.js.map +0 -1
  196. package/dist/types/index.js.map +0 -1
  197. package/dist/types/legacyConfig/index.js.map +0 -1
  198. package/dist/types/legacyConfig/output.js.map +0 -1
  199. package/dist/types/legacyConfig/source.js.map +0 -1
  200. package/dist/types/legacyConfig/tools.js.map +0 -1
  201. package/dist/types/utils.d.ts +0 -5
  202. package/dist/types/utils.js +0 -4
  203. package/dist/utils/builder.js.map +0 -1
  204. package/dist/utils/common.js.map +0 -1
  205. package/dist/utils/context.d.ts +0 -7
  206. package/dist/utils/context.js +0 -23
  207. package/dist/utils/context.js.map +0 -1
  208. package/dist/utils/dts.js.map +0 -1
  209. package/dist/utils/input.js.map +0 -1
  210. package/dist/utils/language.d.ts +0 -1
  211. package/dist/utils/language.js +0 -22
  212. package/dist/utils/language.js.map +0 -1
  213. package/dist/utils/libuild-plugin.d.ts +0 -3
  214. package/dist/utils/libuild-plugin.js +0 -31
  215. package/dist/utils/libuild-plugin.js.map +0 -1
  216. package/dist/utils/log.js.map +0 -1
  217. package/dist/utils/onExit.js.map +0 -1
  218. package/dist/utils/path.d.ts +0 -1
  219. package/dist/utils/path.js +0 -19
  220. package/dist/utils/path.js.map +0 -1
  221. package/dist/utils/print.js.map +0 -1
  222. package/dist/utils/style.js.map +0 -1
  223. package/dist/utils/tspathsTransform.js +0 -91
  224. package/dist/utils/tspathsTransform.js.map +0 -1
  225. /package/dist/utils/{tspathsTransform.d.ts → tspath.d.ts} +0 -0
@@ -0,0 +1,5 @@
1
+ import { ICompiler } from '../../types';
2
+ export declare const formatCjs: {
3
+ name: string;
4
+ apply: (compiler: ICompiler) => void;
5
+ };
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name2 in all)
8
+ __defProp(target, name2, { get: all[name2], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var format_cjs_exports = {};
20
+ __export(format_cjs_exports, {
21
+ formatCjs: () => formatCjs
22
+ });
23
+ module.exports = __toCommonJS(format_cjs_exports);
24
+ var import_sucrase = require("sucrase");
25
+ const name = "format-cjs";
26
+ const apply = (compiler) => {
27
+ compiler.hooks.renderChunk.tapPromise({
28
+ name
29
+ }, async (chunk) => {
30
+ if (chunk.fileName.endsWith(".js") && chunk.type === "chunk") {
31
+ const code = chunk.contents.toString();
32
+ const result = (0, import_sucrase.transform)(code, {
33
+ transforms: [
34
+ "imports"
35
+ ]
36
+ });
37
+ return {
38
+ ...chunk,
39
+ contents: result.code,
40
+ map: result.sourceMap
41
+ };
42
+ }
43
+ return chunk;
44
+ });
45
+ };
46
+ const formatCjs = {
47
+ name,
48
+ apply
49
+ };
50
+ // Annotate the CommonJS export names for ESM import in node:
51
+ 0 && (module.exports = {
52
+ formatCjs
53
+ });
@@ -0,0 +1,2 @@
1
+ import { HookList, Context } from '../../types';
2
+ export declare function getInternalList(context: Context): Promise<HookList>;
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var feature_exports = {};
30
+ __export(feature_exports, {
31
+ getInternalList: () => getInternalList
32
+ });
33
+ module.exports = __toCommonJS(feature_exports);
34
+ var import_dts = require("../../utils/dts");
35
+ var import_format_cjs = require("./format-cjs");
36
+ var import_style = require("./style");
37
+ var import_terser = require("./terser");
38
+ var import_asset = require("./asset");
39
+ async function getInternalList(context) {
40
+ var _userTsconfig_compilerOptions;
41
+ const internal = [];
42
+ const { config } = context;
43
+ internal.push(import_asset.asset, import_style.css);
44
+ if (config.buildType === "bundle" && config.format === "cjs" && config.splitting) {
45
+ internal.push(import_format_cjs.formatCjs);
46
+ }
47
+ if (config.buildType === "bundleless") {
48
+ const { redirect } = await Promise.resolve().then(() => __toESM(require("./redirect")));
49
+ const { json } = await Promise.resolve().then(() => __toESM(require("./json")));
50
+ internal.push(redirect, json);
51
+ }
52
+ if (config.minify && config.minify !== "esbuild") {
53
+ internal.push(import_terser.minify);
54
+ }
55
+ const userTsconfig = await (0, import_dts.getProjectTsconfig)(context.config.tsconfig);
56
+ var _userTsconfig_compilerOptions_emitDecoratorMetadata;
57
+ const emitDecoratorMetadata = (_userTsconfig_compilerOptions_emitDecoratorMetadata = userTsconfig === null || userTsconfig === void 0 ? void 0 : (_userTsconfig_compilerOptions = userTsconfig.compilerOptions) === null || _userTsconfig_compilerOptions === void 0 ? void 0 : _userTsconfig_compilerOptions.emitDecoratorMetadata) !== null && _userTsconfig_compilerOptions_emitDecoratorMetadata !== void 0 ? _userTsconfig_compilerOptions_emitDecoratorMetadata : false;
58
+ const { transformImport, transformLodash, externalHelpers, format, target } = context.config;
59
+ const enbaleSwcTransform = transformImport.length > 0 || transformLodash || externalHelpers || emitDecoratorMetadata;
60
+ const enableSwcRenderChunk = enbaleSwcTransform ? format === "umd" : format === "umd" || target === "es5";
61
+ if (enbaleSwcTransform) {
62
+ const { swcTransform } = await Promise.resolve().then(() => __toESM(require("./swc")));
63
+ internal.push(swcTransform(userTsconfig));
64
+ }
65
+ if (enableSwcRenderChunk) {
66
+ const { swcRenderChunk } = await Promise.resolve().then(() => __toESM(require("./swc")));
67
+ internal.push(swcRenderChunk);
68
+ }
69
+ return internal;
70
+ }
71
+ // Annotate the CommonJS export names for ESM import in node:
72
+ 0 && (module.exports = {
73
+ getInternalList
74
+ });
@@ -0,0 +1,5 @@
1
+ import { ICompiler } from '../../types';
2
+ export declare const json: {
3
+ name: string;
4
+ apply(compiler: ICompiler): void;
5
+ };
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name2 in all)
8
+ __defProp(target, name2, { get: all[name2], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var json_exports = {};
20
+ __export(json_exports, {
21
+ json: () => json
22
+ });
23
+ module.exports = __toCommonJS(json_exports);
24
+ var import_fs = require("fs");
25
+ const isJsonExt = (path) => {
26
+ return path.endsWith(".json");
27
+ };
28
+ const name = "json";
29
+ const json = {
30
+ name,
31
+ apply(compiler) {
32
+ compiler.hooks.load.tapPromise({
33
+ name
34
+ }, async (args) => {
35
+ if (isJsonExt(args.path)) {
36
+ return {
37
+ contents: (0, import_fs.readFileSync)(args.path),
38
+ loader: "copy"
39
+ };
40
+ }
41
+ });
42
+ }
43
+ };
44
+ // Annotate the CommonJS export names for ESM import in node:
45
+ 0 && (module.exports = {
46
+ json
47
+ });
@@ -0,0 +1,5 @@
1
+ import { ICompiler } from '../../types';
2
+ export declare const redirect: {
3
+ name: string;
4
+ apply(compiler: ICompiler): void;
5
+ };
@@ -0,0 +1,217 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name2 in all)
10
+ __defProp(target, name2, { get: all[name2], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var redirect_exports = {};
30
+ __export(redirect_exports, {
31
+ redirect: () => redirect
32
+ });
33
+ module.exports = __toCommonJS(redirect_exports);
34
+ var import_path = require("path");
35
+ var import_napi = require("@ast-grep/napi");
36
+ var import_magic_string = __toESM(require("magic-string"));
37
+ var import_tsconfig_paths = require("@modern-js/utils/tsconfig-paths");
38
+ var import_utils = require("@modern-js/utils");
39
+ var import_file = require("../../constants/file");
40
+ var import_utils2 = require("../../utils");
41
+ var import_asset = require("./asset");
42
+ var import_postcssTransformer = require("./style/postcssTransformer");
43
+ async function redirectImport(compiler, code, modules, aliasRecord, filePath, outputDir, matchPath) {
44
+ const str = new import_magic_string.default(code);
45
+ const extensions = [
46
+ ".ts",
47
+ ".tsx",
48
+ ".js",
49
+ ".jsx"
50
+ ];
51
+ await Promise.all(modules.map(async (module2) => {
52
+ if (!module2.name) {
53
+ return;
54
+ }
55
+ const { start, end } = module2;
56
+ let { name: name2 } = module2;
57
+ const { redirect: redirect2, asset } = compiler.config;
58
+ const { alias, style } = redirect2;
59
+ if (alias) {
60
+ let absoluteImportPath = matchPath ? matchPath(name2, void 0, void 0, extensions) : void 0;
61
+ for (const alias2 of Object.keys(aliasRecord)) {
62
+ if (name2.startsWith(`${alias2}/`)) {
63
+ absoluteImportPath = (0, import_path.join)(aliasRecord[alias2], name2.slice(alias2.length + 1));
64
+ break;
65
+ }
66
+ if (name2 === alias2) {
67
+ absoluteImportPath = aliasRecord[alias2];
68
+ break;
69
+ }
70
+ }
71
+ if (absoluteImportPath) {
72
+ const relativePath = (0, import_path.relative)((0, import_path.dirname)(filePath), absoluteImportPath);
73
+ const relativeImportPath = (0, import_utils2.normalizeSlashes)(relativePath.startsWith("..") ? relativePath : `./${relativePath}`);
74
+ str.overwrite(start, end, relativeImportPath);
75
+ name2 = relativeImportPath;
76
+ }
77
+ }
78
+ if (style) {
79
+ const ext = (0, import_path.extname)(name2);
80
+ const { originalFilePath, query } = (0, import_utils2.resolvePathAndQuery)(name2);
81
+ if (query.css_virtual) {
82
+ const replacedName = (0, import_path.basename)(originalFilePath, (0, import_path.extname)(originalFilePath)).replace(".", "_");
83
+ const base = `${replacedName}.css`;
84
+ const key = query.hash;
85
+ const contents = compiler.virtualModule.get(key);
86
+ const fileName = (0, import_path.join)(outputDir, base);
87
+ compiler.emitAsset(fileName, {
88
+ type: "asset",
89
+ contents,
90
+ fileName,
91
+ originalFileName: name2
92
+ });
93
+ const relativeImportPath = (0, import_utils2.normalizeSlashes)(`./${base}`);
94
+ str.overwrite(start, end, relativeImportPath);
95
+ }
96
+ if (!name2.startsWith(".")) {
97
+ return;
98
+ }
99
+ if (ext === ".less" || ext === ".sass" || ext === ".scss" || ext === ".css") {
100
+ var _compiler_config_style_autoModules;
101
+ if ((0, import_postcssTransformer.isCssModule)(name2, (_compiler_config_style_autoModules = compiler.config.style.autoModules) !== null && _compiler_config_style_autoModules !== void 0 ? _compiler_config_style_autoModules : true)) {
102
+ str.overwrite(start, end, `${name2.slice(0, -ext.length)}`);
103
+ } else {
104
+ str.overwrite(start, end, `${name2.slice(0, -ext.length)}.css`);
105
+ }
106
+ return;
107
+ }
108
+ }
109
+ if (redirect2.asset) {
110
+ if (import_file.assetExt.filter((ext) => name2.endsWith(ext)).length) {
111
+ const absPath = (0, import_path.resolve)((0, import_path.dirname)(filePath), name2);
112
+ const svgrResult = await (0, import_asset.loadSvgr)(absPath, asset.svgr);
113
+ if (svgrResult) {
114
+ const ext = (0, import_path.extname)(name2);
115
+ const outputName = `${name2.slice(0, -ext.length)}.js`;
116
+ str.overwrite(start, end, outputName);
117
+ } else {
118
+ const { contents: relativeImportPath } = await import_asset.getAssetContents.apply(compiler, [
119
+ absPath,
120
+ outputDir
121
+ ]);
122
+ str.overwrite(start, end, `${relativeImportPath}`);
123
+ }
124
+ }
125
+ }
126
+ }));
127
+ return str;
128
+ }
129
+ const name = "redirect";
130
+ const redirect = {
131
+ name,
132
+ apply(compiler) {
133
+ let matchPath;
134
+ if (import_utils.fs.existsSync(compiler.config.tsconfig)) {
135
+ const result = (0, import_tsconfig_paths.loadConfig)(compiler.config.tsconfig);
136
+ if (result.resultType === "success") {
137
+ const { absoluteBaseUrl, paths, mainFields, addMatchAll } = result;
138
+ matchPath = (0, import_tsconfig_paths.createMatchPath)(absoluteBaseUrl, paths, mainFields, addMatchAll);
139
+ }
140
+ }
141
+ compiler.hooks.transform.tapPromise({
142
+ name
143
+ }, async (args) => {
144
+ if (!(0, import_utils2.isJsExt)(args.path) && !(0, import_utils2.isJsLoader)(args.loader)) {
145
+ return args;
146
+ }
147
+ const { code, path: id } = args;
148
+ const { format, alias, sourceDir, outDir } = compiler.config;
149
+ if (!code || format === "iife" || format === "umd") {
150
+ return args;
151
+ }
152
+ const absoluteAlias = Object.entries(alias).reduce((result, [name2, target]) => {
153
+ if (!(0, import_path.isAbsolute)(target)) {
154
+ result[name2] = (0, import_path.resolve)(compiler.context.root, target);
155
+ } else {
156
+ result[name2] = target;
157
+ }
158
+ return result;
159
+ }, {});
160
+ let matchModule = [];
161
+ try {
162
+ const sgNode = import_napi.js.parse(code).root();
163
+ const funcPattern = [
164
+ `require($MATCH)`,
165
+ `import($MATCH)`
166
+ ];
167
+ const staticPattern = [
168
+ `import $$VAR from '$MATCH'`,
169
+ `import $$VAR from "$MATCH"`,
170
+ `import '$MATCH'`,
171
+ `import "$MATCH"`
172
+ ];
173
+ const funcMatchModule = funcPattern.map((p) => {
174
+ return sgNode.findAll(p);
175
+ }).flat().map((node) => {
176
+ const matchNode = node.getMatch("MATCH");
177
+ return {
178
+ name: matchNode.text().slice(1, -1),
179
+ start: matchNode.range().start.index + 1,
180
+ end: matchNode.range().end.index - 1
181
+ };
182
+ });
183
+ const staticMatchModule = staticPattern.map((p) => sgNode.findAll(p)).flat().map((node) => {
184
+ const matchNode = node.getMatch("MATCH");
185
+ return {
186
+ name: matchNode.text(),
187
+ start: matchNode.range().start.index,
188
+ end: matchNode.range().end.index
189
+ };
190
+ });
191
+ matchModule = [
192
+ ...funcMatchModule,
193
+ ...staticMatchModule
194
+ ];
195
+ } catch (e) {
196
+ import_utils.logger.error("[parse error]", e);
197
+ }
198
+ if (!matchModule.length) {
199
+ return args;
200
+ }
201
+ const outputPath = (0, import_path.resolve)(outDir, (0, import_path.relative)(sourceDir, id));
202
+ const str = await redirectImport(compiler, code, matchModule, absoluteAlias, id, (0, import_path.dirname)(outputPath), matchPath);
203
+ return {
204
+ ...args,
205
+ code: str.toString(),
206
+ map: str.generateMap({
207
+ hires: true,
208
+ includeContent: true
209
+ })
210
+ };
211
+ });
212
+ }
213
+ };
214
+ // Annotate the CommonJS export names for ESM import in node:
215
+ 0 && (module.exports = {
216
+ redirect
217
+ });
@@ -0,0 +1,5 @@
1
+ import { ICompiler } from '../../../types';
2
+ export declare const css: {
3
+ name: string;
4
+ apply(compiler: ICompiler): void;
5
+ };
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name2 in all)
10
+ __defProp(target, name2, { get: all[name2], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var style_exports = {};
30
+ __export(style_exports, {
31
+ css: () => css
32
+ });
33
+ module.exports = __toCommonJS(style_exports);
34
+ var import_fs = require("fs");
35
+ var import_safe_identifier = require("safe-identifier");
36
+ var import_utils = require("../../../utils");
37
+ var import_transformStyle = require("./transformStyle");
38
+ const name = "css";
39
+ const css = {
40
+ name,
41
+ apply(compiler) {
42
+ compiler.hooks.load.tapPromise({
43
+ name
44
+ }, async (args) => {
45
+ if ((0, import_utils.isStyleExt)(args.path)) {
46
+ const { query } = (0, import_utils.resolvePathAndQuery)(args.path);
47
+ if (query === null || query === void 0 ? void 0 : query.css_virtual) {
48
+ const key = query.hash;
49
+ const contents = compiler.virtualModule.get(key);
50
+ return {
51
+ contents,
52
+ loader: "css"
53
+ };
54
+ }
55
+ return {
56
+ contents: (0, import_fs.readFileSync)(args.path),
57
+ loader: "css"
58
+ };
59
+ }
60
+ });
61
+ compiler.hooks.transform.tapPromise({
62
+ name
63
+ }, async (source) => {
64
+ if ((0, import_utils.isStyleExt)(source.path)) {
65
+ let { code, loader = "css" } = source;
66
+ const { query } = (0, import_utils.resolvePathAndQuery)(source.path);
67
+ if (!(query === null || query === void 0 ? void 0 : query.css_virtual)) {
68
+ ({ code, loader } = await import_transformStyle.transformStyle.apply(compiler, [
69
+ source
70
+ ]));
71
+ }
72
+ const { style, buildType } = compiler.config;
73
+ if (style.inject && buildType === "bundle" && loader === "css") {
74
+ const styleInjectPath = require.resolve("style-inject/dist/style-inject.es").replace(/[\\/]+/g, "/");
75
+ const cssVariableName = (0, import_safe_identifier.identifier)("css", true);
76
+ code = `var ${cssVariableName} = ${JSON.stringify(code)};
77
+ import styleInject from '${styleInjectPath}';
78
+ styleInject(${cssVariableName});`;
79
+ loader = "js";
80
+ }
81
+ return {
82
+ ...source,
83
+ code,
84
+ loader
85
+ };
86
+ }
87
+ return source;
88
+ });
89
+ }
90
+ };
91
+ // Annotate the CommonJS export names for ESM import in node:
92
+ 0 && (module.exports = {
93
+ css
94
+ });
@@ -0,0 +1,12 @@
1
+ import { ICompiler } from '../../../types';
2
+ interface Options {
3
+ compiler: ICompiler;
4
+ stdinDir: string;
5
+ }
6
+ export default class LessAliasesPlugin {
7
+ compiler: ICompiler;
8
+ stdinDir: string;
9
+ constructor(options: Options);
10
+ install(less: any, pluginManager: any): void;
11
+ }
12
+ export {};
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var lessAliasPlugin_exports = {};
30
+ __export(lessAliasPlugin_exports, {
31
+ default: () => LessAliasesPlugin
32
+ });
33
+ module.exports = __toCommonJS(lessAliasPlugin_exports);
34
+ var import_define_property = require("@swc/helpers/_/_define_property");
35
+ var import_fs = __toESM(require("fs"));
36
+ var import_utils = require("./utils");
37
+ class LessAliasesPlugin {
38
+ install(less, pluginManager) {
39
+ const getResolve = (filename, currentDirectory) => {
40
+ return this.compiler.css_resolve(filename, currentDirectory || this.stdinDir);
41
+ };
42
+ class AliasPlugin extends less.FileManager {
43
+ async loadFile(filename, currentDirectory) {
44
+ const resolved = getResolve(filename, currentDirectory);
45
+ const rebasedContents = await (0, import_utils.rebaseUrls)(resolved, this.stdinDir, this.compiler.css_resolve);
46
+ const contents = rebasedContents.contents ? rebasedContents.contents : import_fs.default.readFileSync(resolved, "utf8");
47
+ return {
48
+ filename: resolved,
49
+ contents
50
+ };
51
+ }
52
+ constructor(options) {
53
+ super();
54
+ (0, import_define_property._)(this, "compiler", void 0);
55
+ this.compiler = options.compiler;
56
+ this.stdinDir = options.stdinDir;
57
+ }
58
+ }
59
+ pluginManager.addFileManager(new AliasPlugin({
60
+ compiler: this.compiler,
61
+ stdinDir: this.stdinDir
62
+ }));
63
+ }
64
+ constructor(options) {
65
+ (0, import_define_property._)(this, "compiler", void 0);
66
+ (0, import_define_property._)(this, "stdinDir", void 0);
67
+ this.compiler = options.compiler;
68
+ this.stdinDir = options.stdinDir;
69
+ }
70
+ }
@@ -0,0 +1,2 @@
1
+ import { PreprocessRender } from './transformStyle';
2
+ export declare const lessRender: PreprocessRender;
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var lessRender_exports = {};
30
+ __export(lessRender_exports, {
31
+ lessRender: () => lessRender
32
+ });
33
+ module.exports = __toCommonJS(lessRender_exports);
34
+ var import_utils = require("./utils");
35
+ var import_lessAliasPlugin = __toESM(require("./lessAliasPlugin"));
36
+ const lessRender = async function(content, originPath, stdinDir, options, _, implementation) {
37
+ const less = await (0, import_utils.loadProcessor)("less", this.context.root, implementation);
38
+ const result = {
39
+ ...await less.render(content, {
40
+ relativeUrls: true,
41
+ filename: originPath,
42
+ ...options,
43
+ paths: [
44
+ ...(options === null || options === void 0 ? void 0 : options.paths) || [
45
+ "node_modules"
46
+ ],
47
+ this.context.root,
48
+ stdinDir
49
+ ],
50
+ plugins: [
51
+ new import_lessAliasPlugin.default({
52
+ compiler: this,
53
+ stdinDir
54
+ }),
55
+ ...(options === null || options === void 0 ? void 0 : options.plugins) || []
56
+ ]
57
+ })
58
+ };
59
+ return result;
60
+ };
61
+ // Annotate the CommonJS export names for ESM import in node:
62
+ 0 && (module.exports = {
63
+ lessRender
64
+ });
@@ -0,0 +1,6 @@
1
+ import { ICompiler } from '../../../types';
2
+ export declare const isCssModule: (filePath: string, autoModules: boolean | RegExp) => boolean;
3
+ export declare const postcssTransformer: (css: string, entryPath: string, compilation: ICompiler) => Promise<{
4
+ code: string;
5
+ loader: 'js' | 'css';
6
+ }>;