Commit 7ddd0ebb authored by Andrew Imm's avatar Andrew Imm

import.meta.glob well-known function

Detects import.meta.glob calls without conflicting with the existing import.meta visitor / rewrite. Constructs a new WellKnownFunctionKind for this method, and introduces a code generator for it which replaces the original span.

In this commit, it is constructed with default arguments (not from the method arguments) and simply outputs an empty object. This is exported as a standalone chunk that is imported at the original callsite.
parent 20433ca4
...@@ -1974,6 +1974,10 @@ impl JsValue { ...@@ -1974,6 +1974,10 @@ impl JsValue {
"URL".to_string(), "URL".to_string(),
"The standard URL constructor: https://developer.mozilla.org/en-US/docs/Web/API/URL/URL" "The standard URL constructor: https://developer.mozilla.org/en-US/docs/Web/API/URL/URL"
), ),
WellKnownFunctionKind::ImportMetaGlob => (
"import.meta.glob()".to_string(),
"ESM method that imports a dynamic set of files based on a matched pattern"
),
}; };
if depth > 0 { if depth > 0 {
let i = hints.len(); let i = hints.len();
...@@ -3638,6 +3642,7 @@ pub enum WellKnownFunctionKind { ...@@ -3638,6 +3642,7 @@ pub enum WellKnownFunctionKind {
// The worker_threads Worker class // The worker_threads Worker class
NodeWorkerConstructor, NodeWorkerConstructor,
URLConstructor, URLConstructor,
ImportMetaGlob,
} }
impl WellKnownFunctionKind { impl WellKnownFunctionKind {
...@@ -3648,6 +3653,7 @@ impl WellKnownFunctionKind { ...@@ -3648,6 +3653,7 @@ impl WellKnownFunctionKind {
Self::RequireResolve => Some(&["require", "resolve"]), Self::RequireResolve => Some(&["require", "resolve"]),
Self::RequireContext => Some(&["require", "context"]), Self::RequireContext => Some(&["require", "context"]),
Self::Define => Some(&["define"]), Self::Define => Some(&["define"]),
Self::ImportMetaGlob => Some(&["import", "meta", "glob"]),
_ => None, _ => None,
} }
} }
......
...@@ -624,6 +624,7 @@ async fn well_known_object_member( ...@@ -624,6 +624,7 @@ async fn well_known_object_member(
WellKnownObjectKind::NodePreGyp => node_pre_gyp(prop), WellKnownObjectKind::NodePreGyp => node_pre_gyp(prop),
WellKnownObjectKind::NodeExpressApp => express(prop), WellKnownObjectKind::NodeExpressApp => express(prop),
WellKnownObjectKind::NodeProtobufLoader => protobuf_loader(prop), WellKnownObjectKind::NodeProtobufLoader => protobuf_loader(prop),
WellKnownObjectKind::ImportMeta => import_meta(prop),
#[allow(unreachable_patterns)] #[allow(unreachable_patterns)]
_ => { _ => {
return Ok(( return Ok((
...@@ -929,3 +930,19 @@ fn protobuf_loader(prop: JsValue) -> JsValue { ...@@ -929,3 +930,19 @@ fn protobuf_loader(prop: JsValue) -> JsValue {
), ),
} }
} }
fn import_meta(prop: JsValue) -> JsValue {
if let Some("glob") = prop.as_str() {
JsValue::WellKnownFunction(WellKnownFunctionKind::ImportMetaGlob)
} else {
// other cases like "url" should already have been transformed
JsValue::unknown(
JsValue::member(
Box::new(JsValue::WellKnownObject(WellKnownObjectKind::ImportMeta)),
Box::new(prop),
),
true,
"unsupported property on import.meta object",
)
}
}
...@@ -34,6 +34,7 @@ use crate::{ ...@@ -34,6 +34,7 @@ use crate::{
}, },
exports_info::{ExportsInfoBinding, ExportsInfoRef}, exports_info::{ExportsInfoBinding, ExportsInfoRef},
ident::IdentReplacement, ident::IdentReplacement,
import_meta_glob::ImportMetaGlobAssetReferenceCodeGen,
member::MemberReplacement, member::MemberReplacement,
require_context::RequireContextAssetReferenceCodeGen, require_context::RequireContextAssetReferenceCodeGen,
unreachable::Unreachable, unreachable::Unreachable,
...@@ -199,6 +200,7 @@ pub enum CodeGen { ...@@ -199,6 +200,7 @@ pub enum CodeGen {
RequireContextAssetReferenceCodeGen(RequireContextAssetReferenceCodeGen), RequireContextAssetReferenceCodeGen(RequireContextAssetReferenceCodeGen),
UrlAssetReferenceCodeGen(UrlAssetReferenceCodeGen), UrlAssetReferenceCodeGen(UrlAssetReferenceCodeGen),
WorkerAssetReferenceCodeGen(WorkerAssetReferenceCodeGen), WorkerAssetReferenceCodeGen(WorkerAssetReferenceCodeGen),
ImportMetaGlobAssetReferenceCodeGen(ImportMetaGlobAssetReferenceCodeGen),
} }
impl CodeGen { impl CodeGen {
...@@ -231,6 +233,7 @@ impl CodeGen { ...@@ -231,6 +233,7 @@ impl CodeGen {
Self::RequireContextAssetReferenceCodeGen(v) => v.code_generation(ctx).await, Self::RequireContextAssetReferenceCodeGen(v) => v.code_generation(ctx).await,
Self::UrlAssetReferenceCodeGen(v) => v.code_generation(ctx).await, Self::UrlAssetReferenceCodeGen(v) => v.code_generation(ctx).await,
Self::WorkerAssetReferenceCodeGen(v) => v.code_generation(ctx).await, Self::WorkerAssetReferenceCodeGen(v) => v.code_generation(ctx).await,
Self::ImportMetaGlobAssetReferenceCodeGen(v) => v.code_generation(ctx).await,
} }
} }
} }
......
...@@ -9,6 +9,7 @@ pub mod esm; ...@@ -9,6 +9,7 @@ pub mod esm;
pub mod exports_info; pub mod exports_info;
pub mod external_module; pub mod external_module;
pub mod ident; pub mod ident;
pub mod import_meta_glob;
pub mod member; pub mod member;
pub mod node; pub mod node;
pub mod pattern_mapping; pub mod pattern_mapping;
...@@ -139,6 +140,7 @@ use crate::{ ...@@ -139,6 +140,7 @@ use crate::{
}, },
exports_info::{ExportsInfoBinding, ExportsInfoRef}, exports_info::{ExportsInfoBinding, ExportsInfoRef},
ident::IdentReplacement, ident::IdentReplacement,
import_meta_glob::ImportMetaGlobAssetReference,
member::MemberReplacement, member::MemberReplacement,
node::{FilePathModuleReference, PackageJsonReference}, node::{FilePathModuleReference, PackageJsonReference},
raw::{DirAssetReference, FileSourceReference}, raw::{DirAssetReference, FileSourceReference},
...@@ -2189,6 +2191,28 @@ where ...@@ -2189,6 +2191,28 @@ where
); );
} }
WellKnownFunctionKind::ImportMetaGlob => {
let _args = linked_args().await?;
let pattern = rcstr!("**");
let eager = false;
let import = None;
analysis.add_reference_code_gen(
ImportMetaGlobAssetReference::new(
source,
origin,
pattern,
eager,
import,
Some(issue_source(source, span)),
in_try,
)
.await?,
ast_path.to_vec().into(),
);
}
WellKnownFunctionKind::FsReadMethod(name) if analysis.analyze_mode.is_tracing() => { WellKnownFunctionKind::FsReadMethod(name) if analysis.analyze_mode.is_tracing() => {
let args = linked_args().await?; let args = linked_args().await?;
if !args.is_empty() { if !args.is_empty() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment