Skip to content

Commit

Permalink
alias @swc/helpers to the version within next.js (#3865)
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Feb 17, 2023
1 parent 7682601 commit 6a15aa3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 40 deletions.
37 changes: 3 additions & 34 deletions crates/next-core/src/next_build.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,8 @@
use anyhow::{Context, Result};
use turbo_tasks::Value;
use anyhow::Result;
use turbo_tasks_fs::FileSystemPathVc;
use turbopack::{resolve_options, resolve_options_context::ResolveOptionsContext};
use turbopack_core::{
asset::Asset,
resolve::{
options::{ImportMapping, ImportMappingVc},
parse::RequestVc,
pattern::Pattern,
resolve,
},
};
use turbopack_core::resolve::options::{ImportMapping, ImportMappingVc};

#[turbo_tasks::function]
pub async fn get_next_package(project_root: FileSystemPathVc) -> Result<FileSystemPathVc> {
let result = resolve(
project_root,
RequestVc::parse(Value::new(Pattern::Constant(
"next/package.json".to_string(),
))),
resolve_options(
project_root,
ResolveOptionsContext {
enable_node_modules: true,
enable_node_native_modules: true,
custom_conditions: vec!["development".to_string()],
..Default::default()
}
.cell(),
),
);
let assets = result.primary_assets().await?;
let asset = assets.first().context("Next.js package not found")?;
Ok(asset.path().parent())
}
use crate::next_import_map::get_next_package;

#[turbo_tasks::function]
pub async fn get_postcss_package_mapping(
Expand Down
69 changes: 63 additions & 6 deletions crates/next-core/src/next_import_map.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
use std::collections::{BTreeMap, HashMap};

use anyhow::Result;
use anyhow::{Context, Result};
use turbo_tasks::Value;
use turbo_tasks_fs::{glob::GlobVc, FileSystemPathVc};
use turbopack_core::resolve::{
options::{
ConditionValue, ImportMap, ImportMapVc, ImportMapping, ImportMappingVc, ResolvedMap,
ResolvedMapVc,
use turbopack::{resolve_options, resolve_options_context::ResolveOptionsContext};
use turbopack_core::{
asset::Asset,
resolve::{
options::{
ConditionValue, ImportMap, ImportMapVc, ImportMapping, ImportMappingVc,
ResolveOptionsVc, ResolvedMap, ResolvedMapVc,
},
parse::RequestVc,
pattern::Pattern,
resolve, AliasPattern, ExportsValue, ResolveAliasMapVc,
},
AliasPattern, ExportsValue, ResolveAliasMapVc,
};

use crate::{
Expand Down Expand Up @@ -371,9 +377,60 @@ pub async fn insert_next_shared_aliases(
ImportMapping::Dynamic(NextFontGoogleCssModuleReplacerVc::new(project_path).into()).into(),
);

import_map.insert_wildcard_alias(
"@swc/helpers/",
ImportMapping::PrimaryAlternative(
"./*".to_string(),
Some(get_swc_helpers_package(project_path)),
)
.cell(),
);

Ok(())
}

#[turbo_tasks::function]
fn package_lookup_resolve_options(project_root: FileSystemPathVc) -> ResolveOptionsVc {
resolve_options(
project_root,
ResolveOptionsContext {
enable_node_modules: true,
enable_node_native_modules: true,
custom_conditions: vec!["development".to_string()],
..Default::default()
}
.cell(),
)
}

#[turbo_tasks::function]
pub async fn get_next_package(project_root: FileSystemPathVc) -> Result<FileSystemPathVc> {
let result = resolve(
project_root,
RequestVc::parse(Value::new(Pattern::Constant(
"next/package.json".to_string(),
))),
package_lookup_resolve_options(project_root),
);
let assets = result.primary_assets().await?;
let asset = assets.first().context("Next.js package not found")?;
Ok(asset.path().parent())
}

#[turbo_tasks::function]
pub async fn get_swc_helpers_package(project_root: FileSystemPathVc) -> Result<FileSystemPathVc> {
let result = resolve(
get_next_package(project_root),
RequestVc::parse(Value::new(Pattern::Constant(
"@swc/helpers/package.json".to_string(),
))),
package_lookup_resolve_options(project_root),
);
let assets = result.primary_assets().await?;
let asset = assets.first().context("Next.js package not found")?;
Ok(asset.path().parent())
}

pub async fn insert_alias_option<const N: usize>(
import_map: &mut ImportMap,
project_path: FileSystemPathVc,
Expand Down

1 comment on commit 6a15aa3

@vercel
Copy link

@vercel vercel bot commented on 6a15aa3 Feb 17, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.