Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions crates/rspack_plugin_javascript/src/parser_plugin/drive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ use crate::{
parser_plugin::r#const::is_logic_op,
utils::eval::BasicEvaluatedExpression,
visitors::{
ClassDeclOrExpr, ExportDefaultDeclaration, ExportDefaultExpression, ExportImport, ExportLocal,
ExportedVariableInfo, JavascriptParser, Statement, VariableDeclaration,
ClassDeclOrExpr, DestructuringAssignmentProperty, ExportDefaultDeclaration,
ExportDefaultExpression, ExportImport, ExportLocal, ExportedVariableInfo, JavascriptParser,
Statement, VariableDeclaration,
},
};

Expand Down Expand Up @@ -817,4 +818,19 @@ impl JavascriptParserPlugin for JavaScriptParserPluginDrive {
}
None
}

fn import_meta_property_in_destructuring(
&self,
parser: &mut JavascriptParser,
property: &DestructuringAssignmentProperty,
) -> Option<String> {
for plugin in &self.plugins {
let res = plugin.import_meta_property_in_destructuring(parser, property);
// `SyncBailHook`
if res.is_some() {
return res;
}
}
None
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,23 @@ impl JavascriptParserPlugin for ImportMetaPlugin {
span: Span,
) -> Option<bool> {
if root_name == expr_name::IMPORT_META {
if let Some(referenced_properties_in_destructuring) =
parser.destructuring_assignment_properties.get(&span)
{
let destructuring_assignment_properties = parser
.destructuring_assignment_properties
.get(&span)
.cloned();

if let Some(referenced_properties_in_destructuring) = destructuring_assignment_properties {
let mut content = vec![];
for prop in referenced_properties_in_destructuring.iter() {
let res = parser
.plugin_drive
.clone()
.import_meta_property_in_destructuring(parser, prop);

if let Some(property) = res {
content.push(property);
continue;
}
if prop.id == "url" {
content.push(format!(r#"url: "{}""#, self.import_meta_url(parser)))
} else if prop.id == "webpack" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ use rspack_core::{
use sugar_path::SugarPath;
use swc_core::{common::Spanned, ecma::ast::Expr};

use super::JavascriptParserPlugin;
use crate::{dependency::ExternalModuleDependency, utils::eval, visitors::JavascriptParser};
use crate::{
JavascriptParserPlugin,
dependency::ExternalModuleDependency,
utils::eval,
visitors::{DestructuringAssignmentProperty, JavascriptParser},
};

const DIR_NAME: &str = "__dirname";
const FILE_NAME: &str = "__filename";
Expand Down Expand Up @@ -206,4 +210,13 @@ impl JavascriptParserPlugin for NodeStuffPlugin {
None
}
}

fn import_meta_property_in_destructuring(
&self,
_parser: &mut JavascriptParser,
_property: &DestructuringAssignmentProperty,
) -> Option<String> {
// TODO: implement import.meta.filename/dirname in destructuring
None
}
}
19 changes: 17 additions & 2 deletions crates/rspack_plugin_javascript/src/parser_plugin/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ use swc_core::{
use crate::{
utils::eval::BasicEvaluatedExpression,
visitors::{
ClassDeclOrExpr, ExportDefaultDeclaration, ExportDefaultExpression, ExportImport, ExportLocal,
ExportedVariableInfo, JavascriptParser, Statement, VariableDeclaration,
ClassDeclOrExpr, DestructuringAssignmentProperty, ExportDefaultDeclaration,
ExportDefaultExpression, ExportImport, ExportLocal, ExportedVariableInfo, JavascriptParser,
Statement, VariableDeclaration,
},
};

Expand Down Expand Up @@ -436,6 +437,20 @@ pub trait JavascriptParserPlugin {
fn is_pure(&self, _parser: &mut JavascriptParser, _expr: &Expr) -> Option<bool> {
None
}

/* plugin interop methods */

/**
* This method is used to interop with other plugins.
* It will be called in ImportMetaPlugin when processing destructuring of `import.meta`
*/
fn import_meta_property_in_destructuring(
&self,
_parser: &mut JavascriptParser,
_property: &DestructuringAssignmentProperty,
) -> Option<String> {
None
}
}

pub type BoxJavascriptParserPlugin = Box<dyn JavascriptParserPlugin + Send + Sync>;
Loading