Skip to content

Commit 6ee567e

Browse files
authored
chore: add import_meta_property_in_destructuring parser hook (#12255)
* chore: add import_meta_property_in_desturcturing hook * chore: add import_meta_property_in_desturcturing hook
1 parent 5795e26 commit 6ee567e

File tree

4 files changed

+65
-9
lines changed

4 files changed

+65
-9
lines changed

crates/rspack_plugin_javascript/src/parser_plugin/drive.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ use crate::{
1212
parser_plugin::r#const::is_logic_op,
1313
utils::eval::BasicEvaluatedExpression,
1414
visitors::{
15-
ClassDeclOrExpr, ExportDefaultDeclaration, ExportDefaultExpression, ExportImport, ExportLocal,
16-
ExportedVariableInfo, JavascriptParser, Statement, VariableDeclaration,
15+
ClassDeclOrExpr, DestructuringAssignmentProperty, ExportDefaultDeclaration,
16+
ExportDefaultExpression, ExportImport, ExportLocal, ExportedVariableInfo, JavascriptParser,
17+
Statement, VariableDeclaration,
1718
},
1819
};
1920

@@ -817,4 +818,19 @@ impl JavascriptParserPlugin for JavaScriptParserPluginDrive {
817818
}
818819
None
819820
}
821+
822+
fn import_meta_property_in_destructuring(
823+
&self,
824+
parser: &mut JavascriptParser,
825+
property: &DestructuringAssignmentProperty,
826+
) -> Option<String> {
827+
for plugin in &self.plugins {
828+
let res = plugin.import_meta_property_in_destructuring(parser, property);
829+
// `SyncBailHook`
830+
if res.is_some() {
831+
return res;
832+
}
833+
}
834+
None
835+
}
820836
}

crates/rspack_plugin_javascript/src/parser_plugin/import_meta_plugin.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,23 @@ impl JavascriptParserPlugin for ImportMetaPlugin {
166166
span: Span,
167167
) -> Option<bool> {
168168
if root_name == expr_name::IMPORT_META {
169-
if let Some(referenced_properties_in_destructuring) =
170-
parser.destructuring_assignment_properties.get(&span)
171-
{
169+
let destructuring_assignment_properties = parser
170+
.destructuring_assignment_properties
171+
.get(&span)
172+
.cloned();
173+
174+
if let Some(referenced_properties_in_destructuring) = destructuring_assignment_properties {
172175
let mut content = vec![];
173176
for prop in referenced_properties_in_destructuring.iter() {
177+
let res = parser
178+
.plugin_drive
179+
.clone()
180+
.import_meta_property_in_destructuring(parser, prop);
181+
182+
if let Some(property) = res {
183+
content.push(property);
184+
continue;
185+
}
174186
if prop.id == "url" {
175187
content.push(format!(r#"url: "{}""#, self.import_meta_url(parser)))
176188
} else if prop.id == "webpack" {

crates/rspack_plugin_javascript/src/parser_plugin/node_stuff_plugin.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ use rspack_core::{
55
use sugar_path::SugarPath;
66
use swc_core::{common::Spanned, ecma::ast::Expr};
77

8-
use super::JavascriptParserPlugin;
9-
use crate::{dependency::ExternalModuleDependency, utils::eval, visitors::JavascriptParser};
8+
use crate::{
9+
JavascriptParserPlugin,
10+
dependency::ExternalModuleDependency,
11+
utils::eval,
12+
visitors::{DestructuringAssignmentProperty, JavascriptParser},
13+
};
1014

1115
const DIR_NAME: &str = "__dirname";
1216
const FILE_NAME: &str = "__filename";
@@ -212,4 +216,13 @@ impl JavascriptParserPlugin for NodeStuffPlugin {
212216
None
213217
}
214218
}
219+
220+
fn import_meta_property_in_destructuring(
221+
&self,
222+
_parser: &mut JavascriptParser,
223+
_property: &DestructuringAssignmentProperty,
224+
) -> Option<String> {
225+
// TODO: implement import.meta.filename/dirname in destructuring
226+
None
227+
}
215228
}

crates/rspack_plugin_javascript/src/parser_plugin/trait.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ use swc_core::{
1111
use crate::{
1212
utils::eval::BasicEvaluatedExpression,
1313
visitors::{
14-
ClassDeclOrExpr, ExportDefaultDeclaration, ExportDefaultExpression, ExportImport, ExportLocal,
15-
ExportedVariableInfo, JavascriptParser, Statement, VariableDeclaration,
14+
ClassDeclOrExpr, DestructuringAssignmentProperty, ExportDefaultDeclaration,
15+
ExportDefaultExpression, ExportImport, ExportLocal, ExportedVariableInfo, JavascriptParser,
16+
Statement, VariableDeclaration,
1617
},
1718
};
1819

@@ -436,6 +437,20 @@ pub trait JavascriptParserPlugin {
436437
fn is_pure(&self, _parser: &mut JavascriptParser, _expr: &Expr) -> Option<bool> {
437438
None
438439
}
440+
441+
/* plugin interop methods */
442+
443+
/**
444+
* This method is used to interop with other plugins.
445+
* It will be called in ImportMetaPlugin when processing destructuring of `import.meta`
446+
*/
447+
fn import_meta_property_in_destructuring(
448+
&self,
449+
_parser: &mut JavascriptParser,
450+
_property: &DestructuringAssignmentProperty,
451+
) -> Option<String> {
452+
None
453+
}
439454
}
440455

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

0 commit comments

Comments
 (0)