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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/rspack_plugin_rstest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rspack_core = { workspace = true }
rspack_error = { workspace = true }
rspack_hook = { workspace = true }
rspack_plugin_javascript = { workspace = true }
rspack_util = { workspace = true }
swc_core = { workspace = true }
tracing = { workspace = true }

Expand Down
34 changes: 22 additions & 12 deletions crates/rspack_plugin_rstest/src/module_path_name_dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ use rspack_cacheable::{cacheable, cacheable_dyn};
use rspack_core::{
AsContextDependency, AsModuleDependency, DependencyCodeGeneration, DependencyTemplate,
DependencyTemplateType, DependencyType, InitFragmentExt, InitFragmentKey, InitFragmentStage,
Module, NormalInitFragment, TemplateContext, TemplateReplaceSource,
NormalInitFragment, TemplateContext, TemplateReplaceSource,
};
use rspack_util::json_stringify;

#[cacheable]
#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -60,7 +61,6 @@ impl DependencyTemplate for ModulePathNameDependencyTemplate {
let m = module.as_normal_module();
if let Some(m) = m {
let resource_path = &m.resource_resolved_data().resource_path;
let context = &m.get_context();

let dep = dep
.as_any()
Expand All @@ -70,7 +70,10 @@ impl DependencyTemplate for ModulePathNameDependencyTemplate {
if dep.r#type == NameType::FileName {
if let Some(resource_path) = resource_path {
let init = NormalInitFragment::new(
format!("const __filename = '{resource_path}';\n"),
format!(
"const __filename = {};\n",
json_stringify(&resource_path.as_std_path())
),
InitFragmentStage::StageConstants,
0,
InitFragmentKey::Const(format!("retest __filename {}", m.id())),
Expand All @@ -80,16 +83,23 @@ impl DependencyTemplate for ModulePathNameDependencyTemplate {
init_fragments.push(init.boxed());
}
} else if dep.r#type == NameType::DirName {
if let Some(context) = context {
let init = NormalInitFragment::new(
format!("const __dirname = '{context}';\n"),
InitFragmentStage::StageConstants,
0,
InitFragmentKey::Const(format!("retest __dirname {}", m.id())),
None,
);
if let Some(resource_path) = resource_path {
if let Some(parent_path) = resource_path.parent() {
// If the parent path is None, we use an empty string
// to avoid issues with the path being undefined.
let init = NormalInitFragment::new(
format!(
"const __dirname = {};\n",
json_stringify(parent_path.as_std_path())
),
InitFragmentStage::StageConstants,
0,
InitFragmentKey::Const(format!("retest __dirname {}", m.id())),
None,
);

init_fragments.push(init.boxed());
init_fragments.push(init.boxed());
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/rspack_plugin_rstest/src/parser_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rspack_plugin_javascript::{
visitors::JavascriptParser,
JavascriptParserPlugin,
};
use rspack_util::json_stringify;
use swc_core::{
common::Spanned,
ecma::ast::{CallExpr, Ident, MemberExpr, UnaryExpr},
Expand Down Expand Up @@ -77,7 +78,7 @@ impl RstestParserPlugin {
fn process_import_meta(&self, parser: &mut JavascriptParser, r#type: ModulePathType) -> String {
if r#type == ModulePathType::FileName {
if let Some(resource_path) = &parser.resource_data.resource_path {
format!("'{}'", resource_path.clone().into_string())
json_stringify(&resource_path.clone().into_string())
} else {
"''".to_string()
}
Expand All @@ -89,7 +90,7 @@ impl RstestParserPlugin {
.and_then(|p| p.parent())
.map(|p| p.to_string())
.unwrap_or_default();
format!("'{resource_path}'")
json_stringify(&resource_path)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
const fs = require('fs');
const path = require('path');
const fs = require("fs");
const path = require("path");

const sourceDir = path.resolve(__dirname, "../../../../configCases/rstest/module-path-names/src");

it("Insert module path names with concatenateModules", () => {
const content = fs.readFileSync(path.resolve(__dirname, "modulePathName.js"), "utf-8");
// __dirname and __filename renamed after concatenation
expect(content).toContain(`const foo_filename = '${path.resolve(sourceDir, "./foo.js")}'`);
expect(content).toMatch(`const foo_dirname = '${path.resolve(sourceDir, ".")}'`);
expect(content).toContain(`const foo_filename = ${JSON.stringify(path.resolve(sourceDir, "./foo.js"))}`);
expect(content).toContain(`const foo_dirname = ${JSON.stringify(path.resolve(sourceDir, "."))}`);

expect(content).toMatch(`const metaFile1 = '${path.resolve(sourceDir, "./foo.js")}'`)
expect(content).toMatch(`const metaDir1 = '${path.resolve(sourceDir, ".")}'`)
expect(content).toContain(`const metaFile1 = ${JSON.stringify(path.resolve(sourceDir, "./foo.js"))}`)
expect(content).toContain(`const metaDir1 = ${JSON.stringify(path.resolve(sourceDir, "."))}`)

expect(content).toContain(`const typeofMetaDir = 'string'`);
expect(content).toContain(`const typeofMetaFile = 'string'`);
});

it("Insert module path names without concatenateModules", () => {
const content = fs.readFileSync(path.resolve(__dirname, "modulePathNameWithoutConcatenate.js"), "utf-8");
expect(content).toContain(`const __filename = '${path.resolve(sourceDir, "./foo.js")}'`);
expect(content).toMatch(`const __dirname = '${path.resolve(sourceDir, ".")}'`);
expect(content).toContain(`const __filename = ${JSON.stringify(path.resolve(sourceDir, "./foo.js"))}`);
expect(content).toContain(`const __dirname = ${JSON.stringify(path.resolve(sourceDir, "."))}`);
expect(content.match(/const __dirname = /g).length).toBe(2);
expect(content.match(/const __filename = /g).length).toBe(2);

expect(content).toMatch(`const metaFile1 = '${path.resolve(sourceDir, "./foo.js")}'`)
expect(content).toMatch(`const metaDir1 = '${path.resolve(sourceDir, ".")}'`)
expect(content).toContain(`const metaFile1 = ${JSON.stringify(path.resolve(sourceDir, "./foo.js"))}`)
expect(content).toContain(`const metaDir1 = ${JSON.stringify(path.resolve(sourceDir, "."))}`)

expect(content).toContain(`const typeofMetaDir = 'string'`);
expect(content).toContain(`const typeofMetaFile = 'string'`);
Expand Down
Loading