Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rspack_plugin_copy): Set source_filename and copied flag when copying an asset #8077

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
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 @@ -3602,6 +3602,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
Loading