@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,275 @@
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 adapter_exports = {};
30
+ __export(adapter_exports, {
31
+ adapterPlugin: () => adapterPlugin
32
+ });
33
+ module.exports = __toCommonJS(adapter_exports);
34
+ var import_path = require("path");
35
+ var import_module = __toESM(require("module"));
36
+ var import_picomatch = __toESM(require("picomatch"));
37
+ var import_utils = require("@modern-js/utils");
38
+ var import_pluginutils = require("@rollup/pluginutils");
39
+ var import_utils2 = require("../../utils");
40
+ var import_loader = require("../../constants/loader");
41
+ var import_debug = require("../../debug");
42
+ var import_write_file = require("./write-file");
43
+ const globalNamespace = "globals";
44
+ const HTTP_PATTERNS = /^(https?:)?\/\//;
45
+ const DATAURL_PATTERNS = /^data:/;
46
+ const HASH_PATTERNS = /#[^#]+$/;
47
+ const DATAURL_JAVASCRIPT_PATTERNS = /^data:text\/javascript/;
48
+ const adapterPlugin = (compiler) => {
49
+ const { context } = compiler;
50
+ const { config, root } = context;
51
+ return {
52
+ name: "esbuild:adapter",
53
+ setup(build) {
54
+ build.onStart(async () => {
55
+ compiler.outputChunk = /* @__PURE__ */ new Map();
56
+ });
57
+ build.onResolve({
58
+ filter: /.*/
59
+ }, async (args) => {
60
+ if (args.kind === "url-token") {
61
+ return {
62
+ path: args.path,
63
+ external: true
64
+ };
65
+ }
66
+ for (const [key] of Object.entries(config.umdGlobals)) {
67
+ const isMatch = (0, import_picomatch.default)(key);
68
+ if (isMatch(args.path)) {
69
+ (0, import_debug.debugResolve)("resolve umdGlobals:", key);
70
+ return {
71
+ path: args.path,
72
+ namespace: globalNamespace
73
+ };
74
+ }
75
+ }
76
+ if (/^node:/.test(args.path)) {
77
+ return {
78
+ path: args.path.slice(5),
79
+ external: true
80
+ };
81
+ }
82
+ if (DATAURL_JAVASCRIPT_PATTERNS.test(args.path)) {
83
+ return {
84
+ path: args.path,
85
+ namespace: "dataurl"
86
+ };
87
+ }
88
+ const isUrl = (source) => HTTP_PATTERNS.test(source) || DATAURL_PATTERNS.test(source) || HASH_PATTERNS.test(source);
89
+ if (isUrl(args.path)) {
90
+ return {
91
+ path: args.path,
92
+ external: true
93
+ };
94
+ }
95
+ const { externals, sideEffects: userSideEffects } = config;
96
+ const regExternal = externals.filter((item) => !(0, import_utils.isString)(item));
97
+ const externalList = externals.filter(import_utils.isString).concat(config.platform === "node" ? import_module.default.builtinModules : []);
98
+ const externalMap = externalList.reduce((map, item) => {
99
+ map.set(item, true);
100
+ return map;
101
+ }, /* @__PURE__ */ new Map());
102
+ const getIsExternal = (name) => {
103
+ if (externalMap.get(name)) {
104
+ return true;
105
+ }
106
+ if (regExternal.some((reg) => reg.test(name))) {
107
+ return true;
108
+ }
109
+ return false;
110
+ };
111
+ const getSideEffects = async (filePath, isExternal2) => {
112
+ let pkgPath = "";
113
+ let sideEffects2 = userSideEffects;
114
+ let moduleSideEffects = true;
115
+ if (typeof userSideEffects === "undefined") {
116
+ let curDir = (0, import_path.dirname)(filePath);
117
+ try {
118
+ while (curDir !== (0, import_path.dirname)(curDir)) {
119
+ if (import_utils.fs.existsSync((0, import_path.resolve)(curDir, "package.json"))) {
120
+ pkgPath = (0, import_path.resolve)(curDir, "package.json");
121
+ break;
122
+ }
123
+ curDir = (0, import_path.dirname)(curDir);
124
+ }
125
+ sideEffects2 = JSON.parse(import_utils.fs.readFileSync(pkgPath, "utf-8")).sideEffects;
126
+ } catch (err) {
127
+ }
128
+ if (!pkgPath) {
129
+ return void 0;
130
+ }
131
+ }
132
+ if (typeof sideEffects2 === "boolean") {
133
+ moduleSideEffects = sideEffects2;
134
+ } else if (Array.isArray(sideEffects2)) {
135
+ moduleSideEffects = (0, import_pluginutils.createFilter)(sideEffects2.map((glob) => {
136
+ if (typeof glob === "string") {
137
+ if (!glob.includes("/")) {
138
+ return `**/${glob}`;
139
+ }
140
+ }
141
+ return glob;
142
+ }), null, pkgPath ? {
143
+ resolve: (0, import_path.dirname)(pkgPath)
144
+ } : void 0)(filePath);
145
+ } else if (typeof sideEffects2 === "function") {
146
+ moduleSideEffects = sideEffects2(filePath, isExternal2);
147
+ }
148
+ return moduleSideEffects;
149
+ };
150
+ const getResultPath = (id, dir2, kind) => {
151
+ return id.endsWith(".css") ? compiler.css_resolve(id, dir2) : compiler.node_resolve(id, dir2, kind);
152
+ };
153
+ const { originalFilePath, rawQuery } = (0, import_utils2.resolvePathAndQuery)(args.path);
154
+ const suffix = (rawQuery !== null && rawQuery !== void 0 ? rawQuery : "").length > 0 ? `?${rawQuery}` : "";
155
+ const isExternal = getIsExternal(originalFilePath);
156
+ var _args_resolveDir;
157
+ const dir = (_args_resolveDir = args.resolveDir) !== null && _args_resolveDir !== void 0 ? _args_resolveDir : args.importer ? (0, import_path.dirname)(args.importer) : root;
158
+ const sideEffects = await getSideEffects(originalFilePath, isExternal);
159
+ const result = {
160
+ path: isExternal ? args.path : getResultPath(originalFilePath, dir, args.kind),
161
+ external: isExternal,
162
+ namespace: isExternal ? void 0 : "file",
163
+ sideEffects,
164
+ suffix
165
+ };
166
+ (0, import_debug.debugResolve)("onResolve args:", args);
167
+ (0, import_debug.debugResolve)("onResolve result:", result);
168
+ return result;
169
+ });
170
+ build.onLoad({
171
+ filter: /.*/
172
+ }, async (args) => {
173
+ if (args.namespace === globalNamespace) {
174
+ const value = config.umdGlobals[args.path];
175
+ return {
176
+ contents: `module.exports = (typeof globalThis !== "undefined" ? globalThis : Function('return this')() || global || self)[${JSON.stringify(value)}]`
177
+ };
178
+ }
179
+ if (args.suffix) {
180
+ args.path += args.suffix;
181
+ }
182
+ if (args.namespace !== "file") {
183
+ return;
184
+ }
185
+ compiler.addWatchFile(args.path);
186
+ let result = await compiler.hooks.load.promise(args);
187
+ if (!result) {
188
+ if (DATAURL_JAVASCRIPT_PATTERNS.test(args.path)) {
189
+ return;
190
+ }
191
+ result = {
192
+ contents: await import_utils.fs.promises.readFile(args.path)
193
+ };
194
+ }
195
+ if (!result.contents || result.loader === "copy") {
196
+ return result;
197
+ }
198
+ const context2 = compiler.getTransformContext(args.path);
199
+ context2.addSourceMap(context2.genPluginId("adapter"), result.map);
200
+ const transformResult = await compiler.hooks.transform.promise({
201
+ code: result.contents.toString(),
202
+ path: args.path,
203
+ loader: result.loader
204
+ });
205
+ const ext = (0, import_path.extname)(args.path);
206
+ var _transformResult_loader;
207
+ const loader = (_transformResult_loader = transformResult.loader) !== null && _transformResult_loader !== void 0 ? _transformResult_loader : import_loader.loaderMap[ext];
208
+ const inlineSourceMap = context2.getInlineSourceMap();
209
+ var _result_resolveDir;
210
+ return {
211
+ contents: transformResult.code + inlineSourceMap,
212
+ loader,
213
+ resolveDir: (_result_resolveDir = result.resolveDir) !== null && _result_resolveDir !== void 0 ? _result_resolveDir : (0, import_path.dirname)(args.path)
214
+ };
215
+ });
216
+ build.onEnd(async (result) => {
217
+ if (result.errors.length) {
218
+ return;
219
+ }
220
+ const { outputs } = result.metafile;
221
+ const needSourceMap = Boolean(config.sourceMap);
222
+ for (const [key, value] of Object.entries(outputs)) {
223
+ var _result_outputFiles, _result_outputFiles1;
224
+ if (key.endsWith(".map")) {
225
+ continue;
226
+ }
227
+ const absPath = (0, import_path.resolve)(root, key);
228
+ const item = (_result_outputFiles = result.outputFiles) === null || _result_outputFiles === void 0 ? void 0 : _result_outputFiles.find((x) => x.path === absPath);
229
+ const mapping = (_result_outputFiles1 = result.outputFiles) === null || _result_outputFiles1 === void 0 ? void 0 : _result_outputFiles1.find((x) => x.path.endsWith(".map") && x.path.replace(/\.map$/, "") === absPath);
230
+ if (!item) {
231
+ continue;
232
+ }
233
+ if (absPath.endsWith(".js")) {
234
+ compiler.emitAsset(absPath, {
235
+ type: "chunk",
236
+ contents: item.text,
237
+ map: (0, import_utils2.normalizeSourceMap)(mapping === null || mapping === void 0 ? void 0 : mapping.text, {
238
+ needSourceMap
239
+ }),
240
+ fileName: absPath,
241
+ entryPoint: value === null || value === void 0 ? void 0 : value.entryPoint
242
+ });
243
+ } else {
244
+ compiler.emitAsset(absPath, {
245
+ type: "asset",
246
+ contents: Buffer.from(item.contents),
247
+ fileName: absPath,
248
+ entryPoint: value === null || value === void 0 ? void 0 : value.entryPoint
249
+ });
250
+ }
251
+ }
252
+ for (const [key, value] of compiler.outputChunk.entries()) {
253
+ const context2 = compiler.getSourcemapContext(value.fileName);
254
+ if (value.type === "chunk" && config.sourceMap) {
255
+ context2.addSourceMap(context2.genPluginId("adapter"), value.map);
256
+ }
257
+ const processedResult = await compiler.hooks.renderChunk.promise(value);
258
+ if (processedResult.type === "chunk" && config.sourceMap) {
259
+ processedResult.map = context2.getSourceMap();
260
+ }
261
+ compiler.outputChunk.set(key, processedResult);
262
+ }
263
+ if (config.metafile) {
264
+ const now = Date.now();
265
+ compiler.emitAsset((0, import_path.resolve)(config.outDir, `metafile-${now}.json`), JSON.stringify(result.metafile, null, 2));
266
+ }
267
+ await (0, import_write_file.writeFile)(compiler);
268
+ });
269
+ }
270
+ };
271
+ };
272
+ // Annotate the CommonJS export names for ESM import in node:
273
+ 0 && (module.exports = {
274
+ adapterPlugin
275
+ });
@@ -0,0 +1,4 @@
1
+ import * as tapable from 'tapable';
2
+ import { Source, Chunk, ICompiler } from '../../types';
3
+ export declare function createTransformHook(compiler: ICompiler): tapable.AsyncSeriesWaterfallHook<Source, tapable.UnsetAdditionalOptions>;
4
+ export declare function createRenderChunkHook(compiler: ICompiler): tapable.AsyncSeriesWaterfallHook<Chunk, tapable.UnsetAdditionalOptions>;
@@ -0,0 +1,103 @@
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 hook_exports = {};
30
+ __export(hook_exports, {
31
+ createRenderChunkHook: () => createRenderChunkHook,
32
+ createTransformHook: () => createTransformHook
33
+ });
34
+ module.exports = __toCommonJS(hook_exports);
35
+ var import_util = require("util");
36
+ var tapable = __toESM(require("tapable"));
37
+ function createTransformHook(compiler) {
38
+ const hook = new tapable.AsyncSeriesWaterfallHook([
39
+ "args"
40
+ ]);
41
+ if (!compiler.config.transformCache) {
42
+ return hook;
43
+ }
44
+ const originTapPromise = hook.tapPromise;
45
+ let hook_fn_id = 1;
46
+ const cacheFn = (options, fn) => {
47
+ fn.id = hook_fn_id++;
48
+ originTapPromise.call(hook, options, async (args) => {
49
+ const originCode = args.code;
50
+ const { id } = fn;
51
+ const context = compiler.getTransformContext(args.path);
52
+ const cache = context.getValidCache(id, args.code);
53
+ if (cache) {
54
+ return cache;
55
+ }
56
+ delete args.map;
57
+ const result = await fn(args);
58
+ context.addTransformResult(id, {
59
+ ...result,
60
+ originCode
61
+ });
62
+ return result;
63
+ });
64
+ };
65
+ hook.tap = cacheFn;
66
+ hook.tapPromise = cacheFn;
67
+ return hook;
68
+ }
69
+ function createRenderChunkHook(compiler) {
70
+ const hook = new tapable.AsyncSeriesWaterfallHook([
71
+ "chunk"
72
+ ]);
73
+ if (!compiler.context.config.sourceMap) {
74
+ return hook;
75
+ }
76
+ const originTapPromise = hook.tapPromise;
77
+ let hook_fn_id = 1;
78
+ const wrapper = (options, fn) => {
79
+ fn.id = hook_fn_id++;
80
+ originTapPromise.call(hook, options, async (chunk) => {
81
+ const context = compiler.getSourcemapContext(chunk.fileName);
82
+ if (chunk.type === "chunk") {
83
+ delete chunk.map;
84
+ }
85
+ const result = await fn(chunk);
86
+ if (chunk.type === "chunk") {
87
+ context.addSourceMap(fn.id, result.map);
88
+ }
89
+ return result;
90
+ });
91
+ };
92
+ hook.tap = wrapper;
93
+ hook.tapPromise = wrapper;
94
+ hook.tapAsync = function(options, fn) {
95
+ wrapper(options, (0, import_util.promisify)(fn));
96
+ };
97
+ return hook;
98
+ }
99
+ // Annotate the CommonJS export names for ESM import in node:
100
+ 0 && (module.exports = {
101
+ createRenderChunkHook,
102
+ createTransformHook
103
+ });
@@ -0,0 +1,35 @@
1
+ import { BuildResult, BuildOptions, BuildContext, ImportKind } from 'esbuild';
2
+ import { FSWatcher } from '@modern-js/utils';
3
+ import { BaseBuildConfig, BuilderHooks, Chunk, ICompiler, ModuleTools, PluginAPI, Context, HookList } from '../../types';
4
+ import { TransformContext } from './transform';
5
+ import { SourcemapContext } from './sourcemap';
6
+ export declare class EsbuildCompiler implements ICompiler {
7
+ instance?: BuildContext;
8
+ result?: BuildResult;
9
+ hookList?: HookList;
10
+ reBuildCount: number;
11
+ buildOptions: BuildOptions;
12
+ context: Context;
13
+ config: BaseBuildConfig;
14
+ hooks: BuilderHooks;
15
+ api: PluginAPI<ModuleTools>;
16
+ outputChunk: Map<string, Chunk>;
17
+ watchedFiles: Set<string>;
18
+ css_resolve: (id: string, dir: string) => string;
19
+ node_resolve: (id: string, dir: string, kind: ImportKind) => string;
20
+ watcher?: FSWatcher;
21
+ virtualModule: Map<string, string>;
22
+ private transformContextMap;
23
+ private sourcemapContextMap;
24
+ constructor(context: Context);
25
+ init(): Promise<void>;
26
+ addWatchFile(id: string): void;
27
+ convertConfigToEsbuild(config: BaseBuildConfig): BuildOptions;
28
+ getTransformContext(path: string): TransformContext;
29
+ getSourcemapContext(path: string): SourcemapContext;
30
+ emitAsset(name: string, chunk: string | Chunk): void;
31
+ close(): Promise<void>;
32
+ build(): Promise<void>;
33
+ reBuild(type: 'link' | 'change', config: BaseBuildConfig): Promise<void>;
34
+ }
35
+ export declare const createCompiler: (context: Context) => Promise<EsbuildCompiler>;
@@ -0,0 +1,268 @@
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 esbuild_exports = {};
30
+ __export(esbuild_exports, {
31
+ EsbuildCompiler: () => EsbuildCompiler,
32
+ createCompiler: () => createCompiler
33
+ });
34
+ module.exports = __toCommonJS(esbuild_exports);
35
+ var import_define_property = require("@swc/helpers/_/_define_property");
36
+ var import_esbuild = require("esbuild");
37
+ var tapable = __toESM(require("tapable"));
38
+ var import_utils = require("@modern-js/utils");
39
+ var import_utils2 = require("../../utils");
40
+ var import_feature = require("../feature");
41
+ var import_build = require("../../constants/build");
42
+ var import_adapter = require("./adapter");
43
+ var import_transform = require("./transform");
44
+ var import_sourcemap = require("./sourcemap");
45
+ var import_hook = require("./hook");
46
+ var import_resolve = require("./resolve");
47
+ var import_watch = require("./watch");
48
+ class EsbuildCompiler {
49
+ async init() {
50
+ if (this.context.watch) {
51
+ (0, import_watch.initWatcher)(this);
52
+ }
53
+ const internal = await (0, import_feature.getInternalList)(this.context);
54
+ const user = this.config.hooks;
55
+ this.hookList = [
56
+ ...user,
57
+ ...internal
58
+ ];
59
+ await Promise.all(this.hookList.map((item) => item.apply(this)));
60
+ }
61
+ addWatchFile(id) {
62
+ if (!this.watchedFiles.has(id)) {
63
+ var _this_watcher_add, _this_watcher;
64
+ (_this_watcher = this.watcher) === null || _this_watcher === void 0 ? void 0 : (_this_watcher_add = _this_watcher.add) === null || _this_watcher_add === void 0 ? void 0 : _this_watcher_add.call(_this_watcher, id);
65
+ this.watchedFiles.add(id);
66
+ }
67
+ }
68
+ convertConfigToEsbuild(config) {
69
+ const { input, buildType, define, target, sourceMap, platform, splitting, outDir, sourceDir, minify, jsx, esbuildOptions, format, asset, tsconfig, banner, footer } = config;
70
+ const bundle = buildType === "bundle";
71
+ const entryNames = bundle ? "[name]" : "[dir]/[name]";
72
+ const absWorkingDir = this.context.root;
73
+ let esbuildFormat = format === "umd" ? "esm" : format;
74
+ if (bundle && format === "cjs" && splitting) {
75
+ esbuildFormat = "esm";
76
+ }
77
+ const esbuildTarget = target === "es5" ? void 0 : target;
78
+ const jsExtensions = [
79
+ ".jsx",
80
+ ".tsx",
81
+ ".js",
82
+ ".ts",
83
+ ".json"
84
+ ];
85
+ const buildOptions = {
86
+ banner: import_utils.lodash.pick(banner, [
87
+ "js",
88
+ "css"
89
+ ]),
90
+ footer: import_utils.lodash.pick(footer, [
91
+ "js",
92
+ "css"
93
+ ]),
94
+ entryPoints: input,
95
+ metafile: true,
96
+ define,
97
+ bundle: buildType === "bundle",
98
+ format: esbuildFormat,
99
+ target: esbuildTarget,
100
+ sourcemap: sourceMap ? "external" : false,
101
+ resolveExtensions: jsExtensions,
102
+ splitting,
103
+ charset: "utf8",
104
+ logLimit: 5,
105
+ absWorkingDir,
106
+ platform,
107
+ tsconfig: import_utils.fs.existsSync(tsconfig) ? tsconfig : void 0,
108
+ write: false,
109
+ logLevel: "silent",
110
+ outdir: outDir,
111
+ outbase: sourceDir,
112
+ entryNames,
113
+ plugins: [
114
+ (0, import_adapter.adapterPlugin)(this)
115
+ ],
116
+ minify: minify === "esbuild",
117
+ jsx,
118
+ supported: {
119
+ "dynamic-import": buildType === "bundle" || format !== "cjs"
120
+ },
121
+ assetNames: `${asset.path}/[name].[hash]`
122
+ };
123
+ return esbuildOptions(buildOptions);
124
+ }
125
+ getTransformContext(path) {
126
+ if (this.transformContextMap.has(path)) {
127
+ return this.transformContextMap.get(path);
128
+ }
129
+ const context2 = new import_transform.TransformContext(Boolean(this.context.config.sourceMap));
130
+ this.transformContextMap.set(path, context2);
131
+ return context2;
132
+ }
133
+ getSourcemapContext(path) {
134
+ if (this.sourcemapContextMap.has(path)) {
135
+ return this.sourcemapContextMap.get(path);
136
+ }
137
+ const context2 = new import_sourcemap.SourcemapContext(Boolean(this.context.config.sourceMap));
138
+ this.sourcemapContextMap.set(path, context2);
139
+ return context2;
140
+ }
141
+ emitAsset(name, chunk) {
142
+ if (typeof chunk === "string") {
143
+ this.outputChunk.set(name, {
144
+ type: "asset",
145
+ contents: chunk,
146
+ fileName: name
147
+ });
148
+ } else {
149
+ this.outputChunk.set(name, chunk);
150
+ }
151
+ }
152
+ async close() {
153
+ try {
154
+ var _this_instance, _this_instance1;
155
+ await ((_this_instance = this.instance) === null || _this_instance === void 0 ? void 0 : _this_instance.cancel());
156
+ await ((_this_instance1 = this.instance) === null || _this_instance1 === void 0 ? void 0 : _this_instance1.dispose());
157
+ } catch (err) {
158
+ }
159
+ }
160
+ async build() {
161
+ const { buildOptions, context: { watch } } = this;
162
+ try {
163
+ if (watch) {
164
+ this.instance = await (0, import_esbuild.context)(buildOptions);
165
+ this.result = await this.instance.rebuild();
166
+ } else {
167
+ this.result = await (0, import_esbuild.build)(buildOptions);
168
+ }
169
+ if (this.result.warnings.length) {
170
+ const warnings = this.result.warnings.filter((warning) => {
171
+ if (warning.text.includes(`This call to "require" will not be bundled because`) || warning.text.includes(`Indirect calls to "require" will not be bundled`) || warning.text.includes(`Converting "require" to "esm" is currently not supported`)) {
172
+ return false;
173
+ }
174
+ return true;
175
+ });
176
+ const messages = await (0, import_esbuild.formatMessages)(warnings, {
177
+ kind: "warning"
178
+ });
179
+ messages.forEach((m) => {
180
+ import_utils.logger.warn(m);
181
+ });
182
+ }
183
+ } catch (error) {
184
+ if (watch) {
185
+ var _this_instance;
186
+ (_this_instance = this.instance) === null || _this_instance === void 0 ? void 0 : _this_instance.cancel();
187
+ import_utils.logger.error(error);
188
+ } else {
189
+ throw error;
190
+ }
191
+ }
192
+ }
193
+ async reBuild(type, config) {
194
+ const { instance } = this;
195
+ try {
196
+ const start = Date.now();
197
+ if (type === "link") {
198
+ await this.build();
199
+ } else {
200
+ this.result = await (instance === null || instance === void 0 ? void 0 : instance.rebuild());
201
+ }
202
+ const time = import_utils.chalk.gray(`(${Date.now() - start}ms)`);
203
+ import_utils.logger.success((0, import_utils2.withLogTitle)(config.buildType, `Build ${config.format},${config.target} files ${time}`));
204
+ } catch (error) {
205
+ import_utils.logger.error(error);
206
+ }
207
+ }
208
+ constructor(context2) {
209
+ (0, import_define_property._)(this, "instance", void 0);
210
+ (0, import_define_property._)(this, "result", void 0);
211
+ (0, import_define_property._)(this, "hookList", void 0);
212
+ (0, import_define_property._)(this, "reBuildCount", void 0);
213
+ (0, import_define_property._)(this, "buildOptions", void 0);
214
+ (0, import_define_property._)(this, "context", void 0);
215
+ (0, import_define_property._)(this, "config", void 0);
216
+ (0, import_define_property._)(this, "hooks", void 0);
217
+ (0, import_define_property._)(this, "api", void 0);
218
+ (0, import_define_property._)(this, "outputChunk", /* @__PURE__ */ new Map());
219
+ (0, import_define_property._)(this, "watchedFiles", /* @__PURE__ */ new Set());
220
+ (0, import_define_property._)(this, "css_resolve", void 0);
221
+ (0, import_define_property._)(this, "node_resolve", void 0);
222
+ (0, import_define_property._)(this, "watcher", void 0);
223
+ (0, import_define_property._)(this, "virtualModule", /* @__PURE__ */ new Map());
224
+ (0, import_define_property._)(this, "transformContextMap", /* @__PURE__ */ new Map());
225
+ (0, import_define_property._)(this, "sourcemapContextMap", /* @__PURE__ */ new Map());
226
+ const { api, config, root } = context2;
227
+ this.reBuildCount = 0;
228
+ this.context = context2;
229
+ this.api = api;
230
+ this.config = config;
231
+ this.buildOptions = this.convertConfigToEsbuild(config);
232
+ this.hooks = Object.freeze({
233
+ load: new tapable.AsyncSeriesBailHook([
234
+ "loadArgs"
235
+ ]),
236
+ transform: (0, import_hook.createTransformHook)(this),
237
+ renderChunk: (0, import_hook.createRenderChunkHook)(this)
238
+ });
239
+ const resolveOptions = {
240
+ root,
241
+ platform: config.platform,
242
+ alias: config.alias,
243
+ tsconfig: config.tsconfig,
244
+ mainFields: config.resolve.mainFields
245
+ };
246
+ this.css_resolve = (0, import_resolve.createResolver)({
247
+ ...resolveOptions,
248
+ resolveType: "css",
249
+ extensions: import_build.cssExtensions,
250
+ preferRelative: true
251
+ });
252
+ this.node_resolve = (0, import_resolve.createResolver)({
253
+ ...resolveOptions,
254
+ resolveType: "js",
255
+ extensions: config.resolve.jsExtensions
256
+ });
257
+ }
258
+ }
259
+ const createCompiler = async (context2) => {
260
+ const compiler = new EsbuildCompiler(context2);
261
+ await compiler.init();
262
+ return compiler;
263
+ };
264
+ // Annotate the CommonJS export names for ESM import in node:
265
+ 0 && (module.exports = {
266
+ EsbuildCompiler,
267
+ createCompiler
268
+ });