use std::{ cell::Cell, marker::PhantomData, path::Path, ptr::NonNull, rc::Rc, sync::{Arc, atomic::AtomicU8}, }; use parcel_core::{ Asset, AssetFlags, AssetIndex, AssetRequest, AssetType, BufferContent, Bundle, BundleBehavior, BundleFlags, BundleGraph, BundleGraphDependencyResolution, Content, ContentWithSourceMap, DependencyFlags, DependencyResolution, Environment, EnvironmentFlags, ExportsCondition, FileContent, ImportType, LogMessage, Namer, Optimizer, OutputFormat, PathId, PluginContext, Priority, Reporter, ReporterEvent, Resolver, SourceLocation, SourceType, SourceUrl, SpecifierType, Target, Transformer, }; use rquickjs::{ Class, Coerced, Ctx, FromJs, Function, IntoJs, JsLifetime, Object, Symbol, TypedArray, class::{self, Trace}, function::This, methods, }; use crate::{ await_promise, cjs::CjsLoader, diagnostic::diagnostics_error, with_named_plugin_env, with_origin, }; pub struct JsPlugin { path: String, /// The plugin's name in .parcelrc, which its diagnostics are attributed to. origin: String, config: Option, reporter_methods: ReporterMethods, } pub struct ReporterMethods(AtomicU8); impl ReporterMethods { const UNINITIALIZED: u8 = 1 << 0; const BUILD_START: u8 = 1 << 1; const BUILD_SUCCESS: u8 = 1 << 2; const BUILD_FAILURE: u8 = 1 << 3; const LOG: u8 = 1 << 4; pub fn new() -> Self { ReporterMethods(AtomicU8::new(ReporterMethods::UNINITIALIZED)) } pub fn is_empty(&self) -> bool { self.0.load(std::sync::atomic::Ordering::Acquire) == 0 } pub fn contains(&self, flag: u8) -> bool { (self.0.load(std::sync::atomic::Ordering::Acquire) & flag) != 0 } pub fn set(&self, flags: u8) { self.0.store(flags, std::sync::atomic::Ordering::Release); } } impl ReporterMethods { /// The methods looked for, in the order they are reported to a plugin that /// exports none of them. const NAMES: [&'static str; 4] = ["buildStart", "buildSuccess", "buildFailure", "log"]; } impl JsPlugin { pub fn new(path: PathId, origin: &str, config: Option) -> JsPlugin { JsPlugin { path: path.with_path(|path| path.to_str().unwrap().to_owned()), origin: origin.to_owned(), config, reporter_methods: ReporterMethods::new(), } } fn with_env( &self, options: &parcel_core::ParcelOptions, f: F, ) -> Result where F: FnOnce(&Ctx) -> rquickjs::Result, { with_named_plugin_env(options, Some(&self.origin), f) } fn config_to_js<'js>(&self, ctx: &Ctx<'js>) -> rquickjs::Result> { match &self.config { Some(config) => ctx.json_parse(config.to_string()), None => Ok(rquickjs::Value::new_undefined(ctx.clone())), } } } impl Optimizer for JsPlugin { fn optimize( &self, bundle_graph: &BundleGraph, bundle: &Bundle, contents: Arc, options: &parcel_core::ParcelOptions, ) -> Result, parcel_core::DiagnosticList> { let bundle_index = bundle_graph .bundles .iter() .position(|candidate| std::ptr::eq(candidate, bundle)) .expect("Bundle passed to optimizer must belong to the bundle graph"); let source_map = contents .downcast_ref::() .map(|contents| contents.source_map().to_vec()); let contents = contents.read()?; with_call_scope(|scope| { let graph = Rc::new(scope.wrap_graph(bundle_graph, options)); self.with_env(options, |ctx| { let config = self.plugin_config(ctx, "optimizer")?; let optimize: Function = config.get("optimize")?; let args = Object::new(ctx.clone())?; args.set("bundle", JsBundle::new(bundle_index, graph.clone()))?; args.set("bundleGraph", JsBundleGraph::new(graph.clone()))?; args.set("contents", TypedArray::new(ctx.clone(), contents)?)?; // A JSON object, as `Asset.getMap` gives. if let Some(source_map) = source_map { args.set("map", ctx.json_parse(source_map)?)?; } else { args.set("map", rquickjs::Value::new_null(ctx.clone()))?; } args.set("config", self.config_to_js(ctx)?)?; args.set( "options", JsOptions { options: scope.wrap(options), }, )?; let result: rquickjs::Value = optimize.call((This(config), args))?; let result = await_promise(ctx, result)?; let result = result.into_object().ok_or_else(|| { rquickjs::Error::new_from_js_message("optimizer result", "object", "Expected an object") })?; let contents = content_to_bytes(ctx, result.get("contents")?)?; match source_map_to_bytes(ctx, result.get("map")?)? { Some(map) => Ok(Arc::new(ContentWithSourceMap::new(contents, map)) as Arc), None => Ok(Arc::new(BufferContent::new(contents)) as Arc), } }) }) } } pub fn load_module<'js>(ctx: &Ctx<'js>, path: &str) -> rquickjs::Result> { let cjs = ctx.userdata::().unwrap(); let module = cjs.load(ctx, path)?; module.into_object().ok_or(rquickjs::Error::Unknown) } /// The kinds of plugin `@parcel/plugin` registers, each under its own key. const PLUGIN_KINDS: [&str; 5] = ["transformer", "resolver", "namer", "optimizer", "reporter"]; /// The object a plugin of `kind` registered (e.g. with `new Transformer({...})`) in the module /// itself or its default export. fn registered_plugin<'js>( ctx: &Ctx<'js>, module: &Object<'js>, sym: String, ) -> rquickjs::Result>> { let symbol: Object = ctx.globals().get("Symbol")?; let symbol_for: Function = symbol.get("for")?; let key: Symbol = symbol_for.call((sym,))?; if let Some(plugin) = module.get::<_, Option>(key.clone())? { return Ok(Some(plugin)); } match module.get::<_, Option>("default")? { Some(default) => default.get(key), None => Ok(None), } } impl JsPlugin { /// Loads the plugin and returns the object it registered as a plugin of `kind`, the one /// holding `transform`, `resolve`, `name`, `optimize` or the reporter methods. fn plugin_config<'js>(&self, ctx: &Ctx<'js>, kind: &str) -> rquickjs::Result> { let module = load_module(ctx, &self.path)?; if let Some(plugin) = registered_plugin(ctx, &module, format!("parcel.plugin.v3.{kind}"))? { return Ok(plugin); } // Backward compatibility for now, so you can run Parcel 2 and 3 side by side. // TODO: remove before GA. if let Some(plugin) = registered_plugin(ctx, &module, format!("parcel-plugin-config"))? { return Ok(plugin); } let mut hints = Vec::new(); for other in PLUGIN_KINDS { if other != kind && registered_plugin(ctx, &module, format!("parcel.plugin.v3.{other}"))?.is_some() { hints.push(format!("It is a {other} plugin.")); } } let mut class = kind.to_owned(); class[..1].make_ascii_uppercase(); hints.push(format!( "Export `new {class}({{...}})` from @parcel/plugin as the module's default export." )); let mut diagnostic = parcel_core::Diagnostic::from_message(format!( "{} is not a valid Parcel {kind} plugin", self.origin )); diagnostic.hints = hints; Err(ctx.throw(diagnostics_error(ctx, &[diagnostic])?)) } } /// A non-owning reference which may be retained by JavaScript, but can only be /// dereferenced while its originating call scope is alive. struct ScopedRef { ptr: NonNull, alive: Rc>, } impl Clone for ScopedRef { fn clone(&self) -> Self { Self { ptr: self.ptr, alive: self.alive.clone(), } } } unsafe impl<'js, T: 'static> JsLifetime<'js> for ScopedRef { type Changed<'to> = ScopedRef; } impl ScopedRef { fn with(&self, f: impl for<'a> FnOnce(&'a T) -> R) -> rquickjs::Result { if !self.alive.get() { return Err(rquickjs::Error::new_from_js_message( "scoped object", "live scoped object", "This object can no longer be accessed because its plugin call has completed", )); } // SAFETY: ScopedRef values can only be created by CallScope::wrap. The // higher-ranked with_call_scope closure ensures all source references live // through the call, and CallScope::drop invalidates every handle before // those references may expire. The pointer is never exposed to callers. Ok(f(unsafe { self.ptr.as_ref() })) } } struct CallScope<'scope> { alive: Rc>, _lifetime: PhantomData>, } impl<'scope> CallScope<'scope> { fn wrap(&self, value: &'scope T) -> ScopedRef { ScopedRef { ptr: NonNull::from(value), alive: self.alive.clone(), } } } impl<'scope> CallScope<'scope> { fn wrap_graph<'g>( &self, graph: &'scope BundleGraph<'g>, options: &'scope parcel_core::ParcelOptions, ) -> ScopedGraph { ScopedGraph { graph: ScopedRef { // The graph's lifetime is erased here, and restored as a higher-ranked lifetime by // ScopedGraph::with, so no reference into the graph can escape as 'static. ptr: NonNull::from(graph).cast::>(), alive: self.alive.clone(), }, options: self.wrap(options), } } } impl<'scope> CallScope<'scope> { fn wrap_context<'c>(&self, context: &'scope PluginContext<'c>) -> ScopedContext { ScopedContext(ScopedRef { // As with wrap_graph: the context's lifetime is erased here and restored as a higher-ranked // lifetime by ScopedContext::with. ptr: NonNull::from(context).cast::>(), alive: self.alive.clone(), }) } } /// The context of the transform or resolve call a JS plugin is running in, for the `resolve` /// function it is handed. struct ScopedContext(ScopedRef>); impl ScopedContext { fn with(&self, f: impl for<'a, 'c> FnOnce(&'a PluginContext<'c>) -> R) -> rquickjs::Result { self.0.with(|context| f(context)) } } /// A bundle graph shared by the bundles, assets and dependencies exposed from one plugin call. /// Values from the same graph share one `Rc`, which identifies the graph they belong to. struct ScopedGraph { graph: ScopedRef>, /// The options of the build the graph belongs to. options: ScopedRef, } unsafe impl<'js> JsLifetime<'js> for ScopedGraph { type Changed<'to> = ScopedGraph; } impl ScopedGraph { fn with(&self, f: impl for<'a, 'g> FnOnce(&'a BundleGraph<'g>) -> R) -> rquickjs::Result { self.graph.with(|graph| f(graph)) } } fn same_graph( a: &Rc, b: &Rc, what: &'static str, ) -> rquickjs::Result<()> { if Rc::ptr_eq(a, b) { Ok(()) } else { Err(rquickjs::Error::new_from_js_message( what, what, format!("{what} belongs to a different BundleGraph"), )) } } impl Drop for CallScope<'_> { fn drop(&mut self) { self.alive.set(false); } } fn with_call_scope<'scope, R>(f: impl FnOnce(&CallScope<'scope>) -> R) -> R { let scope = CallScope { alive: Rc::new(Cell::new(true)), _lifetime: PhantomData, }; f(&scope) } impl Transformer for JsPlugin { fn transform( &self, asset: Asset, context: &PluginContext, ) -> std::result::Result { let options = context.options; with_call_scope(|scope| { let asset = self.with_env(options, |ctx| { let config = self.plugin_config(ctx, "transformer")?; let transform: Function = config.get("transform")?; // Dependencies resolved through `resolve` are imported by this asset, for its target. let target = asset.target.clone(); let asset = JsAsset::owned(asset); let value = asset.into_js(&ctx)?; let opts = Object::new(ctx.clone())?; opts.set("asset", value.clone())?; opts.set("config", self.config_to_js(ctx)?)?; opts.set( "options", JsOptions { options: scope.wrap(options), }, )?; let context = scope.wrap_context(context); opts.set( "resolve", Function::new(ctx.clone(), move |ctx, from, specifier, options| { resolve(ctx, from, specifier, options, &context, &target) }), )?; let res: rquickjs::Value = transform.call((This(config), opts))?; await_promise(ctx, res)?; let obj = Class::::from_js(&ctx, value)?; let js_asset = &mut *obj.borrow_mut(); let asset = js_asset.take_owned().expect("Asset already taken"); Ok(asset) })?; Ok(asset) }) } } /// The `resolve(from, specifier, options)` function handed to a JS transformer: resolves with the /// configured resolver plugins, exactly as a dependency the transformer added would be. fn resolve<'js>( ctx: Ctx<'js>, from: String, specifier: String, options: rquickjs::function::Opt>, context: &ScopedContext, target: &Arc, ) -> rquickjs::Result> { let mut specifier_type = SpecifierType::Esm; let mut import_type = ImportType::JavaScript; let mut conditions = ExportsCondition::empty(); let mut custom_conditions = Vec::new(); if let Some(options) = options.0 { specifier_type = dependency_enum(&ctx, &options, "specifierType", SpecifierType::Esm)?; import_type = self::import_type(&ctx, &options, ImportType::JavaScript)?; (conditions, custom_conditions) = package_conditions( options .get::<_, Option>>("packageConditions")? .unwrap_or_default(), ); } let dep = parcel_core::Dependency { specifier: specifier.as_str().into(), specifier_type, priority: Priority::Sync, bundle_behavior: BundleBehavior::None, import_type, flags: DependencyFlags::empty(), concurrent_group: 0, target: target.clone(), loc: None, placeholder: None, resolve_from: Some(SourceUrl::from_path(&PathId::new(Path::new(&from)))), range: None, conditions, custom_conditions: custom_conditions.into(), resolution: DependencyResolution::None, }; let (promise, resolve, reject) = rquickjs::Promise::new(&ctx)?; let result = match context.with(|context| context.resolve(&dep))? { Ok(DependencyResolution::None) => Ok(rquickjs::Value::new_null(ctx.clone())), Ok(DependencyResolution::External) => Ok(flag_object(&ctx, "isExternal")?), Ok(DependencyResolution::Excluded) => Ok(flag_object(&ctx, "isExcluded")?), Ok(DependencyResolution::Deferred(req)) if req.loc.url.to_file_path().is_ok() => { let result = Object::new(ctx.clone())?; let path = req.loc.url.to_file_path().unwrap(); result.set( "filePath", path.to_path_buf().to_string_lossy().into_owned(), )?; result.set( "pipeline", or_null(&ctx, req.pipeline.as_deref().map(Into::into))?, )?; result.set("query", or_null(&ctx, req.loc.url.query().map(Into::into))?)?; result.set("sideEffects", req.side_effects)?; Ok(result.into_value()) } // Resolvers don't resolve to assets already in the graph, and a URL that isn't a file has no // path to give. Ok(_) => Err(vec![parcel_core::Diagnostic::from_message(format!( "Could not resolve '{specifier}' to a file" ))]), Err(diagnostics) => Err(diagnostics.0), }; match result { Ok(value) => resolve.call::<_, ()>((value,))?, Err(diagnostics) => reject.call::<_, ()>((diagnostics_error(&ctx, &diagnostics)?,))?, } Ok(promise) } /// `{[flag]: true}`, as `resolve` gives for external and excluded dependencies. fn flag_object<'js>(ctx: &Ctx<'js>, flag: &str) -> rquickjs::Result> { let result = Object::new(ctx.clone())?; result.set(flag, true)?; Ok(result.into_value()) } impl Resolver for JsPlugin { fn resolve( &self, dep: &parcel_core::Dependency, specifier: &str, pipeline: Option<&str>, ctx: &parcel_core::PluginContext, ) -> Result { let options = ctx.options; with_call_scope(|scope| { self.with_env(options, |ctx| { let config = self.plugin_config(ctx, "resolver")?; let resolve: Function = config.get("resolve")?; let js_dep = JsDependency { dep: JsDependencyValue::Borrowed(scope.wrap(dep)), }; let opts = Object::new(ctx.clone())?; opts.set("dependency", js_dep)?; opts.set("specifier", specifier)?; opts.set("pipeline", pipeline)?; opts.set("config", self.config_to_js(ctx)?)?; opts.set( "options", JsOptions { options: scope.wrap(options), }, )?; let res: rquickjs::Value = resolve.call((This(config), opts))?; let res = await_promise(ctx, res)?; if res.is_null() || res.is_undefined() { return Ok(DependencyResolution::None); } let Some(res) = res.as_object() else { return Err(rquickjs::Exception::throw_type( ctx, "A resolver must return an object, null or undefined", )); }; { if res.get::<_, Option>("isExcluded")? == Some(true) { return Ok(DependencyResolution::Excluded); } if res.get::<_, Option>("isExternal")? == Some(true) { return Ok(DependencyResolution::External); } let file_path: Option = res.get("filePath")?; if let Some(file_path) = file_path { if !Path::new(&file_path).is_absolute() { return Err(rquickjs::Exception::throw_type( &ctx, &format!("Resolvers must return an absolute path, returned: {file_path}"), )); } let query_value: rquickjs::Value = res.get("query")?; let query = if query_value.is_null() || query_value.is_undefined() { None } else { Some(Coerced::::from_js(&ctx, query_value)?.0) }; let side_effects: Option = res.get("sideEffects")?; let code: Option = res.get("code")?; let result_pipeline: rquickjs::Value = res.get("pipeline")?; let path = PathId::new(Path::new(&file_path)); let url = SourceUrl::from_path_and_query(&path, query.as_ref().map(|s| s.as_str())); let ty = AssetType::from_url(&url); return Ok(DependencyResolution::Deferred(Arc::new(AssetRequest { loc: SourceLocation { url, ..Default::default() }, content: if let Some(code) = code { Arc::new(BufferContent::new_string(code)) } else { Arc::new(FileContent::new(path, options.input_fs.clone())) }, target: dep.target.clone(), pipeline: if result_pipeline.is_undefined() { pipeline.map(Into::into) } else if result_pipeline.is_null() { None } else { Some(String::from_js(&ctx, result_pipeline)?.into()) }, ty, side_effects: side_effects.unwrap_or(true), unique_key: None, }))); } } Ok(DependencyResolution::None) }) }) } } impl Namer for JsPlugin { fn name( &self, bundle_graph: &BundleGraph, bundle: &Bundle, options: &parcel_core::ParcelOptions, ) -> Result, parcel_core::DiagnosticList> { let bundle_index = bundle_graph .bundles .iter() .position(|candidate| std::ptr::eq(candidate, bundle)) .expect("Bundle passed to namer must belong to the bundle graph"); let name = with_call_scope(|scope| { let graph = Rc::new(scope.wrap_graph(bundle_graph, options)); self.with_env(options, |ctx| { let config = self.plugin_config(ctx, "namer")?; let name: Function = config.get("name")?; let args = Object::new(ctx.clone())?; args.set("bundle", JsBundle::new(bundle_index, graph.clone()))?; args.set("bundleGraph", JsBundleGraph::new(graph.clone()))?; args.set("config", self.config_to_js(ctx)?)?; args.set( "options", JsOptions { options: scope.wrap(options), }, )?; let result: rquickjs::Value = name.call((This(config), args))?; let result = await_promise(ctx, result)?; Option::::from_js(ctx, result) }) })?; // As for native namers, an empty name leaves the bundle to the next namer. Core checks // that any other name stays inside the dist dir. Ok( name .filter(|name| !name.is_empty()) .map(|name| bundle.target.dist_dir.join(Path::new(&name))), ) } } impl JsPlugin { /// The reporter methods this plugin exports, resolved once and cached. fn reporter_methods( &self, options: &parcel_core::ParcelOptions, ) -> Result<&ReporterMethods, parcel_core::DiagnosticList> { if !self .reporter_methods .contains(ReporterMethods::UNINITIALIZED) { return Ok(&self.reporter_methods); } let methods = self.with_env(options, |ctx| { let config = self.plugin_config(ctx, "reporter")?; let exports = |name: &str| -> rquickjs::Result { Ok( config .get::<_, rquickjs::Value>(name)? .as_function() .is_some(), ) }; let mut flags: u8 = 0; if exports("buildStart")? { flags |= ReporterMethods::BUILD_START; } if exports("buildSuccess")? { flags |= ReporterMethods::BUILD_SUCCESS; } if exports("buildFailure")? { flags |= ReporterMethods::BUILD_FAILURE; } if exports("log")? { flags |= ReporterMethods::LOG; } Ok(flags) })?; self.reporter_methods.set(methods); if self.reporter_methods.is_empty() { // Reported once: the cached empty set makes every later event a no-op, // rather than repeating this for every event of every build. return Err(with_origin( parcel_core::Diagnostic::from_message(format!( "{} is configured as a reporter but exports none of {}", self.path, ReporterMethods::NAMES.join(", ") )) .into(), &self.origin, )); } Ok(&self.reporter_methods) } } impl Reporter for JsPlugin { fn report( &self, event: &ReporterEvent, options: &parcel_core::ParcelOptions, ) -> Result<(), parcel_core::DiagnosticList> { let methods = self.reporter_methods(options)?; // Checked before anything is marshalled: building a `buildSuccess` payload // means wrapping every bundle and asset in the graph, which is wasted on a // plugin that only implements `buildFailure`. let method = match event { ReporterEvent::BuildStart => methods .contains(ReporterMethods::BUILD_START) .then_some("buildStart"), ReporterEvent::BuildSuccess(_) => methods .contains(ReporterMethods::BUILD_SUCCESS) .then_some("buildSuccess"), ReporterEvent::BuildFailure { .. } => methods .contains(ReporterMethods::BUILD_FAILURE) .then_some("buildFailure"), ReporterEvent::Log(_) => methods.contains(ReporterMethods::LOG).then_some("log"), // An event added after this build of Parcel. _ => None, }; let Some(method) = method else { return Ok(()); }; with_call_scope(|scope| { // Only a successful build carries a graph to expose. let graph = match event { ReporterEvent::BuildSuccess(success) => { Some(Rc::new(scope.wrap_graph(&success.bundle_graph, options))) } _ => None, }; self.with_env(options, |ctx| { let args = Object::new(ctx.clone())?; args.set( "options", JsOptions { options: scope.wrap(options), }, )?; match event { ReporterEvent::BuildStart => {} ReporterEvent::BuildSuccess(success) => { let graph = graph .as_ref() .expect("a buildSuccess event always wraps its graph"); args.set("bundleGraph", JsBundleGraph::new(graph.clone()))?; args.set("buildTime", success.build_time.as_secs_f64() * 1000.0)?; args.set( "changedAssets", success .changed_assets .iter() .map(|&index| JsAsset::from_graph(graph.clone(), index)) .collect::>(), )?; } ReporterEvent::BuildFailure { diagnostics } => { args.set("diagnostics", diagnostics_to_js(ctx, &diagnostics.0)?)?; } ReporterEvent::Log(log) => { args.set("level", log.level.as_str())?; match log.message { LogMessage::Text(text) => args.set("message", text)?, LogMessage::Diagnostics(diagnostics) => { args.set("diagnostics", diagnostics_to_js(ctx, diagnostics)?)? } } } // Ruled out above, where the method for this event was resolved. _ => return Ok(()), } args.set("config", self.config_to_js(ctx)?)?; let config = self.plugin_config(ctx, "reporter")?; let report: Function = config.get(method)?; let result: rquickjs::Value = report.call((This(config), args))?; await_promise(ctx, result)?; Ok(()) }) }) } } /// Converts diagnostics to plain JavaScript objects, in the shape their /// `serde` representation already defines. pub(crate) fn diagnostics_to_js<'js>( ctx: &Ctx<'js>, diagnostics: &[parcel_core::Diagnostic], ) -> rquickjs::Result> { let json = serde_json::to_string(diagnostics).map_err(|error| { rquickjs::Error::new_from_js_message("diagnostics", "JSON", error.to_string()) })?; ctx.json_parse(json) } fn json_to_js<'js>( ctx: &Ctx<'js>, value: &impl serde::Serialize, ) -> rquickjs::Result> { let json = serde_json::to_vec(value) .map_err(|error| rquickjs::Error::new_from_js_message("value", "JSON", error.to_string()))?; ctx.json_parse(json) } /// A source map given as a JSON object, a JSON string, or UTF-8 JSON in a `Uint8Array`, as the /// JSON Parcel stores. Nullish is no map. fn source_map_to_bytes<'js>( ctx: &Ctx<'js>, map: Option>, ) -> rquickjs::Result>> { let Some(map) = map.filter(|map| !map.is_null() && !map.is_undefined()) else { return Ok(None); }; let invalid = |reason: &str| { rquickjs::Exception::throw_type( ctx, &format!("A source map must be a JSON object, a JSON string or a Uint8Array: {reason}"), ) }; let json = if let Some(string) = map.as_string() { string.to_string()?.into_bytes() } else if let Ok(bytes) = TypedArray::::from_js(ctx, map.clone()) { // SAFETY: copied immediately, before any JS runs. unsafe { bytes.as_bytes() } .ok_or_else(|| invalid("the Uint8Array is detached"))? .to_vec() } else if map.is_object() && !map.is_array() && !map.is_function() { // Stringified by JS, so it is valid JSON. let json = ctx .json_stringify(map)? .ok_or_else(|| invalid("not serializable"))?; return Ok(Some(json.to_string()?.into_bytes())); } else { return Err(invalid(map.type_name())); }; // Checked here, where the plugin that set it gets the error, rather than when it is read. if let Err(error) = serde_json::from_slice::>(&json) { return Err(invalid(&error.to_string())); } Ok(Some(json)) } /// Content given as a string or a `Uint8Array`. fn content_to_bytes<'js>(ctx: &Ctx<'js>, value: rquickjs::Value<'js>) -> rquickjs::Result> { if let Some(string) = value.as_string() { return Ok(string.to_string()?.into_bytes()); } let invalid = || rquickjs::Exception::throw_type(ctx, "Contents must be a string or a Uint8Array"); let bytes = TypedArray::::from_js(ctx, value).map_err(|_| invalid())?; // SAFETY: copied immediately, before any JS runs. Ok(unsafe { bytes.as_bytes() }.ok_or_else(invalid)?.to_vec()) } fn dependency_enum<'js, T: serde::de::DeserializeOwned>( ctx: &Ctx<'js>, options: &Object<'js>, name: &str, default: T, ) -> rquickjs::Result { let Some(value) = options.get::<_, Option>(name)? else { return Ok(default); }; serde_json::from_value(serde_json::Value::String(value.clone())) .map_err(|_| rquickjs::Exception::throw_type(ctx, &format!("Invalid {name}: {value}"))) } /// The `importType` option of a dependency, or `default` when it isn't given. fn import_type<'js>( ctx: &Ctx<'js>, options: &Object<'js>, default: ImportType, ) -> rquickjs::Result { Ok( match options.get::<_, Option>("importType")?.as_deref() { None => default, Some("javascript") => ImportType::JavaScript, Some("url") => ImportType::Url, Some("stylesheet") => ImportType::StyleSheet, Some("text") => ImportType::Text, Some("bytes") => ImportType::Bytes, Some(value) => { return Err(rquickjs::Exception::throw_type( ctx, &format!("Invalid importType: {value}"), )); } }, ) } /// Splits package conditions into the ones Parcel knows, which are flags, and custom ones. fn package_conditions(values: Vec) -> (ExportsCondition, Vec) { let mut conditions = ExportsCondition::empty(); let mut custom_conditions = Vec::new(); for value in values { match ExportsCondition::try_from(value.as_str()) { Ok(condition) => conditions |= condition, Err(_) => custom_conditions.push(value), } } (conditions, custom_conditions) } fn source_url(ctx: &Ctx<'_>, value: &str) -> rquickjs::Result { if Path::new(value).is_absolute() { Ok(SourceUrl::from_path(&PathId::new(Path::new(value)))) } else { SourceUrl::parse(value).map_err(|error| rquickjs::Exception::throw_type(ctx, &error.message)) } } fn dependency_from_js<'js>( ctx: &Ctx<'js>, options: &Object<'js>, asset: &Asset, url: bool, ) -> rquickjs::Result { let mut specifier: String = options.get("specifier")?; if let Some(pipeline) = options.get::<_, Option>("pipeline")? { specifier = format!("{pipeline}:{specifier}"); } let mut flags = DependencyFlags::empty(); for (name, flag) in [ ("isEntry", DependencyFlags::ENTRY), ("isOptional", DependencyFlags::OPTIONAL), ("needsStableName", DependencyFlags::NEEDS_STABLE_NAME), ("sideEffects", DependencyFlags::SIDE_EFFECTS), ] { flags.set(flag, options.get::<_, Option>(name)?.unwrap_or(false)); } let target = match options.get::<_, Option>>("target")? { Some(target) => target.borrow().target.clone(), None => asset.target.clone(), }; let (conditions, custom_conditions) = package_conditions( options .get::<_, Option>>("packageConditions")? .unwrap_or_default(), ); let loc = if let Some(loc) = options.get::<_, Option>("loc")? { let url = match loc .get::<_, Option>("url")? .or(loc.get::<_, Option>("filePath")?) { Some(url) => source_url(ctx, &url)?, None => asset.loc.url.clone(), }; let location = |name| -> rquickjs::Result { let value: Object = loc.get(name)?; Ok(parcel_core::Location { line: value.get("line")?, column: value.get("column")?, }) }; Some(SourceLocation { url, start: location("start")?, end: location("end")?, }) } else { None }; let import_type = import_type( ctx, options, if url { ImportType::Url } else { ImportType::JavaScript }, )?; let mut dep = parcel_core::Dependency { specifier: specifier.into_boxed_str(), specifier_type: dependency_enum( ctx, options, "specifierType", if url { SpecifierType::Url } else { SpecifierType::Esm }, )?, priority: dependency_enum( ctx, options, "priority", if url { Priority::Lazy } else { Priority::Sync }, )?, bundle_behavior: dependency_enum(ctx, options, "bundleBehavior", BundleBehavior::None)?, import_type, flags, concurrent_group: 0, custom_conditions: custom_conditions.into(), target, loc, placeholder: None, resolve_from: Some(match options.get::<_, Option>("resolveFrom")? { Some(from) => source_url(ctx, &from)?, None => asset.loc.url.clone(), }), range: options.get::<_, Option>("range")?.map(Into::into), conditions, resolution: DependencyResolution::None, }; dep.set_placeholder(); Ok(dep) } #[derive(JsLifetime)] #[rquickjs::class] struct JsBundle { index: usize, graph: Rc, } impl<'js> Trace<'js> for JsBundle { fn trace<'a>(&self, _tracer: class::Tracer<'a, 'js>) {} } #[methods(rename_all = "camelCase")] impl JsBundle { #[qjs(get, rename = "type")] fn get_type(&self) -> rquickjs::Result { self.with_bundle(|bundle| bundle.ty.extension().to_owned()) } #[qjs(get)] fn needs_stable_name(&self) -> rquickjs::Result { self.with_bundle(|bundle| bundle.flags.contains(BundleFlags::NEEDS_STABLE_NAME)) } /// The string a namer must include exactly once in the file name of a bundle that may be /// content hashed. #[qjs(get)] fn hash_reference(&self) -> rquickjs::Result { self.with_bundle(|bundle| bundle.hash_reference()) } #[qjs(get)] fn is_entry(&self) -> rquickjs::Result { self.with_bundle(|bundle| bundle.flags.contains(BundleFlags::ENTRY)) } #[qjs(get)] fn bundle_behavior(&self) -> rquickjs::Result> { self.with_bundle(|bundle| match bundle.bundle_behavior { BundleBehavior::None => None, BundleBehavior::Inline => Some("inline"), BundleBehavior::Isolated => Some("isolated"), }) } #[qjs(get)] fn target(&self) -> rquickjs::Result { self.with_bundle(|bundle| JsTarget { target: bundle.target.clone(), }) } /// The path the bundle is written to, once it is known. #[qjs(get)] fn file_path<'js>(&self, ctx: Ctx<'js>) -> rquickjs::Result> { let path = self.graph.options.with(|options| { self.with_bundle(|bundle| { bundle .output_path(options) .map(|path| path.to_path_buf().to_string_lossy().into_owned()) }) })??; or_null(&ctx, path) } /// The bundle's name relative to its target's dist directory, once it can be read. #[qjs(get)] fn name<'js>(&self, ctx: Ctx<'js>) -> rquickjs::Result> { let name = self.with_bundle(|bundle| { // Until it is named, a bundle has no path to take a name from. (bundle.is_name_available() && (bundle.has_final_path() || bundle.dist_path.is_some())) .then(|| bundle.name()) })?; or_null(&ctx, name) } fn get_main_entry(&self) -> rquickjs::Result> { self.with_bundle(|bundle| { bundle .main_entry_asset .map(|index| JsAsset::from_graph(self.graph.clone(), index)) }) } fn get_entry_assets(&self) -> rquickjs::Result> { self.with_bundle(|bundle| { bundle .entry_assets .iter() .map(|&index| JsAsset::from_graph(self.graph.clone(), index)) .collect() }) } fn has_asset(&self, asset: Class<'_, JsAsset>) -> rquickjs::Result { let asset = asset.borrow(); let JsAssetValue::Graph { graph, index } = &asset.asset else { return Err(rquickjs::Error::new_from_js_message( "Asset", "Asset", "Asset belongs to a different BundleGraph", )); }; same_graph(&self.graph, graph, "Asset")?; self.with_bundle(|bundle| bundle.assets.contains(index)) } } impl JsBundle { fn new(index: usize, graph: Rc) -> Self { Self { index, graph } } fn with_bundle(&self, f: impl for<'a> FnOnce(&'a Bundle) -> R) -> rquickjs::Result { self.graph.with(|graph| f(&graph.bundles[self.index])) } } #[derive(JsLifetime)] #[rquickjs::class] struct JsBundleGraph { graph: Rc, } impl<'js> Trace<'js> for JsBundleGraph { fn trace<'a>(&self, _tracer: class::Tracer<'a, 'js>) {} } #[methods(rename_all = "camelCase")] impl JsBundleGraph { fn get_bundles(&self) -> rquickjs::Result> { let len = self.graph.with(|graph| graph.bundles.len())?; Ok(self.bundles(0..len)) } fn get_entry_bundles(&self) -> rquickjs::Result> { let indices = self.graph.with(|graph| { graph .bundles .iter() .enumerate() .filter(|(_, bundle)| bundle.flags.contains(BundleFlags::ENTRY)) .map(|(index, _)| index) .collect::>() })?; Ok(self.bundles(indices)) } fn get_referenced_bundles( &self, bundle: Class<'_, JsBundle>, options: rquickjs::function::Opt>, ) -> rquickjs::Result> { let index = self.bundle_index(&bundle)?; let recursive = options .0 .map(|options| options.get::<_, Option>("recursive")) .transpose()? .flatten() .unwrap_or(false); let indices = self.graph.with(|graph| { if recursive { // The closure starts with the bundle itself. graph.referenced_bundles(index).skip(1).collect() } else { graph.bundles[index].referenced_bundles.clone() } })?; Ok(self.bundles(indices)) } /// Bundles that code in `bundle`'s execution context may load, not including `bundle`. fn get_bundles_in_bundle_group( &self, bundle: Class<'_, JsBundle>, ) -> rquickjs::Result> { let index = self.bundle_index(&bundle)?; let indices = self .graph .with(|graph| graph.context_closure(&graph.bundles[index]).to_vec())?; Ok(self.bundles(indices)) } /// Bundles that dependencies of `bundle`'s assets resolve to: lazy, isolated, inline and URL /// references. fn get_bundle_dependencies( &self, bundle: Class<'_, JsBundle>, ) -> rquickjs::Result> { let index = self.bundle_index(&bundle)?; let indices = self.graph.with(|graph| { let mut indices = Vec::new(); for target in graph.bundle_dependency_targets(&graph.bundles[index]) { if !indices.contains(&target) { indices.push(target); } } indices })?; Ok(self.bundles(indices)) } /// The asset that `dependency` resolved to, if any. fn get_resolved_asset( &self, dependency: Class<'_, JsDependency>, ) -> rquickjs::Result> { let dependency = dependency.borrow(); self.dependency_id(&dependency)?; let index = dependency.with_dependency(|dep| { self.graph.with(|graph| { graph .asset_graph .resolved_asset(dep) .map(|(index, _)| index) }) })??; Ok(index.map(|index| JsAsset::from_graph(self.graph.clone(), index))) } /// The bundle that `dependency` references, if it resolves to a different bundle. fn get_referenced_bundle( &self, dependency: Class<'_, JsDependency>, ) -> rquickjs::Result> { let (asset, index) = self.dependency_id(&dependency.borrow())?; let resolution = self .graph .with(|graph| graph.dependency_resolution(asset, index))?; Ok(match resolution { BundleGraphDependencyResolution::Bundle { bundle_index, .. } => { Some(JsBundle::new(bundle_index as usize, self.graph.clone())) } _ => None, }) } } impl JsBundleGraph { fn new(graph: Rc) -> Self { Self { graph } } fn bundles(&self, indices: impl IntoIterator) -> Vec { indices .into_iter() .map(|index| JsBundle::new(index, self.graph.clone())) .collect() } fn bundle_index(&self, bundle: &Class<'_, JsBundle>) -> rquickjs::Result { let bundle = bundle.borrow(); same_graph(&self.graph, &bundle.graph, "Bundle")?; Ok(bundle.index) } fn dependency_id(&self, dependency: &JsDependency) -> rquickjs::Result<(AssetIndex, usize)> { match &dependency.dep { JsDependencyValue::Graph { graph, asset, index, } => { same_graph(&self.graph, graph, "Dependency")?; Ok((*asset, *index)) } _ => Err(rquickjs::Error::new_from_js_message( "Dependency", "Dependency", "Dependency belongs to a different BundleGraph", )), } } } #[derive(JsLifetime)] #[rquickjs::class] struct JsAsset { asset: JsAssetValue, } #[derive(JsLifetime)] enum JsAssetValue { Owned(Option), Graph { graph: Rc, index: AssetIndex, }, } impl<'js> Trace<'js> for JsAsset { fn trace<'a>(&self, _tracer: class::Tracer<'a, 'js>) {} } impl JsAsset { fn owned(asset: Asset) -> Self { Self { asset: JsAssetValue::Owned(Some(asset)), } } fn from_graph(graph: Rc, index: AssetIndex) -> Self { Self { asset: JsAssetValue::Graph { graph, index }, } } fn take_owned(&mut self) -> Option { match &mut self.asset { JsAssetValue::Owned(asset) => asset.take(), JsAssetValue::Graph { .. } => None, } } fn with_asset(&self, f: impl for<'a> FnOnce(&'a Asset) -> R) -> rquickjs::Result { match &self.asset { JsAssetValue::Owned(Some(asset)) => Ok(f(asset)), JsAssetValue::Owned(None) => Err(rquickjs::Error::new_from_js_message( "asset", "available asset", "Asset has already been consumed", )), JsAssetValue::Graph { graph, index } => { graph.with(|graph| f(&graph.asset_graph.assets[index.index()])) } } } fn with_asset_mut(&mut self, f: impl FnOnce(&mut Asset) -> R) -> rquickjs::Result { match &mut self.asset { JsAssetValue::Owned(Some(asset)) => Ok(f(asset)), JsAssetValue::Owned(None) => Err(rquickjs::Error::new_from_js_message( "asset", "available asset", "Asset has already been consumed", )), JsAssetValue::Graph { .. } => Err(rquickjs::Error::new_from_js_message( "read-only asset", "mutable asset", "Assets in a bundle graph are read-only", )), } } } #[methods(rename_all = "camelCase")] impl JsAsset { #[qjs(get)] fn url(&self) -> rquickjs::Result { self.with_asset(|asset| asset.loc.url.to_string()) } #[qjs(get)] fn file_path(&self) -> rquickjs::Result { self.with_asset(|asset| { asset .loc .url .to_file_path() .unwrap() .to_path_buf() .to_string_lossy() .into_owned() }) } #[qjs(get, rename = "type")] fn get_type(&self) -> rquickjs::Result { self.with_asset(|asset| asset.ty.extension().to_owned()) } #[qjs(set, rename = "type")] fn set_type(&mut self, ty: String) -> rquickjs::Result<()> { self.with_asset_mut(|asset| asset.ty = AssetType::from_extension(&ty)) } fn get_buffer<'js>(&self, ctx: Ctx<'js>) -> rquickjs::Result> { let src = self .with_asset(|asset| asset.content.read())? .map_err(|e| { rquickjs::Error::new_from_js_message("content", "readable content", e.to_string()) })?; TypedArray::new(ctx, src) } fn get_code(&self) -> rquickjs::Result { let src = self .with_asset(|asset| asset.content.read())? .map_err(|e| { rquickjs::Error::new_from_js_message("content", "readable content", e.to_string()) })?; Ok(String::from_utf8_lossy(&src).into_owned()) } fn get_map<'js>(&self, ctx: Ctx<'js>) -> rquickjs::Result> { let map = self.with_asset(|asset| { asset .content .downcast_ref::() .map(|content| content.source_map().to_vec()) })?; match map { Some(map) => ctx.json_parse(map), None => Ok(rquickjs::Value::new_null(ctx)), } } fn set_buffer<'js>( &mut self, ctx: Ctx<'js>, buf: TypedArray<'js, u8>, map: rquickjs::function::Opt>, ) -> rquickjs::Result<()> { // SAFETY: copied immediately, before any JS runs. let bytes = unsafe { buf.as_bytes() } .ok_or_else(|| { rquickjs::Error::new_from_js_message( "buffer", "Uint8Array", "Invalid or detached Uint8Array", ) })? .to_owned(); let map = source_map_to_bytes(&ctx, map.0)?; self.with_asset_mut(|asset| { asset.content = match map { Some(map) => Arc::new(ContentWithSourceMap::new(bytes, map)), None => Arc::new(BufferContent::new(bytes)), }; }) } fn set_code<'js>( &mut self, ctx: Ctx<'js>, value: String, map: rquickjs::function::Opt>, ) -> rquickjs::Result<()> { let map = source_map_to_bytes(&ctx, map.0)?; self.with_asset_mut(|asset| { asset.content = match map { Some(map) => Arc::new(ContentWithSourceMap::new_string(value, map)), None => Arc::new(BufferContent::new_string(value)), }; }) } fn get_dependencies(&self) -> rquickjs::Result> { if let JsAssetValue::Graph { graph, index } = &self.asset { let len = self.with_asset(|asset| asset.dependencies.len())?; return Ok( (0..len) .map(|dependency| JsDependency { dep: JsDependencyValue::Graph { graph: graph.clone(), asset: *index, index: dependency, }, }) .collect(), ); } // Snapshots cannot be invalidated by a later addDependency reallocating the vector. self.with_asset(|asset| { asset .dependencies .iter() .cloned() .map(|dep| JsDependency { dep: JsDependencyValue::Owned(Arc::new(dep)), }) .collect() }) } /// Returns a placeholder that can be embedded in transformed content. fn add_dependency<'js>( &mut self, ctx: Ctx<'js>, options: Object<'js>, ) -> rquickjs::Result { let dep = self.with_asset(|asset| dependency_from_js(&ctx, &options, asset, false))??; self.with_asset_mut(|asset| { let placeholder = dep.placeholder.as_ref().unwrap().to_string(); asset.dependencies.push(dep); placeholder }) } #[qjs(rename = "addURLDependency")] fn add_url_dependency<'js>( &mut self, ctx: Ctx<'js>, url: String, options: rquickjs::function::Opt>, ) -> rquickjs::Result { // Copy the options so frozen objects and caller-owned objects are left intact. let opts = Object::new(ctx.clone())?; if let Some(options) = options.0 { for property in options.props::() { let (key, value) = property?; opts.set(key, value)?; } } opts.set("specifier", url)?; let dep = self.with_asset(|asset| dependency_from_js(&ctx, &opts, asset, true))??; self.with_asset_mut(|asset| { let placeholder = dep.placeholder.as_ref().unwrap().to_string(); asset.dependencies.push(dep); placeholder }) } #[qjs(get)] fn unique_key(&self) -> rquickjs::Result> { self.with_asset(|asset| asset.unique_key.as_ref().map(|key| key.to_string())) } #[qjs(set, rename = "uniqueKey")] fn set_unique_key(&mut self, value: Option) -> rquickjs::Result<()> { self.with_asset_mut(|asset| { if asset.unique_key.is_some() { return Err(rquickjs::Error::new_from_js_message( "uniqueKey", "unset uniqueKey", "Cannot change an asset's uniqueKey after it has been set", )); } asset.unique_key = value.map(Into::into); Ok(()) })? } #[qjs(get)] fn pipeline(&self) -> rquickjs::Result> { self.with_asset(|asset| asset.pipeline.as_ref().map(|pipeline| pipeline.to_string())) } #[qjs(set, rename = "pipeline")] fn set_pipeline(&mut self, value: Option) -> rquickjs::Result<()> { self.with_asset_mut(|asset| asset.pipeline = value.map(Into::into)) } #[qjs(get)] fn target(&self) -> rquickjs::Result { self.with_asset(|asset| JsTarget { target: asset.target.clone(), }) } #[qjs(get)] fn is_source(&self) -> rquickjs::Result { self.with_asset(|asset| asset.flags.contains(AssetFlags::IS_SOURCE)) } #[qjs(get)] fn side_effects(&self) -> rquickjs::Result { self.with_asset(|asset| asset.flags.contains(AssetFlags::SIDE_EFFECTS)) } #[qjs(set, rename = "sideEffects")] fn set_side_effects(&mut self, value: bool) -> rquickjs::Result<()> { self.with_asset_mut(|asset| asset.flags.set(AssetFlags::SIDE_EFFECTS, value)) } #[qjs(get, rename = "bundleBehavior")] fn bundle_behavior(&self) -> rquickjs::Result> { self.with_asset(|asset| match asset.bundle_behavior { BundleBehavior::None => None, BundleBehavior::Inline => Some("inline".into()), BundleBehavior::Isolated => Some("isolated".into()), }) } #[qjs(set, rename = "bundleBehavior")] fn set_bundle_behavior(&mut self, ctx: Ctx<'_>, value: Option) -> rquickjs::Result<()> { let behavior = match value.as_deref() { None | Some("none") => BundleBehavior::None, Some("inline") => BundleBehavior::Inline, Some("isolated") => BundleBehavior::Isolated, Some(value) => { return Err(rquickjs::Exception::throw_type( &ctx, &format!("Invalid bundleBehavior: {value}"), )); } }; self.with_asset_mut(|asset| asset.bundle_behavior = behavior) } } #[derive(JsLifetime)] #[rquickjs::class] pub struct JsTarget { target: Arc, } impl<'js> Trace<'js> for JsTarget { fn trace<'a>(&self, _tracer: class::Tracer<'a, 'js>) {} } #[methods(rename_all = "camelCase")] impl JsTarget { #[qjs(get)] fn dist_dir(&self) -> String { self .target .dist_dir .with_path(|path| path.to_string_lossy().into_owned()) } #[qjs(get)] fn public_url(&self) -> &str { &self.target.public_url } #[qjs(get)] fn engines<'js>(&self, ctx: Ctx<'js>) -> rquickjs::Result> { json_to_js(&ctx, &self.target.engines) } #[qjs(get)] fn include_node_modules<'js>(&self, ctx: Ctx<'js>) -> rquickjs::Result> { json_to_js(&ctx, &self.target.include_node_modules) } #[qjs(get)] fn source_map<'js>(&self, ctx: Ctx<'js>) -> rquickjs::Result> { json_to_js(&ctx, &self.target.source_map) } #[qjs(get)] fn environment(&self) -> &str { match self.target.environment { Environment::Browser => "browser", Environment::WebWorker => "web-worker", Environment::ServiceWorker => "service-worker", Environment::Worklet => "worklet", Environment::Node => "node", Environment::ElectronMain => "electron-main", Environment::ElectronRenderer => "electron-renderer", Environment::ReactClient => "react-client", Environment::ReactServer => "react-server", } } #[qjs(get)] fn output_format(&self) -> &str { match self.target.output_format { OutputFormat::Commonjs => "commonjs", OutputFormat::Esmodule => "esmodule", OutputFormat::Global => "global", } } #[qjs(get)] fn source_type(&self) -> &str { match self.target.source_type { SourceType::Module => "module", SourceType::Script => "script", } } #[qjs(get)] fn is_library(&self) -> bool { self.target.flags.contains(EnvironmentFlags::IS_LIBRARY) } #[qjs(get)] fn should_optimize(&self) -> bool { self .target .flags .contains(EnvironmentFlags::SHOULD_OPTIMIZE) } fn is_browser(&self) -> bool { self.target.environment.is_browser() } fn is_node(&self) -> bool { self.target.environment.is_node() } fn is_electron(&self) -> bool { self.target.environment.is_electron() } fn is_worker(&self) -> bool { self.target.environment.is_worker() } fn is_worklet(&self) -> bool { self.target.environment.is_worklet() } fn is_server(&self) -> bool { self.target.environment.is_server() } } #[derive(JsLifetime)] #[rquickjs::class] pub struct JsDependency { dep: JsDependencyValue, } #[derive(JsLifetime)] enum JsDependencyValue { Borrowed(ScopedRef), Owned(Arc), /// A dependency of an asset in a bundle graph. Graph { graph: Rc, asset: AssetIndex, index: usize, }, } impl JsDependency { fn with_dependency( &self, f: impl for<'a> FnOnce(&'a parcel_core::Dependency) -> R, ) -> rquickjs::Result { match &self.dep { JsDependencyValue::Borrowed(dep) => dep.with(f), JsDependencyValue::Owned(dep) => Ok(f(dep)), JsDependencyValue::Graph { graph, asset, index, } => graph.with(|graph| f(&graph.asset_graph.assets[asset.index()].dependencies[*index])), } } } impl<'js> Trace<'js> for JsDependency { fn trace<'a>(&self, _tracer: class::Tracer<'a, 'js>) {} } #[methods(rename_all = "camelCase")] impl JsDependency { #[qjs(get)] pub fn placeholder(&self) -> rquickjs::Result> { self.with_dependency(|dep| { dep .placeholder .as_ref() .map(|placeholder| placeholder.to_string()) }) } #[qjs(get)] pub fn import_type(&self) -> rquickjs::Result<&'static str> { self.with_dependency(|dep| match dep.import_type { ImportType::JavaScript => "javascript", ImportType::StyleSheet => "stylesheet", ImportType::Url => "url", ImportType::Text => "text", ImportType::Bytes => "bytes", }) } #[qjs(get)] pub fn specifier(&self) -> rquickjs::Result { self.with_dependency(|dep| dep.specifier.to_string()) } #[qjs(get)] pub fn specifier_type(&self) -> rquickjs::Result<&'static str> { self.with_dependency(|dep| match dep.specifier_type { SpecifierType::Commonjs => "commonjs", SpecifierType::Esm => "esm", SpecifierType::Url => "url", SpecifierType::Custom => "custom", }) } #[qjs(get)] pub fn priority(&self) -> rquickjs::Result<&'static str> { self.with_dependency(|dep| match dep.priority { Priority::Sync => "sync", Priority::Parallel => "parallel", Priority::Lazy => "lazy", }) } #[qjs(get)] pub fn bundle_behavior(&self) -> rquickjs::Result> { self.with_dependency(|dep| match dep.bundle_behavior { BundleBehavior::None => None, BundleBehavior::Inline => Some("inline"), BundleBehavior::Isolated => Some("isolated"), }) } #[qjs(get)] pub fn is_entry(&self) -> rquickjs::Result { self.with_dependency(|dep| dep.flags.contains(DependencyFlags::ENTRY)) } #[qjs(get)] pub fn is_optional(&self) -> rquickjs::Result { self.with_dependency(|dep| dep.flags.contains(DependencyFlags::OPTIONAL)) } #[qjs(get)] pub fn needs_stable_name(&self) -> rquickjs::Result { self.with_dependency(|dep| dep.flags.contains(DependencyFlags::NEEDS_STABLE_NAME)) } #[qjs(get)] pub fn side_effects(&self) -> rquickjs::Result { self.with_dependency(|dep| dep.flags.contains(DependencyFlags::SIDE_EFFECTS)) } #[qjs(get)] pub fn target(&self) -> rquickjs::Result { self.with_dependency(|dep| JsTarget { target: dep.target.clone(), }) } /// The path of the file the dependency is resolved from: its `resolveFrom`, or else the file /// it was found in. As for native plugins, `undefined` when that isn't a file. #[qjs(get)] pub fn resolve_from(&self) -> rquickjs::Result> { self.with_dependency(|dep| { let url = dep .resolve_from .as_ref() .or(dep.loc.as_ref().map(|loc| &loc.url))?; let path = url.to_file_path().ok()?; Some(path.to_path_buf().to_string_lossy().into_owned()) }) } #[qjs(get)] pub fn loc<'js>(&self, ctx: Ctx<'js>) -> rquickjs::Result>> { self.with_dependency(|dep| { let Some(loc) = &dep.loc else { return Ok(None); }; let result = Object::new(ctx.clone())?; result.set( "filePath", loc .url .to_file_path() .map(|path| path.to_path_buf().to_string_lossy().into_owned()) .unwrap_or_else(|_| loc.url.to_string()), )?; result.set( "start", js_location(&ctx, loc.start.line, loc.start.column)?, )?; result.set("end", js_location(&ctx, loc.end.line, loc.end.column)?)?; Ok(Some(result)) })? } #[qjs(get)] pub fn package_conditions(&self) -> rquickjs::Result> { self.with_dependency(|dep| { [ (ExportsCondition::IMPORT, "import"), (ExportsCondition::REQUIRE, "require"), (ExportsCondition::MODULE, "module"), (ExportsCondition::NODE, "node"), (ExportsCondition::BROWSER, "browser"), (ExportsCondition::WORKER, "worker"), (ExportsCondition::WORKLET, "worklet"), (ExportsCondition::ELECTRON, "electron"), (ExportsCondition::DEVELOPMENT, "development"), (ExportsCondition::PRODUCTION, "production"), (ExportsCondition::TYPES, "types"), (ExportsCondition::DEFAULT, "default"), (ExportsCondition::STYLE, "style"), (ExportsCondition::SASS, "sass"), (ExportsCondition::LESS, "less"), (ExportsCondition::STYLUS, "stylus"), (ExportsCondition::REACT_SERVER, "react-server"), (ExportsCondition::SOURCE, "source"), ] .into_iter() .filter_map(|(flag, name)| dep.conditions.contains(flag).then(|| name.to_owned())) .chain( dep .custom_conditions .iter() .map(|condition| condition.to_string()), ) .collect() }) } #[qjs(get)] pub fn range(&self) -> rquickjs::Result> { self.with_dependency(|dep| dep.range.as_ref().map(|r| (**r).to_string())) } } #[derive(JsLifetime)] #[rquickjs::class] pub struct JsOptions { options: ScopedRef, } impl<'js> Trace<'js> for JsOptions { fn trace<'a>(&self, _tracer: class::Tracer<'a, 'js>) {} } #[methods(rename_all = "camelCase")] impl JsOptions { #[qjs(get)] pub fn mode(&self) -> rquickjs::Result { self.options.with(|options| match &options.mode { parcel_core::BuildMode::Development => "development".into(), parcel_core::BuildMode::Production => "production".into(), parcel_core::BuildMode::Other(mode) => mode.clone(), }) } #[qjs(get)] pub fn env<'js>(&self, ctx: Ctx<'js>) -> rquickjs::Result> { self .options .with(|options| json_to_js(&ctx, &options.env))? } #[qjs(get)] pub fn log_level(&self) -> rquickjs::Result<&'static str> { self.options.with(|options| options.log_level.as_str()) } #[qjs(get)] pub fn hmr_options<'js>(&self, ctx: Ctx<'js>) -> rquickjs::Result>> { self.options.with(|options| { let Some(hmr) = &options.hmr else { return Ok(None); }; let result = Object::new(ctx)?; result.set("host", hmr.host.as_ref())?; result.set("port", hmr.port)?; Ok(Some(result)) })? } #[qjs(get)] pub fn project_root(&self) -> rquickjs::Result { self.options.with(|options| { options .project_root .to_path_buf() .to_string_lossy() .into_owned() }) } } fn or_null<'js>(ctx: &Ctx<'js>, value: Option) -> rquickjs::Result> { match value { Some(value) => value.into_js(ctx), None => Ok(rquickjs::Value::new_null(ctx.clone())), } } fn js_location<'js>(ctx: &Ctx<'js>, line: u32, column: u32) -> rquickjs::Result> { let location = Object::new(ctx.clone())?; location.set("line", line)?; location.set("column", column)?; Ok(location) }