Skip to content

Commit

Permalink
feat(rspack_plugin_copy): Set source_filename and copied flag when co…
Browse files Browse the repository at this point in the history
…pying an asset (#8077)
  • Loading branch information
ramon-villain authored Oct 14, 2024
1 parent b145c15 commit 636d37d
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 9 deletions.
3 changes: 3 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ export interface JsAssetInfo {
*/
contenthash: Array<string>
sourceFilename?: string
/** when asset was created from a source file (potentially transformed), it should be flagged as copied */
copied?: boolean
/**
* size in bytes, only set after asset has been emitted
* when asset is only used for development and doesn't count towards user-facing assets
Expand Down Expand Up @@ -802,6 +804,7 @@ export interface JsStatsAssetInfo {
development?: boolean
hotModuleReplacement?: boolean
sourceFilename?: string
copied?: boolean
immutable?: boolean
javascriptModule?: boolean
chunkhash: Array<string>
Expand Down
4 changes: 4 additions & 0 deletions crates/rspack_binding_values/src/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub struct JsAssetInfo {
pub contenthash: Vec<String>,
// when asset was created from a source file (potentially transformed), the original filename relative to compilation context
pub source_filename: Option<String>,
/// when asset was created from a source file (potentially transformed), it should be flagged as copied
pub copied: Option<bool>,
/// size in bytes, only set after asset has been emitted
// pub size: f64,
/// when asset is only used for development and doesn't count towards user-facing assets
Expand Down Expand Up @@ -63,6 +65,7 @@ impl From<JsAssetInfo> for rspack_core::AssetInfo {
content_hash: i.contenthash.into_iter().collect(),
version: String::from(""),
source_filename: i.source_filename,
copied: i.copied,
javascript_module: i.javascript_module,
css_unused_idents: i.css_unused_idents.map(|i| i.into_iter().collect()),
extras: i.extras,
Expand Down Expand Up @@ -97,6 +100,7 @@ impl From<rspack_core::AssetInfo> for JsAssetInfo {
fullhash: info.full_hash.into_iter().collect(),
contenthash: info.content_hash.into_iter().collect(),
source_filename: info.source_filename,
copied: info.copied,
javascript_module: info.javascript_module,
css_unused_idents: info.css_unused_idents.map(|i| i.into_iter().collect()),
extras: info.extras,
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_binding_values/src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ pub struct JsStatsAssetInfo {
pub development: Option<bool>,
pub hot_module_replacement: Option<bool>,
pub source_filename: Option<String>,
pub copied: Option<bool>,
pub immutable: Option<bool>,
pub javascript_module: Option<bool>,
pub chunkhash: Vec<String>,
Expand All @@ -438,6 +439,7 @@ impl From<rspack_core::StatsAssetInfo> for JsStatsAssetInfo {
development: stats.development,
hot_module_replacement: stats.hot_module_replacement,
source_filename: stats.source_filename,
copied: stats.copied,
immutable: stats.immutable,
javascript_module: stats.javascript_module,
chunkhash: stats.chunk_hash,
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1895,6 +1895,8 @@ pub struct AssetInfo {
pub content_hash: HashSet<String>,
/// when asset was created from a source file (potentially transformed), the original filename relative to compilation context
pub source_filename: Option<String>,
/// when asset was created from a source file (potentially transformed), it should be flagged as copied
pub copied: Option<bool>,
/// size in bytes, only set after asset has been emitted
// pub size: f64,
/// when asset is only used for development and doesn't count towards user-facing assets
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/stats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl Stats<'_> {
development: asset.info.development,
hot_module_replacement: asset.info.hot_module_replacement,
source_filename: asset.info.source_filename.clone(),
copied: asset.info.copied,
is_over_size_limit: asset.info.is_over_size_limit,
},
emitted: self.compilation.emitted_assets.contains(name),
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_core/src/stats/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub struct StatsAssetInfo {
pub development: Option<bool>,
pub hot_module_replacement: Option<bool>,
pub source_filename: Option<String>,
pub copied: Option<bool>,
pub immutable: Option<bool>,
pub javascript_module: Option<bool>,
pub chunk_hash: Vec<String>,
Expand Down
10 changes: 8 additions & 2 deletions crates/rspack_plugin_copy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,15 @@ async fn process_assets(&self, compilation: &mut Compilation) -> Result<()> {
if let Some(info) = result.info {
set_info(&mut exist_asset.info, info);
}
// TODO set info { copied: true, sourceFilename }
exist_asset.info.source_filename = Some(result.source_filename.to_string());
exist_asset.info.copied = Some(true);
} else {
let mut asset_info = Default::default();
let mut asset_info = AssetInfo {
source_filename: Some(result.source_filename.to_string()),
copied: Some(true),
..Default::default()
};

if let Some(info) = result.info {
set_info(&mut asset_info, info);
}
Expand Down
1 change: 1 addition & 0 deletions packages/rspack/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3494,6 +3494,7 @@ type KnownAssetInfo = {
chunkhash?: string | string[];
contenthash?: string | string[];
sourceFilename?: string;
copied?: boolean;
size?: number;
development?: boolean;
hotModuleReplacement?: boolean;
Expand Down
3 changes: 3 additions & 0 deletions packages/rspack/src/stats/DefaultStatsPrinterPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ const SIMPLE_PRINTERS: Record<
: `from: ${sourceFilename}`
)
: undefined,
"asset.info.copied": (copied, { green, formatFlag }) =>
copied ? green(formatFlag("copied")) : undefined,
"asset.info.development": (development, { green, formatFlag }) =>
development ? green(formatFlag("dev")) : undefined,
"asset.info.hotModuleReplacement": (
Expand Down Expand Up @@ -779,6 +781,7 @@ const PREFERRED_ORDERS: Record<string, string[]> = {
"asset.info": [
"immutable",
"sourceFilename",
"copied",
"javascriptModule",
"development",
"hotModuleReplacement"
Expand Down
1 change: 1 addition & 0 deletions packages/rspack/src/stats/statsFactoryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export type KnownAssetInfo = {
// modulehash?: string | string[];
contenthash?: string | string[];
sourceFilename?: string;
copied?: boolean;
size?: number;
development?: boolean;
hotModuleReplacement?: boolean;
Expand Down
8 changes: 6 additions & 2 deletions packages/rspack/src/util/AssetInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class JsAssetInfo {
contenthash,
javascriptModule,
sourceFilename,
copied,
extras
} = jsAssetInfo;
return {
Expand All @@ -29,7 +30,8 @@ class JsAssetInfo {
chunkhash,
contenthash,
javascriptModule,
sourceFilename
sourceFilename,
copied
};
}

Expand All @@ -45,6 +47,7 @@ class JsAssetInfo {
contenthash = [],
javascriptModule,
sourceFilename,
copied,
...extras
} = assetInfo;
extras = extras ?? {};
Expand All @@ -59,7 +62,8 @@ class JsAssetInfo {
contenthash,
extras,
javascriptModule,
sourceFilename
sourceFilename,
copied
};
}
}
Expand Down
16 changes: 11 additions & 5 deletions tests/plugin-test/copy-plugin/CopyPlugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ describe("CopyPlugin", () => {
});

it("should copy a file to a new file", done => {
runEmit({
run({
expectedAssetKeys: ["newfile.txt"],
patterns: [
{
Expand All @@ -155,6 +155,12 @@ describe("CopyPlugin", () => {
}
]
})
.then(({ stats, compilation }) => {
const assetInfo = stats.compilation.getAsset("newfile.txt");
expect(assetInfo.info.sourceFilename).toBe("file.txt");
expect(assetInfo.name).toBe("newfile.txt");
expect(compilation.assets["newfile.txt"]).toBeDefined();
})
.then(done)
.catch(done);
});
Expand Down Expand Up @@ -306,7 +312,7 @@ describe("CopyPlugin", () => {
})
.then(({ stats }) => {
for (const name of expectedAssetKeys) {
const info = stats.compilation.assetsInfo.get(name);
const { info } = stats.compilation.getAsset(name);

expect(info.copied).toBe(true);

Expand Down Expand Up @@ -350,7 +356,7 @@ describe("CopyPlugin", () => {
})
.then(({ stats }) => {
for (const name of expectedAssetKeys) {
const info = stats.compilation.assetsInfo.get(name);
const { info } = stats.compilation.getAsset(name);

expect(info.immutable).toBe(true);

Expand All @@ -363,7 +369,7 @@ describe("CopyPlugin", () => {
.catch(done);
});

it.skip('should copy files and print "copied" in the string representation ', done => {
it('should copy files and print "copied" in the string representation ', done => {
expect.assertions(1);

const expectedAssetKeys = [
Expand Down Expand Up @@ -524,7 +530,7 @@ describe("CopyPlugin", () => {
{
from: path.resolve(__dirname, "./fixtures/directory"),
to: () => {
return 'directory';
return "directory";
}
}
]
Expand Down

2 comments on commit 636d37d

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-10-14 d38945b) Current Change
10000_development-mode + exec 2.11 s ± 19 ms 2.1 s ± 33 ms -0.10 %
10000_development-mode_hmr + exec 662 ms ± 8.2 ms 661 ms ± 14 ms -0.29 %
10000_production-mode + exec 2.66 s ± 21 ms 2.65 s ± 35 ms -0.36 %
arco-pro_development-mode + exec 1.77 s ± 63 ms 1.77 s ± 83 ms -0.02 %
arco-pro_development-mode_hmr + exec 426 ms ± 2.6 ms 426 ms ± 1 ms +0.06 %
arco-pro_production-mode + exec 3.1 s ± 106 ms 3.08 s ± 71 ms -0.59 %
arco-pro_production-mode_generate-package-json-webpack-plugin + exec 3.13 s ± 85 ms 3.12 s ± 84 ms -0.25 %
threejs_development-mode_10x + exec 1.65 s ± 14 ms 1.64 s ± 18 ms -0.45 %
threejs_development-mode_10x_hmr + exec 788 ms ± 7.9 ms 791 ms ± 8.4 ms +0.30 %
threejs_production-mode_10x + exec 4.98 s ± 32 ms 4.99 s ± 24 ms +0.21 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ✅ success
_selftest ✅ success
rspress ✅ success
rslib ✅ success
rsbuild ✅ success
examples ✅ success
devserver ❌ failure

Please sign in to comment.