Skip to content

Commit 6631901

Browse files
authored
fix: should transform output.path while emitting assets (#12181)
* fix: should transform output.path while emitting assets * fix: should transform output.path while emitting assets
1 parent 5b47c08 commit 6631901

File tree

5 files changed

+44
-27
lines changed

5 files changed

+44
-27
lines changed

crates/rspack_core/src/compiler/mod.rs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use tracing::instrument;
1515
pub use self::rebuild::CompilationRecords;
1616
use crate::{
1717
BoxPlugin, CleanOptions, Compilation, CompilationAsset, CompilerOptions, ContextModuleFactory,
18-
KeepPattern, Logger, NormalModuleFactory, PluginDriver, ResolverFactory, SharedPluginDriver,
18+
Filename, KeepPattern, Logger, NormalModuleFactory, PluginDriver, ResolverFactory,
19+
SharedPluginDriver,
1920
cache::{Cache, new_cache},
2021
compilation::make::ModuleExecutor,
2122
fast_set, include_hash,
@@ -349,7 +350,15 @@ impl Compiler {
349350

350351
#[instrument("emit_assets", skip_all)]
351352
pub async fn emit_assets(&mut self) -> Result<()> {
352-
self.run_clean_options().await?;
353+
let output_path_str = self
354+
.compilation
355+
.get_path(
356+
&Filename::from(&self.options.output.path),
357+
Default::default(),
358+
)
359+
.await?;
360+
let output_path = Utf8Path::new(&output_path_str);
361+
self.run_clean_options(output_path).await?;
353362

354363
self
355364
.plugin_driver
@@ -383,10 +392,10 @@ impl Compiler {
383392
}
384393

385394
// SAFETY: await immediately and trust caller to poll future entirely
386-
let s = unsafe { token.used((&self, filename, asset)) };
395+
let s = unsafe { token.used((&self, filename, asset, output_path)) };
387396

388-
s.spawn(|(this, filename, asset)| {
389-
this.emit_asset(&this.options.output.path, filename, asset)
397+
s.spawn(|(this, filename, asset, output_path)| {
398+
this.emit_asset(output_path, filename, asset)
390399
});
391400
})
392401
})
@@ -480,7 +489,7 @@ impl Compiler {
480489
Ok(())
481490
}
482491

483-
async fn run_clean_options(&mut self) -> Result<()> {
492+
async fn run_clean_options(&mut self, output_path: &Utf8Path) -> Result<()> {
484493
let clean_options = &self.options.output.clean;
485494

486495
// keep all
@@ -491,37 +500,24 @@ impl Compiler {
491500
if self.emitted_asset_versions.is_empty() {
492501
match clean_options {
493502
CleanOptions::CleanAll(true) => {
494-
self
495-
.output_filesystem
496-
.remove_dir_all(&self.options.output.path)
497-
.await?;
503+
self.output_filesystem.remove_dir_all(output_path).await?;
498504
}
499505
CleanOptions::KeepPath(p) => {
500-
let path = self.options.output.path.join(p);
506+
let path = output_path.join(p);
501507
trim_dir(
502508
&*self.output_filesystem,
503-
&self.options.output.path,
509+
output_path,
504510
KeepPattern::Path(&path),
505511
)
506512
.await?;
507513
}
508514
CleanOptions::KeepRegex(r) => {
509515
let keep_pattern = KeepPattern::Regex(r);
510-
trim_dir(
511-
&*self.output_filesystem,
512-
&self.options.output.path,
513-
keep_pattern,
514-
)
515-
.await?;
516+
trim_dir(&*self.output_filesystem, output_path, keep_pattern).await?;
516517
}
517518
CleanOptions::KeepFunc(f) => {
518519
let keep_pattern = KeepPattern::Func(f);
519-
trim_dir(
520-
&*self.output_filesystem,
521-
&self.options.output.path,
522-
keep_pattern,
523-
)
524-
.await?;
520+
trim_dir(&*self.output_filesystem, output_path, keep_pattern).await?;
525521
}
526522
_ => {}
527523
}
@@ -539,7 +535,7 @@ impl Compiler {
539535
let filename = filename.to_owned();
540536
Some(async {
541537
if !clean_options.keep(&filename).await {
542-
let filename = Utf8Path::new(&self.options.output.path).join(filename);
538+
let filename = output_path.join(filename);
543539
let _ = self.output_filesystem.remove_file(&filename).await;
544540
}
545541
})

crates/rspack_core/src/options/filename.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rspack_cacheable::{
1212
with::{AsPreset, Unsupported},
1313
};
1414
use rspack_error::ToStringResultToRspackResultExt;
15+
use rspack_paths::Utf8PathBuf;
1516
use rspack_util::{MergeFrom, atom::Atom, base64, ext::CowExt};
1617

1718
use crate::{AssetInfo, PathData, ReplaceAllPlaceholder, ResourceParsedData, parse_resource};
@@ -98,6 +99,11 @@ impl From<String> for Filename {
9899
Self(FilenameKind::Template(Atom::from(value)))
99100
}
100101
}
102+
impl From<&Utf8PathBuf> for Filename {
103+
fn from(value: &Utf8PathBuf) -> Self {
104+
Self(FilenameKind::Template(Atom::from(value.as_str())))
105+
}
106+
}
101107
impl From<&str> for Filename {
102108
fn from(value: &str) -> Self {
103109
Self(FilenameKind::Template(Atom::from(value)))

tests/rspack-test/Incremental-node.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ describeByWalk(
2020
dist: path.resolve(__dirname, `./js/incremental/hot-node`),
2121
exclude: [
2222
/^css$/,
23-
// FIXME: incremental failed
24-
/^rebuild-abnormal-module$/
2523
]
2624
}
2725
);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
it("should work", async () => {
2+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const path = require("path");
2+
const fs = require("fs");
3+
module.exports = (_, { testPath }) => ({
4+
output: {
5+
path: path.join(testPath, "__[fullhash]__")
6+
},
7+
plugins: [{
8+
apply(compiler) {
9+
compiler.hooks.done.tap("Test", (stats) => {
10+
const dirs = fs.readdirSync(testPath);
11+
expect(dirs).not.toContain("__[fullhash]__");
12+
});
13+
}
14+
}]
15+
});

0 commit comments

Comments
 (0)