@modern-js/module-tools 2.57.0 → 2.58.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.
@@ -215,8 +215,8 @@ const adapterPlugin = (compiler) => {
215
215
  loader: result.loader
216
216
  });
217
217
  const ext = (0, import_path.extname)(args.path);
218
- var _transformResult_loader;
219
- const loader = (_transformResult_loader = transformResult.loader) !== null && _transformResult_loader !== void 0 ? _transformResult_loader : import_loader.loaderMap[ext];
218
+ var _transformResult_loader, _ref;
219
+ const loader = (_ref = (_transformResult_loader = transformResult.loader) !== null && _transformResult_loader !== void 0 ? _transformResult_loader : compiler.config.loader[ext]) !== null && _ref !== void 0 ? _ref : import_loader.loaderMap[ext];
220
220
  const inlineSourceMap = context2.getInlineSourceMap();
221
221
  var _result_resolveDir;
222
222
  return {
@@ -51,10 +51,12 @@ class EsbuildCompiler {
51
51
  (0, import_watch.initWatcher)(this);
52
52
  }
53
53
  const internal = await (0, import_feature.getInternalList)(this.context);
54
- const user = this.config.hooks;
54
+ const before = this.config.hooks.filter((hook) => !hook.applyAfterBuiltIn);
55
+ const after = this.config.hooks.filter((hook) => hook.applyAfterBuiltIn);
55
56
  this.hookList = [
56
- ...user,
57
- ...internal
57
+ ...before,
58
+ ...internal,
59
+ ...after
58
60
  ];
59
61
  await Promise.all(this.hookList.map((item) => item.apply(this)));
60
62
  }
@@ -40,6 +40,22 @@ var import_file = require("../../constants/file");
40
40
  var import_utils2 = require("../../utils");
41
41
  var import_asset = require("./asset");
42
42
  var import_postcssTransformer = require("./style/postcssTransformer");
43
+ var PathType;
44
+ (function(PathType2) {
45
+ PathType2[PathType2["Absolute"] = 0] = "Absolute";
46
+ PathType2[PathType2["Relative"] = 1] = "Relative";
47
+ PathType2[PathType2["ModuleId"] = 2] = "ModuleId";
48
+ })(PathType || (PathType = {}));
49
+ function getTypeOfPath(path, compiler) {
50
+ const isSupportModuleIdAlias = Object.keys(compiler.config.resolve.alias).length > 0;
51
+ if ((0, import_path.isAbsolute)(path)) {
52
+ return 0;
53
+ }
54
+ if (!path.startsWith(".") && isSupportModuleIdAlias) {
55
+ return 2;
56
+ }
57
+ return 1;
58
+ }
43
59
  async function redirectImport(compiler, code, modules, aliasRecord, filePath, outputDir, jsExtension, isModule, matchPath) {
44
60
  const str = new import_magic_string.default(code);
45
61
  const extensions = [
@@ -70,10 +86,15 @@ async function redirectImport(compiler, code, modules, aliasRecord, filePath, ou
70
86
  }
71
87
  }
72
88
  if (absoluteImportPath) {
73
- const relativePath = (0, import_path.relative)((0, import_path.dirname)(filePath), absoluteImportPath);
74
- const relativeImportPath = (0, import_utils2.normalizeSlashes)(relativePath.startsWith("..") ? relativePath : `./${relativePath}`);
75
- str.overwrite(start, end, relativeImportPath);
76
- name2 = relativeImportPath;
89
+ if (getTypeOfPath(absoluteImportPath, compiler) === 2) {
90
+ str.overwrite(start, end, absoluteImportPath);
91
+ name2 = absoluteImportPath;
92
+ } else {
93
+ const relativePath = (0, import_path.relative)((0, import_path.dirname)(filePath), absoluteImportPath);
94
+ const relativeImportPath = (0, import_utils2.normalizeSlashes)(relativePath.startsWith("..") ? relativePath : `./${relativePath}`);
95
+ str.overwrite(start, end, relativeImportPath);
96
+ name2 = relativeImportPath;
97
+ }
77
98
  }
78
99
  }
79
100
  if (redirect2.autoExtension) {
@@ -158,13 +179,14 @@ const redirect = {
158
179
  return args;
159
180
  }
160
181
  const { code, path: id } = args;
161
- const { format, alias, sourceDir, outDir, autoExtension } = compiler.config;
182
+ const { format, sourceDir, outDir, autoExtension } = compiler.config;
162
183
  const { root } = compiler.context;
163
184
  if (!code || format === "iife" || format === "umd") {
164
185
  return args;
165
186
  }
187
+ const alias = Object.keys(compiler.config.resolve.alias).length > 0 ? compiler.config.resolve.alias : compiler.config.alias;
166
188
  const absoluteAlias = Object.entries(alias).reduce((result, [name2, target]) => {
167
- if (!(0, import_path.isAbsolute)(target)) {
189
+ if (getTypeOfPath(target, compiler) === 1) {
168
190
  result[name2] = (0, import_path.resolve)(compiler.context.root, target);
169
191
  } else {
170
192
  result[name2] = target;
@@ -24,6 +24,7 @@ __export(swc_exports, {
24
24
  module.exports = __toCommonJS(swc_exports);
25
25
  var import_path = require("path");
26
26
  var import_swc_plugins = require("@modern-js/swc-plugins");
27
+ var import_loader = require("../../constants/loader");
27
28
  var import_utils = require("../../utils");
28
29
  const name = "swc:transform";
29
30
  const getSwcTarget = (target) => {
@@ -67,13 +68,16 @@ const swcTransform = (userTsconfig) => ({
67
68
  } else {
68
69
  useDefineForClassFields = true;
69
70
  }
70
- const { transformImport, transformLodash, externalHelpers } = compiler.config;
71
+ const { transformImport, transformLodash, externalHelpers, loader: userLoader } = compiler.config;
71
72
  compiler.hooks.transform.tapPromise({
72
73
  name
73
74
  }, async (source) => {
74
75
  const { path } = source;
75
76
  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
+ const ext = (0, import_path.extname)(path);
78
+ var _source_loader, _ref;
79
+ const loader = (_ref = (_source_loader = source.loader) !== null && _source_loader !== void 0 ? _source_loader : userLoader[ext]) !== null && _ref !== void 0 ? _ref : import_loader.loaderMap[ext];
80
+ const enableJsx = loader === "tsx" || loader === "jsx";
77
81
  if ((0, import_utils.isJsExt)(path) || (0, import_utils.isJsLoader)(source.loader)) {
78
82
  const { target, jsx } = compiler.config;
79
83
  const swcCompilerOptions = {
@@ -36,7 +36,7 @@ var import_utils = require("@modern-js/utils");
36
36
  var import_utils2 = require("../utils");
37
37
  var import_build = require("../constants/build");
38
38
  const mergeDefaultBaseConfig = async (pConfig, options) => {
39
- var _pConfig_resolve, _pConfig_resolve1, _pConfig_style, _pConfig_style1, _pConfig_style2, _pConfig_style3;
39
+ var _pConfig_resolve, _pConfig_resolve1, _pConfig_resolve2, _pConfig_style, _pConfig_style1, _pConfig_style2, _pConfig_style3;
40
40
  const defaultConfig = (0, import_build.getDefaultBuildConfig)();
41
41
  const { context, buildCmdOptions } = options;
42
42
  const { applyOptionsChain, ensureAbsolutePath, slash } = await Promise.resolve().then(() => __toESM(require("@modern-js/utils")));
@@ -45,6 +45,7 @@ const mergeDefaultBaseConfig = async (pConfig, options) => {
45
45
  "@": context.srcDirectory
46
46
  };
47
47
  const mergedAlias = applyOptionsChain(defaultAlias, pConfig.alias);
48
+ const mergedResolveAlias = applyOptionsChain({}, (_pConfig_resolve = pConfig.resolve) === null || _pConfig_resolve === void 0 ? void 0 : _pConfig_resolve.alias);
48
49
  const alias = Object.keys(mergedAlias).reduce((prev, name) => {
49
50
  const formattedValue = (value2) => {
50
51
  if (typeof value2 === "string" && value2.startsWith(".")) {
@@ -115,13 +116,15 @@ const mergeDefaultBaseConfig = async (pConfig, options) => {
115
116
  ];
116
117
  var _pConfig_resolve_mainFields, _pConfig_resolve_jsExtensions;
117
118
  const resolve = {
118
- mainFields: (_pConfig_resolve_mainFields = (_pConfig_resolve = pConfig.resolve) === null || _pConfig_resolve === void 0 ? void 0 : _pConfig_resolve.mainFields) !== null && _pConfig_resolve_mainFields !== void 0 ? _pConfig_resolve_mainFields : defaultMainFields,
119
- jsExtensions: (_pConfig_resolve_jsExtensions = (_pConfig_resolve1 = pConfig.resolve) === null || _pConfig_resolve1 === void 0 ? void 0 : _pConfig_resolve1.jsExtensions) !== null && _pConfig_resolve_jsExtensions !== void 0 ? _pConfig_resolve_jsExtensions : defaultConfig.resolve.jsExtensions
119
+ mainFields: (_pConfig_resolve_mainFields = (_pConfig_resolve1 = pConfig.resolve) === null || _pConfig_resolve1 === void 0 ? void 0 : _pConfig_resolve1.mainFields) !== null && _pConfig_resolve_mainFields !== void 0 ? _pConfig_resolve_mainFields : defaultMainFields,
120
+ jsExtensions: (_pConfig_resolve_jsExtensions = (_pConfig_resolve2 = pConfig.resolve) === null || _pConfig_resolve2 === void 0 ? void 0 : _pConfig_resolve2.jsExtensions) !== null && _pConfig_resolve_jsExtensions !== void 0 ? _pConfig_resolve_jsExtensions : defaultConfig.resolve.jsExtensions,
121
+ alias: mergedResolveAlias
120
122
  };
121
123
  var _pConfig_esbuildOptions;
122
124
  const esbuildOptions = (_pConfig_esbuildOptions = pConfig.esbuildOptions) !== null && _pConfig_esbuildOptions !== void 0 ? _pConfig_esbuildOptions : defaultConfig.esbuildOptions;
123
- var _pConfig_shims, _pConfig_autoExtension, _pConfig_footer, _pConfig_banner, _pConfig_hooks, _pConfig_format, _pConfig_target, _pConfig_sourceMap, _pConfig_copy, _pConfig_outDir, _pConfig_jsx, _pConfig_splitting, _pConfig_minify, _pConfig_umdModuleName, _pConfig_sideEffects, _pConfig_style_inject, _pConfig_style_modules, _pConfig_style_autoModules, _pConfig_style_tailwindcss, _pConfig_externalHelpers, _pConfig_transformCache, _pConfig_transformImport, _pConfig_transformLodash, _pConfig_sourceType, _pConfig_disableSwcTransform;
125
+ var _pConfig_loader, _pConfig_shims, _pConfig_autoExtension, _pConfig_footer, _pConfig_banner, _pConfig_hooks, _pConfig_format, _pConfig_target, _pConfig_sourceMap, _pConfig_copy, _pConfig_outDir, _pConfig_jsx, _pConfig_splitting, _pConfig_minify, _pConfig_umdModuleName, _pConfig_sideEffects, _pConfig_style_inject, _pConfig_style_modules, _pConfig_style_autoModules, _pConfig_style_tailwindcss, _pConfig_externalHelpers, _pConfig_transformCache, _pConfig_transformImport, _pConfig_transformLodash, _pConfig_sourceType, _pConfig_disableSwcTransform;
124
126
  return {
127
+ loader: (_pConfig_loader = pConfig.loader) !== null && _pConfig_loader !== void 0 ? _pConfig_loader : defaultConfig.loader,
125
128
  shims: (_pConfig_shims = pConfig.shims) !== null && _pConfig_shims !== void 0 ? _pConfig_shims : defaultConfig.shims,
126
129
  autoExtension: (_pConfig_autoExtension = pConfig.autoExtension) !== null && _pConfig_autoExtension !== void 0 ? _pConfig_autoExtension : defaultConfig.autoExtension,
127
130
  footer: (_pConfig_footer = pConfig.footer) !== null && _pConfig_footer !== void 0 ? _pConfig_footer : defaultConfig.footer,
@@ -44,6 +44,7 @@ const validPartialBuildConfig = (config, appDirectory) => {
44
44
  }
45
45
  };
46
46
  const validBuildConfig = (config, appDirectory) => {
47
+ var _config_resolve;
47
48
  var _config_format;
48
49
  if (config.buildType === "bundleless" && [
49
50
  "iife",
@@ -54,6 +55,9 @@ const validBuildConfig = (config, appDirectory) => {
54
55
  if (config.tsconfig && !import_utils.fs.existsSync(import_path.default.resolve(appDirectory, config.tsconfig))) {
55
56
  throw new Error(`${config.tsconfig} does not exist in your project`);
56
57
  }
58
+ if (config.alias && ((_config_resolve = config.resolve) === null || _config_resolve === void 0 ? void 0 : _config_resolve.alias)) {
59
+ throw new Error("alias and resolve.alias are not allowed to be used together, alias will be deprecated in the future, please use resolve.alias instead");
60
+ }
57
61
  };
58
62
  // Annotate the CommonJS export names for ESM import in node:
59
63
  0 && (module.exports = {
@@ -57,6 +57,7 @@ const getDefaultBuildConfig = () => {
57
57
  "src/index.ts"
58
58
  ],
59
59
  jsx: "automatic",
60
+ loader: {},
60
61
  metafile: false,
61
62
  minify: false,
62
63
  outDir: "./dist",
@@ -78,7 +79,8 @@ const getDefaultBuildConfig = () => {
78
79
  ".js",
79
80
  ".ts",
80
81
  ".json"
81
- ]
82
+ ],
83
+ alias: {}
82
84
  },
83
85
  shims: false,
84
86
  sideEffects: void 0,
@@ -15,6 +15,7 @@ export * from './copy';
15
15
  export type HookList = {
16
16
  name: string;
17
17
  apply: (compiler: ICompiler) => void;
18
+ applyAfterBuiltIn?: boolean;
18
19
  }[];
19
20
  export type EsbuildOptions = (options: BuildOptions) => BuildOptions;
20
21
  export type BuildType = 'bundleless' | 'bundle';
@@ -93,6 +94,12 @@ export type AliasOption = Record<string, string> | ((aliases: Record<string, str
93
94
  export type Resolve = {
94
95
  mainFields?: string[];
95
96
  jsExtensions?: string[];
97
+ alias?: AliasOption;
98
+ };
99
+ export type ResolveOptions = {
100
+ mainFields: string[];
101
+ jsExtensions: string[];
102
+ alias: Record<string, string>;
96
103
  };
97
104
  export type BaseBuildConfig = Omit<Required<PartialBaseBuildConfig>, 'dts' | 'style' | 'alias' | 'sideEffects' | 'asset' | 'resolve'> & {
98
105
  sideEffects?: SideEffects;
@@ -100,9 +107,13 @@ export type BaseBuildConfig = Omit<Required<PartialBaseBuildConfig>, 'dts' | 'st
100
107
  style: Style;
101
108
  alias: Record<string, string>;
102
109
  asset: Required<Asset>;
103
- resolve: Required<Resolve>;
110
+ resolve: ResolveOptions;
104
111
  };
105
112
  export type PartialBaseBuildConfig = {
113
+ /**
114
+ * @experimental
115
+ */
116
+ loader?: Record<string, string>;
106
117
  shims?: boolean;
107
118
  autoExtension?: boolean;
108
119
  resolve?: Resolve;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@modern-js/module-tools",
3
- "version": "2.57.0",
3
+ "version": "2.58.0",
4
4
  "description": "Simple, powerful, high-performance modern npm package development solution.",
5
5
  "keywords": [
6
6
  "modern",
@@ -70,22 +70,22 @@
70
70
  "tapable": "2.2.1",
71
71
  "terser": "^5.31.1",
72
72
  "tsconfig-paths-webpack-plugin": "4.1.0",
73
- "@modern-js/core": "2.57.0",
74
- "@modern-js/plugin": "2.57.0",
75
- "@modern-js/plugin-i18n": "2.57.0",
76
- "@modern-js/plugin-changeset": "2.57.0",
77
- "@modern-js/plugin-lint": "2.57.0",
78
- "@modern-js/types": "2.57.0",
79
- "@modern-js/utils": "2.57.0"
73
+ "@modern-js/core": "2.58.0",
74
+ "@modern-js/plugin": "2.58.0",
75
+ "@modern-js/plugin-changeset": "2.58.0",
76
+ "@modern-js/plugin-lint": "2.58.0",
77
+ "@modern-js/plugin-i18n": "2.58.0",
78
+ "@modern-js/types": "2.58.0",
79
+ "@modern-js/utils": "2.58.0"
80
80
  },
81
81
  "devDependencies": {
82
- "@rsbuild/core": "1.0.1-beta.3",
82
+ "@rsbuild/core": "1.0.1-beta.9",
83
83
  "@types/convert-source-map": "1.5.2",
84
84
  "@types/node": "^14",
85
85
  "typescript": "^5",
86
- "@modern-js/self": "npm:@modern-js/module-tools@2.57.0",
87
- "@scripts/build": "2.57.0",
88
- "@scripts/vitest-config": "2.57.0"
86
+ "@modern-js/self": "npm:@modern-js/module-tools@2.58.0",
87
+ "@scripts/build": "2.58.0",
88
+ "@scripts/vitest-config": "2.58.0"
89
89
  },
90
90
  "peerDependencies": {
91
91
  "typescript": "^4 || ^5"