diff --git a/crates/rspack_binding_options/src/options/raw_resolve.rs b/crates/rspack_binding_options/src/options/raw_resolve.rs index 97c1c13e3e5..bdbb8dd6f25 100644 --- a/crates/rspack_binding_options/src/options/raw_resolve.rs +++ b/crates/rspack_binding_options/src/options/raw_resolve.rs @@ -110,7 +110,7 @@ impl TryFrom for Resolve { .collect::>() }) .transpose()?; - let exports_field = value + let exports_fields = value .exports_fields .map(|v| v.into_iter().map(|s| vec![s]).collect()); let extension_alias = value.extension_alias.map(|v| v.into_iter().collect()); @@ -121,7 +121,7 @@ impl TryFrom for Resolve { let roots = value.roots; let enforce_extension = value.enforce_extension; let description_files = value.description_files; - let imports_field = value + let imports_fields = value .imports_fields .map(|v| v.into_iter().map(|s| vec![s]).collect()); @@ -139,14 +139,14 @@ impl TryFrom for Resolve { fallback, by_dependency, fully_specified, - exports_field, + exports_fields, extension_alias, alias_fields, restrictions, roots, enforce_extension, description_files, - imports_field, + imports_fields, }) } } diff --git a/crates/rspack_core/src/options/resolve/clever_merge.rs b/crates/rspack_core/src/options/resolve/clever_merge.rs index 4fe172c385a..807e408d95e 100644 --- a/crates/rspack_core/src/options/resolve/clever_merge.rs +++ b/crates/rspack_core/src/options/resolve/clever_merge.rs @@ -2,8 +2,8 @@ use hashlink::LinkedHashMap; use super::value_type::{GetValueType, ValueType}; use super::{ - Alias, AliasFields, ConditionNames, DescriptionFiles, EnforceExtension, ExportsField, - ExtensionAlias, Extensions, Fallback, FullySpecified, ImportsField, MainFields, MainFiles, + Alias, AliasFields, ConditionNames, DescriptionFiles, EnforceExtension, ExportsFields, + ExtensionAlias, Extensions, Fallback, FullySpecified, ImportsFields, MainFields, MainFiles, Modules, PreferAbsolute, PreferRelative, Restrictions, Roots, Symlink, TsconfigOptions, }; use super::{ByDependency, DependencyCategoryStr, Resolve}; @@ -30,8 +30,8 @@ fn is_empty(resolve: &Resolve) -> bool { && is_none!(modules) && is_none!(fallback) && is_none!(fully_specified) - && is_none!(exports_field) - && is_none!(imports_field) + && is_none!(exports_fields) + && is_none!(imports_fields) && is_none!(description_files) && is_none!(enforce_extension) && is_none!(extension_alias) @@ -62,8 +62,8 @@ struct ResolveWithEntry { fallback: Entry, tsconfig: Entry, fully_specified: Entry, - exports_field: Entry, - imports_field: Entry, + exports_fields: Entry, + imports_fields: Entry, description_files: Entry, enforce_extension: Entry, extension_alias: Entry, @@ -94,8 +94,8 @@ fn parse_resolve(resolve: Resolve) -> ResolveWithEntry { fallback: entry!(fallback), tsconfig: entry!(tsconfig), fully_specified: entry!(fully_specified), - exports_field: entry!(exports_field), - imports_field: entry!(imports_field), + exports_fields: entry!(exports_fields), + imports_fields: entry!(imports_fields), description_files: entry!(description_files), enforce_extension: entry!(enforce_extension), extension_alias: entry!(extension_alias), @@ -141,8 +141,8 @@ fn parse_resolve(resolve: Resolve) -> ResolveWithEntry { update_by_value!(modules); update_by_value!(fallback); update_by_value!(fully_specified); - update_by_value!(exports_field); - update_by_value!(imports_field); + update_by_value!(exports_fields); + update_by_value!(imports_fields); update_by_value!(description_files); update_by_value!(enforce_extension); update_by_value!(extension_alias); @@ -317,8 +317,8 @@ fn _merge_resolve(first: Resolve, second: Resolve) -> Resolve { ), fallback: merge!(fallback, ValueType::Other, |_| false, extend_alias), alias: merge!(alias, ValueType::Other, |_| false, extend_alias), - exports_field: merge!(exports_field, ValueType::Other, |_| false, |_, b| b), - imports_field: merge!(imports_field, ValueType::Other, |_| false, |_, b| b), + exports_fields: merge!(exports_fields, ValueType::Other, |_| false, |_, b| b), + imports_fields: merge!(imports_fields, ValueType::Other, |_| false, |_, b| b), description_files: merge!( description_files, second.description_files.base.get_value_type(), @@ -366,8 +366,8 @@ fn _merge_resolve(first: Resolve, second: Resolve) -> Resolve { setup_by_values!(modules); setup_by_values!(fallback); setup_by_values!(fully_specified); - setup_by_values!(exports_field); - setup_by_values!(imports_field); + setup_by_values!(exports_fields); + setup_by_values!(imports_fields); setup_by_values!(description_files); setup_by_values!(enforce_extension); setup_by_values!(extension_alias); @@ -399,8 +399,8 @@ fn _merge_resolve(first: Resolve, second: Resolve) -> Resolve { to_resolve!(modules); to_resolve!(fallback); to_resolve!(fully_specified); - to_resolve!(exports_field); - to_resolve!(imports_field); + to_resolve!(exports_fields); + to_resolve!(imports_fields); to_resolve!(description_files); to_resolve!(enforce_extension); to_resolve!(extension_alias); @@ -428,8 +428,8 @@ fn _merge_resolve(first: Resolve, second: Resolve) -> Resolve { modules: result_entry.modules.base, fallback: result_entry.fallback.base, fully_specified: result_entry.fully_specified.base, - exports_field: result_entry.exports_field.base, - imports_field: result_entry.imports_field.base, + exports_fields: result_entry.exports_fields.base, + imports_fields: result_entry.imports_fields.base, description_files: result_entry.description_files.base, enforce_extension: result_entry.enforce_extension.base, extension_alias: result_entry.extension_alias.base, diff --git a/crates/rspack_core/src/options/resolve/mod.rs b/crates/rspack_core/src/options/resolve/mod.rs index a640fa2991d..66a09385f51 100644 --- a/crates/rspack_core/src/options/resolve/mod.rs +++ b/crates/rspack_core/src/options/resolve/mod.rs @@ -22,8 +22,8 @@ pub(super) type ConditionNames = Vec; pub(super) type Fallback = Alias; pub(super) type FullySpecified = bool; pub(super) type EnforceExtension = bool; -pub(super) type ExportsField = Vec>; -pub(super) type ImportsField = Vec>; +pub(super) type ExportsFields = Vec>; +pub(super) type ImportsFields = Vec>; pub(super) type ExtensionAlias = Vec<(String, Vec)>; pub(super) type Modules = Vec; pub(super) type Roots = Vec; @@ -67,7 +67,7 @@ pub struct Resolve { pub fully_specified: Option, /// A list of exports fields in descriptions files /// Default is `[["exports"]]`. - pub exports_field: Option, + pub exports_fields: Option, /// A list map ext to another. /// Default is `[]` pub extension_alias: Option, @@ -76,7 +76,9 @@ pub struct Resolve { /// A list of directories where requests of server-relative URLs (starting with '/') are resolved pub roots: Option, /// A list of resolve restrictions to restrict the paths that a request can be resolved on. - pub restrictions: Option, + pub restrictions: Option, + /// Field names from the description file (usually package.json) which are used to provide internal request of a package (requests starting with # are considered as internal). + pub imports_fields: Option, /// Configure resolve options by the type of module request. pub by_dependency: Option, /// The JSON files to use for descriptions @@ -84,8 +86,6 @@ pub struct Resolve { pub description_files: Option, /// If enforce_extension is set to EnforceExtension::Enabled, resolution will not allow extension-less files. This means require('./foo.js') will resolve, while require('./foo') will not. pub enforce_extension: Option, - /// Fields from package.json which are used to provide the internal requests of a package (requests starting with # are considered internal). - pub imports_field: Option, } /// Tsconfig Options diff --git a/crates/rspack_core/src/resolver/resolver_impl.rs b/crates/rspack_core/src/resolver/resolver_impl.rs index e0b8e40682f..1a70cd03137 100644 --- a/crates/rspack_core/src/resolver/resolver_impl.rs +++ b/crates/rspack_core/src/resolver/resolver_impl.rs @@ -202,7 +202,7 @@ fn to_oxc_resolver_options( .description_files .unwrap_or_else(|| vec!["package.json".to_string()]); let imports_fields = options - .imports_field + .imports_fields .unwrap_or_else(|| vec![vec!["imports".to_string()]]); let extensions = options.extensions.expect("should have extensions"); let alias = options @@ -252,7 +252,7 @@ fn to_oxc_resolver_options( .collect(); let fully_specified = options.fully_specified.unwrap_or_default(); let exports_fields = options - .exports_field + .exports_fields .unwrap_or_else(|| vec![vec!["exports".to_string()]]); let extension_alias = options.extension_alias.unwrap_or_default(); let alias_fields = options @@ -270,6 +270,7 @@ fn to_oxc_resolver_options( .into_iter() .map(PathBuf::from) .collect(); + oxc_resolver::ResolveOptions { fallback, modules, diff --git a/packages/rspack-test-tools/tests/__snapshots__/Defaults.test.js.snap b/packages/rspack-test-tools/tests/__snapshots__/Defaults.test.js.snap index 3f1bfb0aeca..fa7d4c5f9e0 100644 --- a/packages/rspack-test-tools/tests/__snapshots__/Defaults.test.js.snap +++ b/packages/rspack-test-tools/tests/__snapshots__/Defaults.test.js.snap @@ -487,6 +487,9 @@ Object { "exports", ], "extensions": Array [], + "importsFields": Array [ + "imports", + ], "mainFields": Array [ "main", ], diff --git a/packages/rspack/src/config/defaults.ts b/packages/rspack/src/config/defaults.ts index 34fcdf8e185..e2de2bbc547 100644 --- a/packages/rspack/src/config/defaults.ts +++ b/packages/rspack/src/config/defaults.ts @@ -1007,6 +1007,7 @@ const getResolveDefaults = ({ exportsFields: ["exports"], roots: [context], mainFields: ["main"], + importsFields: ["imports"], byDependency: { wasm: esmDeps(), esm: esmDeps(), diff --git a/webpack-test/configCases/resolve-merging/imports-fields/index.js b/webpack-test/configCases/resolve-merging/imports-fields/index.js new file mode 100644 index 00000000000..616dbc039b9 --- /dev/null +++ b/webpack-test/configCases/resolve-merging/imports-fields/index.js @@ -0,0 +1,8 @@ +import d1 from "./pkg.mjs"; +import d2 from "#internal"; +import { d3, d4 } from "lib"; + +it("imports field to resolve to the same", () => { + expect(d2).toBe(d1); + expect(d4).toBe(d3); +}); diff --git a/webpack-test/configCases/resolve-merging/imports-fields/node_modules/lib/index.js b/webpack-test/configCases/resolve-merging/imports-fields/node_modules/lib/index.js new file mode 100644 index 00000000000..1953c3537f5 --- /dev/null +++ b/webpack-test/configCases/resolve-merging/imports-fields/node_modules/lib/index.js @@ -0,0 +1,2 @@ +export { default as d3 } from "./lib.mjs"; +export { default as d4 } from "#internal"; diff --git a/webpack-test/configCases/resolve-merging/imports-fields/node_modules/lib/lib.mjs b/webpack-test/configCases/resolve-merging/imports-fields/node_modules/lib/lib.mjs new file mode 100644 index 00000000000..71c401bf2aa --- /dev/null +++ b/webpack-test/configCases/resolve-merging/imports-fields/node_modules/lib/lib.mjs @@ -0,0 +1 @@ +export default 'lib'; diff --git a/webpack-test/configCases/resolve-merging/imports-fields/node_modules/lib/package.json b/webpack-test/configCases/resolve-merging/imports-fields/node_modules/lib/package.json new file mode 100644 index 00000000000..a085977002b --- /dev/null +++ b/webpack-test/configCases/resolve-merging/imports-fields/node_modules/lib/package.json @@ -0,0 +1,6 @@ +{ + "name": "lib", + "hash-start": { + "#internal": "./lib.mjs" + } +} \ No newline at end of file diff --git a/webpack-test/configCases/resolve-merging/imports-fields/package.json b/webpack-test/configCases/resolve-merging/imports-fields/package.json new file mode 100644 index 00000000000..1843391bb5a --- /dev/null +++ b/webpack-test/configCases/resolve-merging/imports-fields/package.json @@ -0,0 +1,6 @@ +{ + "name": "pkg", + "imports": { + "#internal": "./pkg.mjs" + } +} \ No newline at end of file diff --git a/webpack-test/configCases/resolve-merging/imports-fields/pkg.mjs b/webpack-test/configCases/resolve-merging/imports-fields/pkg.mjs new file mode 100644 index 00000000000..3fa44fe7fa6 --- /dev/null +++ b/webpack-test/configCases/resolve-merging/imports-fields/pkg.mjs @@ -0,0 +1 @@ +export default 'pkg'; diff --git a/webpack-test/configCases/resolve-merging/imports-fields/webpack.config.js b/webpack-test/configCases/resolve-merging/imports-fields/webpack.config.js new file mode 100644 index 00000000000..a1150b90eed --- /dev/null +++ b/webpack-test/configCases/resolve-merging/imports-fields/webpack.config.js @@ -0,0 +1,6 @@ +module.exports = { + entry: "./index.js", + resolve: { + importsFields: ["hash-start", "..."] + } +};