@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,95 @@
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 postcssTransformer_exports = {};
30
+ __export(postcssTransformer_exports, {
31
+ isCssModule: () => isCssModule,
32
+ postcssTransformer: () => postcssTransformer
33
+ });
34
+ module.exports = __toCommonJS(postcssTransformer_exports);
35
+ var import_postcss = __toESM(require("postcss"));
36
+ var import_utils = require("../../../utils");
37
+ var import_postcssUrlPlugin = require("./postcssUrlPlugin");
38
+ const cssLangs = `\\.(css|less|sass|scss)($|\\?)`;
39
+ const cssModuleRE = new RegExp(`\\.module${cssLangs}`);
40
+ const isCssModule = (filePath, autoModules) => {
41
+ return typeof autoModules === "boolean" ? autoModules && cssModuleRE.test(filePath) : autoModules.test(filePath);
42
+ };
43
+ const postcssTransformer = async (css, entryPath, compilation) => {
44
+ const postcssConfig = compilation.config.style.postcss;
45
+ const { plugins = [], processOptions = {} } = postcssConfig;
46
+ const { modules: modulesOption = {}, autoModules = true } = compilation.config.style;
47
+ const { getJSON: userGetJSON } = modulesOption;
48
+ let modules = {};
49
+ const finalPlugins = [
50
+ (0, import_postcssUrlPlugin.postcssUrlPlugin)({
51
+ entryPath,
52
+ compilation
53
+ }),
54
+ ...plugins
55
+ ];
56
+ if (isCssModule(entryPath, autoModules)) {
57
+ finalPlugins.push((await Promise.resolve().then(() => __toESM(require("postcss-modules")))).default({
58
+ generateScopedName(name, filename, _css) {
59
+ const hash = (0, import_utils.getHash)(filename, "utf-8").substring(0, 5);
60
+ return `${name}_${hash}`;
61
+ },
62
+ async resolve(id) {
63
+ return id;
64
+ },
65
+ ...modulesOption,
66
+ getJSON(cssFileName, _modules, outputFileName) {
67
+ if (userGetJSON) {
68
+ userGetJSON(cssFileName, _modules, outputFileName);
69
+ }
70
+ modules = _modules;
71
+ }
72
+ }));
73
+ }
74
+ let loader = "css";
75
+ let { css: code } = await (0, import_postcss.default)(finalPlugins).process(css, {
76
+ from: entryPath,
77
+ ...processOptions
78
+ });
79
+ if (Object.values(modules).length) {
80
+ const normalizedPath = (0, import_utils.normalizeSlashes)(entryPath);
81
+ const key = (0, import_utils.getHash)(code, "utf-8").slice(0, 5);
82
+ compilation.virtualModule.set(key, code);
83
+ code = `import "${normalizedPath}?css_virtual&hash=${key}";export default ${JSON.stringify(modules)}`;
84
+ loader = "js";
85
+ }
86
+ return {
87
+ code,
88
+ loader
89
+ };
90
+ };
91
+ // Annotate the CommonJS export names for ESM import in node:
92
+ 0 && (module.exports = {
93
+ isCssModule,
94
+ postcssTransformer
95
+ });
@@ -0,0 +1,6 @@
1
+ import { Plugin } from 'postcss';
2
+ import { ICompiler } from '../../../types';
3
+ export declare const postcssUrlPlugin: (options: {
4
+ entryPath: string;
5
+ compilation: ICompiler;
6
+ }) => Plugin;
@@ -0,0 +1,58 @@
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 name in all)
8
+ __defProp(target, name, { get: all[name], 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 postcssUrlPlugin_exports = {};
20
+ __export(postcssUrlPlugin_exports, {
21
+ postcssUrlPlugin: () => postcssUrlPlugin
22
+ });
23
+ module.exports = __toCommonJS(postcssUrlPlugin_exports);
24
+ var import_path = require("path");
25
+ var import_asset = require("../asset");
26
+ var import_utils = require("./utils");
27
+ const Processed = Symbol("processed");
28
+ const HTTP_PATTERNS = /^(https?:)?\/\//;
29
+ const DATAURL_PATTERNS = /^data:/;
30
+ const HASH_PATTERNS = /#[^#]+$/;
31
+ const postcssUrlPlugin = (options) => {
32
+ return {
33
+ postcssPlugin: "postcss-url",
34
+ async Declaration(decl) {
35
+ const isProcessed = decl[Processed];
36
+ decl.value = await (0, import_utils.rewriteCssUrls)(decl.value, false, async (URL) => {
37
+ if (URL && !HTTP_PATTERNS.test(URL) && !HASH_PATTERNS.test(URL) && !DATAURL_PATTERNS.test(URL) && !isProcessed) {
38
+ let filePath = URL;
39
+ const { outDir, sourceDir, buildType } = options.compilation.config;
40
+ const { css_resolve } = options.compilation;
41
+ filePath = css_resolve(URL, (0, import_path.dirname)(options.entryPath));
42
+ const rebaseFrom = buildType === "bundle" ? outDir : (0, import_path.join)(outDir, (0, import_path.relative)(sourceDir, (0, import_path.dirname)(options.entryPath)));
43
+ const { contents: fileUrl } = await import_asset.getAssetContents.apply(options.compilation, [
44
+ filePath,
45
+ rebaseFrom
46
+ ]);
47
+ decl[Processed] = true;
48
+ return fileUrl.toString();
49
+ }
50
+ return URL;
51
+ });
52
+ }
53
+ };
54
+ };
55
+ // Annotate the CommonJS export names for ESM import in node:
56
+ 0 && (module.exports = {
57
+ postcssUrlPlugin
58
+ });
@@ -0,0 +1,2 @@
1
+ import { PreprocessRender } from './transformStyle';
2
+ export declare const sassRender: PreprocessRender;
@@ -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,124 @@
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
+ const rebased = await rewriteCssUrls(content, import_path.default.extname(file).slice(1), (url) => {
81
+ if (url.startsWith("/")) {
82
+ return url;
83
+ }
84
+ return resolver(url, fileDir);
85
+ });
86
+ return {
87
+ file,
88
+ contents: rebased
89
+ };
90
+ }
91
+ function rewriteCssUrls(css, type, replacer) {
92
+ return asyncReplace(css, cssUrlRE, async (match) => {
93
+ const matched = match[0];
94
+ let rawUrl = match[1];
95
+ let wrap = "";
96
+ const first = rawUrl[0];
97
+ if (first === `"` || first === `'`) {
98
+ wrap = first;
99
+ rawUrl = rawUrl.slice(1, -1);
100
+ }
101
+ if (type === "less" && rawUrl.startsWith("@") || (type === "sass" || type === "scss") && rawUrl.startsWith("$") || isExternalUrl(rawUrl) || isDataUrl(rawUrl) || rawUrl.startsWith("#")) {
102
+ return matched;
103
+ }
104
+ return `url(${wrap}${(0, import_utils.normalizeSlashes)(await replacer(rawUrl))}${wrap})`;
105
+ });
106
+ }
107
+ async function asyncReplace(input, re, replacer) {
108
+ let match;
109
+ let remaining = input;
110
+ let rewritten = "";
111
+ while (match = re.exec(remaining)) {
112
+ rewritten += remaining.slice(0, match.index);
113
+ rewritten += await replacer(match);
114
+ remaining = remaining.slice(match.index + match[0].length);
115
+ }
116
+ rewritten += remaining;
117
+ return rewritten;
118
+ }
119
+ // Annotate the CommonJS export names for ESM import in node:
120
+ 0 && (module.exports = {
121
+ loadProcessor,
122
+ rebaseUrls,
123
+ rewriteCssUrls
124
+ });
@@ -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
+ };