Skip to content

Commit

Permalink
add support for type json
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed May 14, 2024
1 parent 3865866 commit 56b6806
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
7 changes: 7 additions & 0 deletions crates/turbopack-core/src/reference_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@ pub enum CommonJsReferenceSubType {
Undefined,
}

#[turbo_tasks::value(serialization = "auto_for_input")]
#[derive(Debug, Clone, PartialOrd, Ord, Hash)]
pub enum ImportWithType {
Json,
}

#[turbo_tasks::value(serialization = "auto_for_input")]
#[derive(Debug, Default, Clone, PartialOrd, Ord, Hash)]
pub enum EcmaScriptModulesReferenceSubType {
ImportPart(Vc<ModulePart>),
Import,
ImportWithType(ImportWithType),
DynamicImport,
Custom(u8),
#[default]
Expand Down
8 changes: 8 additions & 0 deletions crates/turbopack-ecmascript/src/analyzer/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ static ANNOTATION_TRANSITION: Lazy<JsWord> =
static ANNOTATION_CHUNKING_TYPE: Lazy<JsWord> =
Lazy::new(|| crate::annotations::ANNOTATION_CHUNKING_TYPE.into());

/// Changes the type of the resolved module (only "json" is supported currently)
static ATTRIBUTE_MODULE_TYPE: Lazy<JsWord> = Lazy::new(|| "type".into());

impl ImportAnnotations {
pub fn parse(with: Option<&ObjectLit>) -> ImportAnnotations {
let Some(with) = with else {
Expand Down Expand Up @@ -74,6 +77,11 @@ impl ImportAnnotations {
self.get(&ANNOTATION_CHUNKING_TYPE)
}

/// Returns the content on the type attribute
pub fn module_type(&self) -> Option<&str> {
self.get(&ATTRIBUTE_MODULE_TYPE)
}

pub fn get(&self, key: &JsWord) -> Option<&str> {
self.map.get(key).map(|w| w.as_str())
}
Expand Down
15 changes: 9 additions & 6 deletions crates/turbopack-ecmascript/src/references/esm/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use turbopack_core::{
issue::{IssueSeverity, IssueSource},
module::Module,
reference::ModuleReference,
reference_type::EcmaScriptModulesReferenceSubType,
reference_type::{EcmaScriptModulesReferenceSubType, ImportWithType},
resolve::{
origin::{ResolveOrigin, ResolveOriginExt},
parse::Request,
Expand Down Expand Up @@ -142,15 +142,18 @@ impl EsmAssetReference {
impl ModuleReference for EsmAssetReference {
#[turbo_tasks::function]
async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> {
let ty = Value::new(match &self.export_name {
Some(part) => EcmaScriptModulesReferenceSubType::ImportPart(*part),
None => EcmaScriptModulesReferenceSubType::Import,
});
let ty = if matches!(self.annotations.module_type(), Some("json")) {
EcmaScriptModulesReferenceSubType::ImportWithType(ImportWithType::Json)
} else if let Some(part) = &self.export_name {
EcmaScriptModulesReferenceSubType::ImportPart(*part)
} else {
EcmaScriptModulesReferenceSubType::Import
};

Ok(esm_resolve(
self.get_origin().resolve().await?,
self.request,
ty,
Value::new(ty),
IssueSeverity::Error.cell(),
self.issue_source,
))
Expand Down
15 changes: 13 additions & 2 deletions crates/turbopack-resolve/src/ecmascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,25 @@ pub fn get_condition_maps(
}

#[turbo_tasks::function]
pub async fn apply_esm_specific_options(options: Vc<ResolveOptions>) -> Result<Vc<ResolveOptions>> {
pub async fn apply_esm_specific_options(
options: Vc<ResolveOptions>,
reference_type: Value<ReferenceType>,
) -> Result<Vc<ResolveOptions>> {
let mut options: ResolveOptions = options.await?.clone_value();
// TODO set fully_specified when in strict ESM mode
// options.fully_specified = true;
for conditions in get_condition_maps(&mut options) {
conditions.insert("import".to_string(), ConditionValue::Set);
conditions.insert("require".to_string(), ConditionValue::Unset);
}

if matches!(
reference_type.into_value(),
ReferenceType::EcmaScriptModules(EcmaScriptModulesReferenceSubType::ImportWithType(_))
) {
options.extensions.clear();
}

Ok(options.into())
}

Expand All @@ -71,7 +82,7 @@ pub async fn esm_resolve(
issue_source: Option<Vc<IssueSource>>,
) -> Result<Vc<ModuleResolveResult>> {
let ty = Value::new(ReferenceType::EcmaScriptModules(ty.into_value()));
let options = apply_esm_specific_options(origin.resolve_options(ty.clone()))
let options = apply_esm_specific_options(origin.resolve_options(ty.clone()), ty.clone())
.resolve()
.await?;
specific_resolve(origin, request, options, ty, issue_severity, issue_source).await
Expand Down
21 changes: 19 additions & 2 deletions crates/turbopack/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ use turbopack_core::{
output::OutputAsset,
raw_module::RawModule,
reference_type::{
CssReferenceSubType, EcmaScriptModulesReferenceSubType, InnerAssets, ReferenceType,
CssReferenceSubType, EcmaScriptModulesReferenceSubType, ImportWithType, InnerAssets,
ReferenceType,
},
resolve::{
options::ResolveOptions, origin::PlainResolveOrigin, parse::Request, resolve, ModulePart,
Expand Down Expand Up @@ -471,9 +472,25 @@ async fn process_default_internal(
ReferenceType::Internal(inner_assets) => Some(*inner_assets),
_ => None,
};

let mut has_type_attribute = false;

let mut current_source = source;
let mut current_module_type = None;
let mut current_module_type = match &reference_type {
ReferenceType::EcmaScriptModules(EcmaScriptModulesReferenceSubType::ImportWithType(ty)) => {
has_type_attribute = true;

match ty {
ImportWithType::Json => Some(ModuleType::Json),
}
}
_ => None,
};

for (i, rule) in options.await?.rules.iter().enumerate() {
if has_type_attribute && current_module_type.is_some() {
continue;
}
if processed_rules.contains(&i) {
continue;
}
Expand Down

0 comments on commit 56b6806

Please sign in to comment.