@modern-js/module-tools 2.57.0 → 2.57.1

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.
@@ -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;
@@ -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,8 +116,9 @@ 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;
@@ -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 = {
@@ -78,7 +78,8 @@ const getDefaultBuildConfig = () => {
78
78
  ".js",
79
79
  ".ts",
80
80
  ".json"
81
- ]
81
+ ],
82
+ alias: {}
82
83
  },
83
84
  shims: false,
84
85
  sideEffects: void 0,
@@ -93,6 +93,12 @@ export type AliasOption = Record<string, string> | ((aliases: Record<string, str
93
93
  export type Resolve = {
94
94
  mainFields?: string[];
95
95
  jsExtensions?: string[];
96
+ alias?: AliasOption;
97
+ };
98
+ export type ResolveOptions = {
99
+ mainFields: string[];
100
+ jsExtensions: string[];
101
+ alias: Record<string, string>;
96
102
  };
97
103
  export type BaseBuildConfig = Omit<Required<PartialBaseBuildConfig>, 'dts' | 'style' | 'alias' | 'sideEffects' | 'asset' | 'resolve'> & {
98
104
  sideEffects?: SideEffects;
@@ -100,7 +106,7 @@ export type BaseBuildConfig = Omit<Required<PartialBaseBuildConfig>, 'dts' | 'st
100
106
  style: Style;
101
107
  alias: Record<string, string>;
102
108
  asset: Required<Asset>;
103
- resolve: Required<Resolve>;
109
+ resolve: ResolveOptions;
104
110
  };
105
111
  export type PartialBaseBuildConfig = {
106
112
  shims?: boolean;
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.57.1",
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.57.1",
74
+ "@modern-js/plugin": "2.57.1",
75
+ "@modern-js/plugin-changeset": "2.57.1",
76
+ "@modern-js/plugin-i18n": "2.57.1",
77
+ "@modern-js/plugin-lint": "2.57.1",
78
+ "@modern-js/types": "2.57.1",
79
+ "@modern-js/utils": "2.57.1"
80
80
  },
81
81
  "devDependencies": {
82
82
  "@rsbuild/core": "1.0.1-beta.3",
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.57.1",
87
+ "@scripts/build": "2.57.1",
88
+ "@scripts/vitest-config": "2.57.1"
89
89
  },
90
90
  "peerDependencies": {
91
91
  "typescript": "^4 || ^5"