Skip to content

Commit

Permalink
feat: support destructuring of import.meta
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder committed Jul 19, 2024
1 parent e7e9d7b commit 9e8af95
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use itertools::Itertools;
use rspack_core::{property_access, ConstDependency, SpanExt};
use rspack_error::miette::Severity;
use swc_core::common::{Span, Spanned};
Expand All @@ -6,10 +7,10 @@ use url::Url;

use super::JavascriptParserPlugin;
use crate::utils::eval;
use crate::visitors::expr_name;
use crate::visitors::ExportedVariableInfo;
use crate::visitors::JavascriptParser;
use crate::visitors::{create_traceable_error, RootName};
use crate::visitors::{expr_name, AllowedMemberTypes};
use crate::visitors::{ExportedVariableInfo, MemberExpressionInfo};

pub struct ImportMetaPlugin;

Expand Down Expand Up @@ -232,12 +233,25 @@ impl JavascriptParserPlugin for ImportMetaPlugin {
match root_info {
ExportedVariableInfo::Name(root) => {
if root == expr_name::IMPORT_META {
let members = parser
.get_member_expression_info(expr, AllowedMemberTypes::Expression)
.and_then(|info| match info {
MemberExpressionInfo::Expression(res) => Some(res.members),
_ => None,
});
parser
.presentational_dependencies
.push(Box::new(ConstDependency::new(
expr.span().real_lo(),
expr.span().real_hi(),
"undefined".into(),
members
.map(|members| {
self.import_meta_unknown_property(
&members.iter().map(|x| x.to_string()).collect_vec(),
)
})
.unwrap_or("undefined".to_string())
.into(),
None,
)));
return Some(true);
Expand Down
2 changes: 1 addition & 1 deletion tests/webpack-test/cases/esm/import-meta/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ it("should return correct import.meta.webpack", () => {
it("should return undefined for unknown property", () => {
expect(import.meta.other).toBe(undefined);
if (typeof import.meta.other !== "undefined") require("fail");
// expect(() => import.meta.other.other.other).toThrowError();
expect(() => import.meta.other.other.other).toThrowError();
});

it("should add warning on direct import.meta usage", () => {
Expand Down
17 changes: 0 additions & 17 deletions tests/webpack-test/cases/esm/import-meta/test.filter.js

This file was deleted.

0 comments on commit 9e8af95

Please sign in to comment.