@modern-js/module-tools 2.35.1 → 2.37.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 (223) hide show
  1. package/dist/build.js +43 -16
  2. package/dist/builder/build.d.ts +0 -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 +286 -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 +18 -0
  19. package/dist/builder/esbuild/resolve.js +140 -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 +223 -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 +130 -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.js +59 -28
  59. package/dist/builder/platform.js +38 -23
  60. package/dist/cli.js +58 -29
  61. package/dist/command.js +62 -38
  62. package/dist/config/defineConfig.js +25 -17
  63. package/dist/config/legacySchema.js +35 -32
  64. package/dist/config/merge.js +69 -36
  65. package/dist/config/normalize.js +63 -46
  66. package/dist/config/schema.d.ts +48 -0
  67. package/dist/config/schema.js +53 -34
  68. package/dist/config/transformLegacyConfig.js +67 -61
  69. package/dist/config/valid.d.ts +2 -2
  70. package/dist/config/valid.js +44 -21
  71. package/dist/constants/build.d.ts +5 -1
  72. package/dist/constants/build.js +88 -46
  73. package/dist/constants/color.js +27 -20
  74. package/dist/constants/dts.js +27 -20
  75. package/dist/constants/file.d.ts +2 -2
  76. package/dist/constants/file.js +48 -25
  77. package/dist/constants/legacy-preset.js +45 -32
  78. package/dist/constants/loader.d.ts +2 -0
  79. package/dist/constants/loader.js +53 -0
  80. package/dist/constants/log.d.ts +0 -8
  81. package/dist/constants/log.js +27 -41
  82. package/dist/constants/preset.d.ts +4 -4
  83. package/dist/constants/preset.js +55 -62
  84. package/dist/debug.d.ts +4 -0
  85. package/dist/debug.js +37 -0
  86. package/dist/dev.js +42 -25
  87. package/dist/error.js +49 -42
  88. package/dist/hooks/build.d.ts +1 -2
  89. package/dist/hooks/build.js +36 -28
  90. package/dist/hooks/dev.js +32 -17
  91. package/dist/hooks/index.d.ts +1 -1
  92. package/dist/hooks/index.js +31 -16
  93. package/dist/hooks/misc.d.ts +5 -2
  94. package/dist/hooks/misc.js +31 -12
  95. package/dist/index.d.ts +2 -1
  96. package/dist/index.js +39 -32
  97. package/dist/locale/en.js +32 -17
  98. package/dist/locale/index.d.ts +2 -1
  99. package/dist/locale/index.js +31 -24
  100. package/dist/locale/zh.js +32 -17
  101. package/dist/plugins.js +28 -13
  102. package/dist/types/color.js +15 -5
  103. package/dist/types/command.d.ts +3 -3
  104. package/dist/types/command.js +15 -5
  105. package/dist/types/config/copy.js +15 -5
  106. package/dist/types/config/dev.js +15 -5
  107. package/dist/types/config/index.d.ts +87 -52
  108. package/dist/types/config/index.js +24 -6
  109. package/dist/types/config/style.d.ts +58 -6
  110. package/dist/types/config/style.js +15 -5
  111. package/dist/types/context.js +15 -5
  112. package/dist/types/dts.d.ts +14 -3
  113. package/dist/types/dts.js +15 -5
  114. package/dist/types/esbuild.d.ts +107 -0
  115. package/dist/types/esbuild.js +32 -0
  116. package/dist/types/hooks.js +15 -5
  117. package/dist/types/index.d.ts +1 -0
  118. package/dist/types/index.js +32 -11
  119. package/dist/types/legacyConfig/index.js +15 -5
  120. package/dist/types/legacyConfig/output.d.ts +4 -4
  121. package/dist/types/legacyConfig/output.js +15 -5
  122. package/dist/types/legacyConfig/source.js +15 -5
  123. package/dist/types/legacyConfig/tools.js +15 -5
  124. package/dist/utils/assert.d.ts +5 -0
  125. package/dist/utils/assert.js +50 -0
  126. package/dist/utils/builder.d.ts +10 -14
  127. package/dist/utils/builder.js +59 -59
  128. package/dist/utils/common.d.ts +1 -1
  129. package/dist/utils/common.js +25 -17
  130. package/dist/utils/dts.d.ts +26 -9
  131. package/dist/utils/dts.js +98 -64
  132. package/dist/utils/hash.d.ts +2 -0
  133. package/dist/utils/hash.js +31 -0
  134. package/dist/utils/index.d.ts +11 -0
  135. package/dist/utils/index.js +42 -0
  136. package/dist/utils/input.js +47 -24
  137. package/dist/utils/log.d.ts +1 -2
  138. package/dist/utils/log.js +36 -20
  139. package/dist/utils/map.d.ts +13 -0
  140. package/dist/utils/map.js +58 -0
  141. package/dist/utils/onExit.js +35 -11
  142. package/dist/utils/print.d.ts +2 -2
  143. package/dist/utils/print.js +52 -49
  144. package/dist/utils/style.d.ts +5 -23
  145. package/dist/utils/style.js +57 -37
  146. package/dist/utils/tspath.js +114 -0
  147. package/package.json +61 -50
  148. package/vitest.config.ts +20 -0
  149. package/dist/build.js.map +0 -1
  150. package/dist/builder/build.js.map +0 -1
  151. package/dist/builder/clear.js.map +0 -1
  152. package/dist/builder/copy.js.map +0 -1
  153. package/dist/builder/dts/index.js.map +0 -1
  154. package/dist/builder/dts/rollup.js.map +0 -1
  155. package/dist/builder/dts/tsc.js.map +0 -1
  156. package/dist/builder/index.js.map +0 -1
  157. package/dist/builder/platform.js.map +0 -1
  158. package/dist/cli.js.map +0 -1
  159. package/dist/command.js.map +0 -1
  160. package/dist/config/defineConfig.js.map +0 -1
  161. package/dist/config/legacySchema.js.map +0 -1
  162. package/dist/config/merge.js.map +0 -1
  163. package/dist/config/normalize.js.map +0 -1
  164. package/dist/config/schema.js.map +0 -1
  165. package/dist/config/transformLegacyConfig.js.map +0 -1
  166. package/dist/config/valid.js.map +0 -1
  167. package/dist/constants/build.js.map +0 -1
  168. package/dist/constants/color.js.map +0 -1
  169. package/dist/constants/dts.js.map +0 -1
  170. package/dist/constants/file.js.map +0 -1
  171. package/dist/constants/legacy-preset.js.map +0 -1
  172. package/dist/constants/log.js.map +0 -1
  173. package/dist/constants/preset.js.map +0 -1
  174. package/dist/dev.js.map +0 -1
  175. package/dist/error.js.map +0 -1
  176. package/dist/hooks/build.js.map +0 -1
  177. package/dist/hooks/dev.js.map +0 -1
  178. package/dist/hooks/index.js.map +0 -1
  179. package/dist/hooks/misc.js.map +0 -1
  180. package/dist/index.js.map +0 -1
  181. package/dist/locale/en.js.map +0 -1
  182. package/dist/locale/index.js.map +0 -1
  183. package/dist/locale/zh.js.map +0 -1
  184. package/dist/plugins.js.map +0 -1
  185. package/dist/types/color.js.map +0 -1
  186. package/dist/types/command.js.map +0 -1
  187. package/dist/types/config/copy.js.map +0 -1
  188. package/dist/types/config/dev.js.map +0 -1
  189. package/dist/types/config/index.js.map +0 -1
  190. package/dist/types/config/style.js.map +0 -1
  191. package/dist/types/context.js.map +0 -1
  192. package/dist/types/dts.js.map +0 -1
  193. package/dist/types/hooks.js.map +0 -1
  194. package/dist/types/index.js.map +0 -1
  195. package/dist/types/legacyConfig/index.js.map +0 -1
  196. package/dist/types/legacyConfig/output.js.map +0 -1
  197. package/dist/types/legacyConfig/source.js.map +0 -1
  198. package/dist/types/legacyConfig/tools.js.map +0 -1
  199. package/dist/types/utils.d.ts +0 -5
  200. package/dist/types/utils.js +0 -4
  201. package/dist/utils/builder.js.map +0 -1
  202. package/dist/utils/common.js.map +0 -1
  203. package/dist/utils/context.d.ts +0 -7
  204. package/dist/utils/context.js +0 -23
  205. package/dist/utils/context.js.map +0 -1
  206. package/dist/utils/dts.js.map +0 -1
  207. package/dist/utils/input.js.map +0 -1
  208. package/dist/utils/language.d.ts +0 -1
  209. package/dist/utils/language.js +0 -22
  210. package/dist/utils/language.js.map +0 -1
  211. package/dist/utils/libuild-plugin.d.ts +0 -3
  212. package/dist/utils/libuild-plugin.js +0 -31
  213. package/dist/utils/libuild-plugin.js.map +0 -1
  214. package/dist/utils/log.js.map +0 -1
  215. package/dist/utils/onExit.js.map +0 -1
  216. package/dist/utils/path.d.ts +0 -1
  217. package/dist/utils/path.js +0 -19
  218. package/dist/utils/path.js.map +0 -1
  219. package/dist/utils/print.js.map +0 -1
  220. package/dist/utils/style.js.map +0 -1
  221. package/dist/utils/tspathsTransform.js +0 -91
  222. package/dist/utils/tspathsTransform.js.map +0 -1
  223. /package/dist/utils/{tspathsTransform.d.ts → tspath.d.ts} +0 -0
@@ -0,0 +1,85 @@
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 sassRender_exports = {};
30
+ __export(sassRender_exports, {
31
+ sassRender: () => sassRender
32
+ });
33
+ module.exports = __toCommonJS(sassRender_exports);
34
+ var import_fs = require("fs");
35
+ var import_path = __toESM(require("path"));
36
+ var import_utils = require("./utils");
37
+ const sassRender = async function(content, _, stdinDir, options, resolvePathMap, implementation) {
38
+ const sass = await (0, import_utils.loadProcessor)("sass", this.context.root, implementation);
39
+ return new Promise((resolve, reject) => {
40
+ sass.render({
41
+ data: content,
42
+ importer: [
43
+ (url, dir, done) => {
44
+ if (url.startsWith("data:")) {
45
+ done({
46
+ contents: url
47
+ });
48
+ }
49
+ let filePath = null;
50
+ const baseUrl = import_path.default.basename(url);
51
+ const dirUrl = import_path.default.dirname(url);
52
+ const removeUnderScoreBaseUrl = baseUrl.replace(/^_/, "");
53
+ const prependUnderScoreBaseUrl = `_${removeUnderScoreBaseUrl}`;
54
+ try {
55
+ const id = import_path.default.join(dirUrl, prependUnderScoreBaseUrl);
56
+ filePath = this.css_resolve(id, dir === "stdin" ? stdinDir : resolvePathMap.get(dir));
57
+ } catch (err) {
58
+ const id = import_path.default.join(dirUrl, removeUnderScoreBaseUrl);
59
+ filePath = this.css_resolve(id, dir === "stdin" ? stdinDir : resolvePathMap.get(dir));
60
+ }
61
+ resolvePathMap.set(url, import_path.default.dirname(filePath));
62
+ if (url.startsWith("./")) {
63
+ resolvePathMap.set(url.replace(/^\.\//, ""), import_path.default.dirname(filePath));
64
+ }
65
+ (0, import_utils.rebaseUrls)(filePath, stdinDir, this.css_resolve).then((value) => {
66
+ done({
67
+ contents: value.contents || (0, import_fs.readFileSync)(value.file, "utf-8")
68
+ });
69
+ });
70
+ }
71
+ ],
72
+ ...options
73
+ }, (err, res) => {
74
+ if (err) {
75
+ reject(err);
76
+ } else {
77
+ resolve(res);
78
+ }
79
+ });
80
+ });
81
+ };
82
+ // Annotate the CommonJS export names for ESM import in node:
83
+ 0 && (module.exports = {
84
+ sassRender
85
+ });
@@ -0,0 +1,13 @@
1
+ /// <reference types="node" />
2
+ import { PartialMessage } from 'esbuild';
3
+ import { ICompiler, Source } from '../../../types';
4
+ export type PreprocessRender = (content: string, stdinPath: string, stdinDir: string, preprocessOptions: any, resolvePathMap: Map<string, string>, implementation?: object | string) => Promise<{
5
+ css: Buffer | string;
6
+ errors?: PartialMessage[];
7
+ warnings?: PartialMessage[];
8
+ map?: Buffer | string;
9
+ }>;
10
+ export declare function transformStyle(this: ICompiler, source: Source): Promise<{
11
+ code: string;
12
+ loader: "js" | "css";
13
+ }>;
@@ -0,0 +1,101 @@
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 transformStyle_exports = {};
30
+ __export(transformStyle_exports, {
31
+ transformStyle: () => transformStyle
32
+ });
33
+ module.exports = __toCommonJS(transformStyle_exports);
34
+ var import_path = __toESM(require("path"));
35
+ var import_postcssTransformer = require("./postcssTransformer");
36
+ var import_lessRender = require("./lessRender");
37
+ var import_sassRender = require("./sassRender");
38
+ const cssLangs = `\\.(css|less|sass|scss)($|\\?)`;
39
+ const cssLangRE = new RegExp(cssLangs);
40
+ const cssRender = async function(content) {
41
+ return {
42
+ css: content
43
+ };
44
+ };
45
+ const renderMap = {
46
+ less: import_lessRender.lessRender,
47
+ sass: import_sassRender.sassRender,
48
+ scss: import_sassRender.sassRender,
49
+ css: cssRender
50
+ };
51
+ async function transformStyle(source) {
52
+ var _source_path_match;
53
+ const lang = (_source_path_match = source.path.match(cssLangRE)) === null || _source_path_match === void 0 ? void 0 : _source_path_match[1];
54
+ if (!lang) {
55
+ throw new Error(`UNSUPPORTED_CSS_LANG: not supported css lang${lang}`);
56
+ }
57
+ const { less, sass } = this.config.style;
58
+ let options;
59
+ let additionalData;
60
+ let implementation;
61
+ if (lang === "less") {
62
+ options = less === null || less === void 0 ? void 0 : less.lessOptions;
63
+ additionalData = less === null || less === void 0 ? void 0 : less.additionalData;
64
+ implementation = less === null || less === void 0 ? void 0 : less.implementation;
65
+ }
66
+ if (lang === "scss" || lang === "sass") {
67
+ options = sass === null || sass === void 0 ? void 0 : sass.sassOptions;
68
+ additionalData = sass === null || sass === void 0 ? void 0 : sass.additionalData;
69
+ implementation = sass === null || sass === void 0 ? void 0 : sass.implementation;
70
+ }
71
+ const preprocessRender = renderMap[lang];
72
+ const stdinDir = import_path.default.dirname(source.path);
73
+ const resolvePathMap = /* @__PURE__ */ new Map();
74
+ let content = "";
75
+ if (typeof additionalData === "string") {
76
+ content = `${additionalData}
77
+ `;
78
+ } else if (typeof additionalData === "function") {
79
+ content = `${additionalData(source.path)}
80
+ `;
81
+ }
82
+ content += source.code;
83
+ const renderResult = await preprocessRender.apply(this, [
84
+ content,
85
+ source.path,
86
+ stdinDir,
87
+ options,
88
+ resolvePathMap,
89
+ implementation
90
+ ]);
91
+ const css = renderResult.css.toString();
92
+ const { code, loader } = await (0, import_postcssTransformer.postcssTransformer)(css !== null && css !== void 0 ? css : "", source.path, this);
93
+ return {
94
+ code,
95
+ loader
96
+ };
97
+ }
98
+ // Annotate the CommonJS export names for ESM import in node:
99
+ 0 && (module.exports = {
100
+ transformStyle
101
+ });
@@ -0,0 +1,18 @@
1
+ /**
2
+ * make sure load processor in user project root other than root
3
+ * @param lang
4
+ * @param root
5
+ * @returns
6
+ */
7
+ export declare function loadProcessor(lang: string, root: string, implementation?: object | string): any;
8
+ type CssUrlReplacer = (url: string, importer?: string) => string | Promise<string>;
9
+ /**
10
+ * relative url() inside \@imported sass and less files must be rebased to use
11
+ * root file as base.
12
+ */
13
+ export declare function rebaseUrls(filepath: string, rootDir: string, resolver: (id: string, dir: string) => string): Promise<{
14
+ file: string;
15
+ contents?: string;
16
+ }>;
17
+ export declare function rewriteCssUrls(css: string, type: false | string, replacer: CssUrlReplacer): Promise<string>;
18
+ export {};
@@ -0,0 +1,130 @@
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 utils_exports = {};
30
+ __export(utils_exports, {
31
+ loadProcessor: () => loadProcessor,
32
+ rebaseUrls: () => rebaseUrls,
33
+ rewriteCssUrls: () => rewriteCssUrls
34
+ });
35
+ module.exports = __toCommonJS(utils_exports);
36
+ var import_fs = __toESM(require("fs"));
37
+ var import_path = __toESM(require("path"));
38
+ var import_utils = require("../../../utils");
39
+ const cache = {};
40
+ function loadProcessor(lang, root, implementation) {
41
+ if (cache[lang]) {
42
+ return cache[lang];
43
+ }
44
+ try {
45
+ if (typeof implementation === "string") {
46
+ return cache[lang] = require(implementation);
47
+ }
48
+ if (typeof implementation === "object") {
49
+ return cache[lang] = implementation;
50
+ }
51
+ const loadPath = require.resolve(lang, {
52
+ paths: [
53
+ root
54
+ ]
55
+ });
56
+ return cache[lang] = require(loadPath);
57
+ } catch (err) {
58
+ throw new Error(`${lang} require failed, please install it or use implemention`);
59
+ }
60
+ }
61
+ const dataUrlRE = /^\s*data:/i;
62
+ const cssUrlRE = RegExp(`(?<=^|[^\\w\\-\\u0080-\\uffff])url\\(\\s*('[^']+'|"[^"]+"|[^'")]+)\\s*\\)`);
63
+ const externalRE = /^(https?:)?\/\//;
64
+ const isDataUrl = (url) => dataUrlRE.test(url);
65
+ const isExternalUrl = (url) => externalRE.test(url);
66
+ async function rebaseUrls(filepath, rootDir, resolver) {
67
+ const file = import_path.default.resolve(filepath);
68
+ const fileDir = import_path.default.dirname(file);
69
+ if (fileDir === rootDir) {
70
+ return {
71
+ file
72
+ };
73
+ }
74
+ const content = import_fs.default.readFileSync(file, "utf-8");
75
+ if (!cssUrlRE.test(content)) {
76
+ return {
77
+ file
78
+ };
79
+ }
80
+ try {
81
+ const rebased = await rewriteCssUrls(content, import_path.default.extname(file).slice(1), (url) => {
82
+ if (url.startsWith("/")) {
83
+ return url;
84
+ }
85
+ return resolver(url, fileDir);
86
+ });
87
+ return {
88
+ file,
89
+ contents: rebased
90
+ };
91
+ } catch (e) {
92
+ return {
93
+ file
94
+ };
95
+ }
96
+ }
97
+ function rewriteCssUrls(css, type, replacer) {
98
+ return asyncReplace(css, cssUrlRE, async (match) => {
99
+ const matched = match[0];
100
+ let rawUrl = match[1];
101
+ let wrap = "";
102
+ const first = rawUrl[0];
103
+ if (first === `"` || first === `'`) {
104
+ wrap = first;
105
+ rawUrl = rawUrl.slice(1, -1);
106
+ }
107
+ if (type === "less" && rawUrl.startsWith("@") || (type === "sass" || type === "scss") && rawUrl.startsWith("$") || isExternalUrl(rawUrl) || isDataUrl(rawUrl) || rawUrl.startsWith("#")) {
108
+ return matched;
109
+ }
110
+ return `url(${wrap}${(0, import_utils.normalizeSlashes)(await replacer(rawUrl))}${wrap})`;
111
+ });
112
+ }
113
+ async function asyncReplace(input, re, replacer) {
114
+ let match;
115
+ let remaining = input;
116
+ let rewritten = "";
117
+ while (match = re.exec(remaining)) {
118
+ rewritten += remaining.slice(0, match.index);
119
+ rewritten += await replacer(match);
120
+ remaining = remaining.slice(match.index + match[0].length);
121
+ }
122
+ rewritten += remaining;
123
+ return rewritten;
124
+ }
125
+ // Annotate the CommonJS export names for ESM import in node:
126
+ 0 && (module.exports = {
127
+ loadProcessor,
128
+ rebaseUrls,
129
+ rewriteCssUrls
130
+ });
@@ -0,0 +1,9 @@
1
+ import type { ICompiler, ITsconfig } from '../../types';
2
+ export declare const swcTransform: (userTsconfig: ITsconfig) => {
3
+ name: string;
4
+ apply(compiler: ICompiler): void;
5
+ };
6
+ export declare const swcRenderChunk: {
7
+ name: string;
8
+ apply(compiler: ICompiler): void;
9
+ };
@@ -0,0 +1,170 @@
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 swc_exports = {};
20
+ __export(swc_exports, {
21
+ swcRenderChunk: () => swcRenderChunk,
22
+ swcTransform: () => swcTransform
23
+ });
24
+ module.exports = __toCommonJS(swc_exports);
25
+ var import_path = require("path");
26
+ var import_swc_plugins = require("@modern-js/swc-plugins");
27
+ var import_utils = require("../../utils");
28
+ const name = "swc:transform";
29
+ const getSwcTarget = (target) => {
30
+ const list = [
31
+ "es3",
32
+ "es5",
33
+ "es2015",
34
+ "es2016",
35
+ "es2017",
36
+ "es2018",
37
+ "es2019",
38
+ "es2020",
39
+ "es2021",
40
+ "es2022"
41
+ ];
42
+ if (list.includes(target)) {
43
+ return target;
44
+ }
45
+ if (target === "next") {
46
+ return "es2022";
47
+ }
48
+ if (target === "es6") {
49
+ return "es2015";
50
+ }
51
+ return "es2022";
52
+ };
53
+ const swcTransform = (userTsconfig) => ({
54
+ name,
55
+ apply(compiler) {
56
+ var _userTsconfig_compilerOptions, _userTsconfig_compilerOptions1, _userTsconfig_compilerOptions2;
57
+ const tsUseDefineForClassFields = userTsconfig === null || userTsconfig === void 0 ? void 0 : (_userTsconfig_compilerOptions = userTsconfig.compilerOptions) === null || _userTsconfig_compilerOptions === void 0 ? void 0 : _userTsconfig_compilerOptions.useDefineForClassFields;
58
+ var _userTsconfig_compilerOptions_emitDecoratorMetadata;
59
+ const emitDecoratorMetadata = (_userTsconfig_compilerOptions_emitDecoratorMetadata = userTsconfig === null || userTsconfig === void 0 ? void 0 : (_userTsconfig_compilerOptions1 = userTsconfig.compilerOptions) === null || _userTsconfig_compilerOptions1 === void 0 ? void 0 : _userTsconfig_compilerOptions1.emitDecoratorMetadata) !== null && _userTsconfig_compilerOptions_emitDecoratorMetadata !== void 0 ? _userTsconfig_compilerOptions_emitDecoratorMetadata : false;
60
+ let useDefineForClassFields;
61
+ let tsTarget = userTsconfig === null || userTsconfig === void 0 ? void 0 : (_userTsconfig_compilerOptions2 = userTsconfig.compilerOptions) === null || _userTsconfig_compilerOptions2 === void 0 ? void 0 : _userTsconfig_compilerOptions2.target;
62
+ tsTarget = tsTarget ? tsTarget.toLowerCase() : void 0;
63
+ if (tsUseDefineForClassFields !== void 0) {
64
+ useDefineForClassFields = tsUseDefineForClassFields;
65
+ } else if (tsTarget !== void 0) {
66
+ useDefineForClassFields = (0, import_utils.tsTargetAtOrAboveES2022)(tsTarget);
67
+ } else {
68
+ useDefineForClassFields = true;
69
+ }
70
+ const { transformImport, transformLodash, externalHelpers } = compiler.config;
71
+ compiler.hooks.transform.tapPromise({
72
+ name
73
+ }, async (source) => {
74
+ const { path } = source;
75
+ const isTs = (0, import_utils.isTsLoader)(source.loader) || (0, import_utils.isTsExt)(path);
76
+ const enableJsx = source.loader === "tsx" || source.loader === "jsx" || /\.tsx$|\.jsx$/i.test(path);
77
+ if ((0, import_utils.isJsExt)(path) || (0, import_utils.isJsLoader)(source.loader)) {
78
+ const { target, jsx } = compiler.config;
79
+ const swcCompilerOptions = {
80
+ sourceMaps: Boolean(compiler.config.sourceMap),
81
+ inputSourceMap: false,
82
+ swcrc: false,
83
+ configFile: false,
84
+ jsc: {
85
+ parser: isTs ? {
86
+ syntax: "typescript",
87
+ tsx: enableJsx,
88
+ decorators: true
89
+ } : {
90
+ syntax: "ecmascript",
91
+ jsx: enableJsx,
92
+ decorators: true
93
+ },
94
+ transform: {
95
+ react: {
96
+ runtime: jsx === "transform" ? "classic" : "automatic"
97
+ },
98
+ useDefineForClassFields,
99
+ legacyDecorator: emitDecoratorMetadata ? true : void 0,
100
+ decoratorMetadata: emitDecoratorMetadata ? true : void 0
101
+ },
102
+ externalHelpers,
103
+ target: getSwcTarget(target)
104
+ },
105
+ isModule: "unknown",
106
+ extensions: {
107
+ pluginImport: transformImport,
108
+ lodash: transformLodash ? {
109
+ cwd: compiler.context.root,
110
+ ids: [
111
+ "lodash",
112
+ "lodash-es"
113
+ ]
114
+ } : void 0
115
+ }
116
+ };
117
+ const swcCompiler = new import_swc_plugins.Compiler(swcCompilerOptions);
118
+ const result = await swcCompiler.transform((0, import_path.basename)(path), source.code);
119
+ return {
120
+ ...source,
121
+ code: result.code,
122
+ map: typeof result.map === "string" ? JSON.parse(result.map) : result.map
123
+ };
124
+ }
125
+ return source;
126
+ });
127
+ }
128
+ });
129
+ const swcRenderChunk = {
130
+ name: "swc:renderChunk",
131
+ apply(compiler) {
132
+ compiler.hooks.renderChunk.tapPromise({
133
+ name: "swc:renderChunk"
134
+ }, async (chunk) => {
135
+ if (chunk.fileName.endsWith(".js") && chunk.type === "chunk") {
136
+ const { umdModuleName, format } = compiler.config;
137
+ const name2 = typeof umdModuleName === "function" ? umdModuleName(chunk.fileName) : umdModuleName;
138
+ const swcCompiler = new import_swc_plugins.Compiler({
139
+ sourceMaps: Boolean(compiler.config.sourceMap),
140
+ inputSourceMap: false,
141
+ swcrc: false,
142
+ configFile: false,
143
+ extensions: {},
144
+ jsc: {
145
+ target: getSwcTarget(compiler.config.target),
146
+ parser: {
147
+ syntax: "ecmascript"
148
+ }
149
+ },
150
+ module: format === "umd" ? {
151
+ type: "umd"
152
+ } : void 0,
153
+ isModule: "unknown"
154
+ });
155
+ const result = await swcCompiler.transform(name2, chunk.contents.toString());
156
+ return {
157
+ ...chunk,
158
+ contents: result.code,
159
+ map: typeof result.map === "string" ? JSON.parse(result.map) : result.map
160
+ };
161
+ }
162
+ return chunk;
163
+ });
164
+ }
165
+ };
166
+ // Annotate the CommonJS export names for ESM import in node:
167
+ 0 && (module.exports = {
168
+ swcRenderChunk,
169
+ swcTransform
170
+ });
@@ -0,0 +1,5 @@
1
+ import { ICompiler } from '../../types';
2
+ export declare const minify: {
3
+ name: string;
4
+ apply: (compiler: ICompiler) => void;
5
+ };
@@ -0,0 +1,83 @@
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 terser_exports = {};
20
+ __export(terser_exports, {
21
+ minify: () => minify
22
+ });
23
+ module.exports = __toCommonJS(terser_exports);
24
+ var import_utils = require("@modern-js/utils");
25
+ var import_terser = require("terser");
26
+ var import_types = require("../../types");
27
+ var import_utils2 = require("../../utils");
28
+ const name = "terser";
29
+ const apply = (compiler) => {
30
+ compiler.hooks.renderChunk.tapPromise({
31
+ name
32
+ }, async (chunk) => {
33
+ const { sourceMap, minify: minify2 } = compiler.config;
34
+ if (chunk.type === import_types.ChunkType.chunk) {
35
+ const code = chunk.contents.toString();
36
+ const needSourceMap = Boolean(sourceMap);
37
+ const terserOptions = resolveTerserOptions(minify2, {
38
+ sourceMap: Boolean(needSourceMap)
39
+ });
40
+ const result = await (0, import_terser.minify)(code, {
41
+ ...terserOptions,
42
+ sourceMap: needSourceMap
43
+ });
44
+ return {
45
+ ...chunk,
46
+ contents: result.code || chunk.contents,
47
+ map: (0, import_utils2.normalizeSourceMap)(result.map, {
48
+ needSourceMap
49
+ })
50
+ };
51
+ }
52
+ return chunk;
53
+ });
54
+ };
55
+ const minify = {
56
+ name,
57
+ apply
58
+ };
59
+ function resolveTerserOptions(terserOptions, { sourceMap }) {
60
+ return import_utils.lodash.merge({
61
+ compress: {
62
+ inline: 2,
63
+ comparisons: false
64
+ },
65
+ format: {
66
+ keep_quoted_props: true,
67
+ comments: false
68
+ },
69
+ // Return object to avoid redundant `JSON.parse` in remapping
70
+ sourceMap: sourceMap ? {
71
+ asObject: true,
72
+ // `includeSources` is not necessary for minification,
73
+ // and we can utilize this to reduce the size of the source map.
74
+ includeSources: false
75
+ } : false,
76
+ safari10: true,
77
+ toplevel: true
78
+ }, (0, import_utils.isObject)(terserOptions) ? terserOptions : {});
79
+ }
80
+ // Annotate the CommonJS export names for ESM import in node:
81
+ 0 && (module.exports = {
82
+ minify
83
+ });