Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,24 @@ impl JavascriptParserPlugin for ImportMetaPlugin {
if for_name == expr_name::IMPORT_META_VERSION {
Some(eval::evaluate_to_number(5_f64, start, end))
} else if for_name == expr_name::IMPORT_META_URL {
Some(eval::evaluate_to_string(
self.import_meta_url(parser),
start,
end,
))
if parser.is_esm {
if parser
.compiler_options
.output
.environment
.supports_document()
{
None
} else {
Some(eval::evaluate_to_string(
self.import_meta_url(parser),
start,
end,
))
}
} else {
None
}
} else {
None
}
Expand Down Expand Up @@ -172,7 +185,18 @@ impl JavascriptParserPlugin for ImportMetaPlugin {
let mut content = vec![];
for prop in referenced_properties_in_destructuring.iter() {
if prop.id == "url" {
content.push(format!(r#"url: "{}""#, self.import_meta_url(parser)))
if parser.is_esm
&& parser
.compiler_options
.output
.environment
.supports_document()
{
// Preserve import.meta.url for web targets in ES modules using a getter
content.push(r#"get url() { return import.meta.url; }"#.to_string())
} else {
content.push(format!(r#"url: "{}""#, self.import_meta_url(parser)))
}
} else if prop.id == "webpack" {
content.push(format!(r#"webpack: {}"#, self.import_meta_version()));
} else {
Expand Down Expand Up @@ -226,12 +250,25 @@ impl JavascriptParserPlugin for ImportMetaPlugin {
) -> Option<bool> {
if for_name == expr_name::IMPORT_META_URL {
// import.meta.url
parser.add_presentational_dependency(Box::new(ConstDependency::new(
member_expr.span().into(),
format!("'{}'", self.import_meta_url(parser)).into(),
None,
)));
Some(true)
if !parser.is_esm {
// import.meta.url is only available in ES modules
return None;
}
if parser
.compiler_options
.output
.environment
.supports_document()
{
Some(true)
} else {
parser.add_presentational_dependency(Box::new(ConstDependency::new(
member_expr.span().into(),
format!("'{}'", self.import_meta_url(parser)).into(),
None,
)));
Some(true)
}
} else if for_name == expr_name::IMPORT_META_VERSION {
// import.meta.webpack
parser.add_presentational_dependency(Box::new(ConstDependency::new(
Expand Down
13 changes: 13 additions & 0 deletions tests/rspack-test/configCases/parsing/import-meta-url-web/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export {};

it("should not be replaced by file path", function() {
// import.meta.url should be preserved in the bundle (not replaced with a file:// URL string)
// When preserved, it will be resolved by the runtime environment
// In Node.js test environment, it resolves to file:// URL, but in browser it would be http://
// The important thing is that it's not replaced during build time
const url = import.meta.url;
// Just verify it's a string (not undefined, which would mean it was replaced with unsupported comment)
expect(typeof url).toBe("string");
// The actual URL format depends on the runtime environment, but it should exist
expect(url).toBeTruthy();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = {
target: "web",
experiments: {
outputModule: true
},
output: {
module: true,
chunkFormat: "module"
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
moduleScope(scope) {
scope.pseudoImport = { meta: { url: "http://test.cases/path/index.js" } };
}
};

Loading