Skip to content

Commit

Permalink
fix: support "..." in importFields (#6363)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk authored May 21, 2024
1 parent 63110e3 commit f2afb4d
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 30 deletions.
8 changes: 4 additions & 4 deletions crates/rspack_binding_options/src/options/raw_resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl TryFrom<RawResolveOptions> for Resolve {
.collect::<Result<ByDependency, Self::Error>>()
})
.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());
Expand All @@ -121,7 +121,7 @@ impl TryFrom<RawResolveOptions> 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());

Expand All @@ -139,14 +139,14 @@ impl TryFrom<RawResolveOptions> 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,
})
}
}
Expand Down
36 changes: 18 additions & 18 deletions crates/rspack_core/src/options/resolve/clever_merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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)
Expand Down Expand Up @@ -62,8 +62,8 @@ struct ResolveWithEntry {
fallback: Entry<Fallback>,
tsconfig: Entry<TsconfigOptions>,
fully_specified: Entry<FullySpecified>,
exports_field: Entry<ExportsField>,
imports_field: Entry<ImportsField>,
exports_fields: Entry<ExportsFields>,
imports_fields: Entry<ImportsFields>,
description_files: Entry<DescriptionFiles>,
enforce_extension: Entry<EnforceExtension>,
extension_alias: Entry<ExtensionAlias>,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions crates/rspack_core/src/options/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ pub(super) type ConditionNames = Vec<String>;
pub(super) type Fallback = Alias;
pub(super) type FullySpecified = bool;
pub(super) type EnforceExtension = bool;
pub(super) type ExportsField = Vec<Vec<String>>;
pub(super) type ImportsField = Vec<Vec<String>>;
pub(super) type ExportsFields = Vec<Vec<String>>;
pub(super) type ImportsFields = Vec<Vec<String>>;
pub(super) type ExtensionAlias = Vec<(String, Vec<String>)>;
pub(super) type Modules = Vec<String>;
pub(super) type Roots = Vec<String>;
Expand Down Expand Up @@ -67,7 +67,7 @@ pub struct Resolve {
pub fully_specified: Option<FullySpecified>,
/// A list of exports fields in descriptions files
/// Default is `[["exports"]]`.
pub exports_field: Option<ExportsField>,
pub exports_fields: Option<ExportsFields>,
/// A list map ext to another.
/// Default is `[]`
pub extension_alias: Option<ExtensionAlias>,
Expand All @@ -76,16 +76,16 @@ pub struct Resolve {
/// A list of directories where requests of server-relative URLs (starting with '/') are resolved
pub roots: Option<Roots>,
/// A list of resolve restrictions to restrict the paths that a request can be resolved on.
pub restrictions: Option<Roots>,
pub restrictions: Option<Restrictions>,
/// 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<ImportsFields>,
/// Configure resolve options by the type of module request.
pub by_dependency: Option<ByDependency>,
/// The JSON files to use for descriptions
/// Default is ["package.json"]
pub description_files: Option<DescriptionFiles>,
/// 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<EnforceExtension>,
/// 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<ImportsField>,
}

/// Tsconfig Options
Expand Down
5 changes: 3 additions & 2 deletions crates/rspack_core/src/resolver/resolver_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -270,6 +270,7 @@ fn to_oxc_resolver_options(
.into_iter()
.map(PathBuf::from)
.collect();

oxc_resolver::ResolveOptions {
fallback,
modules,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,9 @@ Object {
"exports",
],
"extensions": Array [],
"importsFields": Array [
"imports",
],
"mainFields": Array [
"main",
],
Expand Down
1 change: 1 addition & 0 deletions packages/rspack/src/config/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,7 @@ const getResolveDefaults = ({
exportsFields: ["exports"],
roots: [context],
mainFields: ["main"],
importsFields: ["imports"],
byDependency: {
wasm: esmDeps(),
esm: esmDeps(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "pkg",
"imports": {
"#internal": "./pkg.mjs"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'pkg';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
entry: "./index.js",
resolve: {
importsFields: ["hash-start", "..."]
}
};

3 comments on commit f2afb4d

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-05-21 5fbabcf) Current Change
10000_development-mode + exec 2.65 s ± 33 ms 2.61 s ± 39 ms -1.51 %
10000_development-mode_hmr + exec 732 ms ± 11 ms 696 ms ± 5.2 ms -4.92 %
10000_production-mode + exec 2.55 s ± 24 ms 2.45 s ± 47 ms -4.01 %
arco-pro_development-mode + exec 2.54 s ± 87 ms 2.47 s ± 74 ms -2.64 %
arco-pro_development-mode_hmr + exec 436 ms ± 2.9 ms 433 ms ± 2.9 ms -0.80 %
arco-pro_development-mode_hmr_intercept-plugin + exec 445 ms ± 1.7 ms 441 ms ± 2 ms -0.87 %
arco-pro_development-mode_intercept-plugin + exec 3.31 s ± 76 ms 3.29 s ± 55 ms -0.74 %
arco-pro_production-mode + exec 4.06 s ± 134 ms 3.97 s ± 106 ms -2.24 %
arco-pro_production-mode_intercept-plugin + exec 4.9 s ± 54 ms 4.78 s ± 92 ms -2.47 %
threejs_development-mode_10x + exec 1.99 s ± 18 ms 1.97 s ± 17 ms -0.86 %
threejs_development-mode_10x_hmr + exec 768 ms ± 5.2 ms 752 ms ± 3.9 ms -2.15 %
threejs_production-mode_10x + exec 5.28 s ± 17 ms 5.17 s ± 42 ms -2.05 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result

Please sign in to comment.