Skip to content

Commit

Permalink
Use base64 for binary results from Webpack loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Aug 2, 2024
1 parent f97feb2 commit 5c142be
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
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 turbopack/crates/turbopack-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ regex = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
#serde_qs = { workspace = true }
serde_with = { workspace = true, features = ["base64"] }
tokio = { workspace = true, features = ["full"] }
tracing = { workspace = true }
turbo-tasks = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ const transform = (
if (!result.result) return reject(new Error("No result from loaders"));
const [source, map] = result.result;
resolve({
source: Buffer.isBuffer(source) ? [...source] : source,
source: Buffer.isBuffer(source) ? {binary: source.toString('base64')} : source,
map:
typeof map === "string"
? map
Expand Down
17 changes: 14 additions & 3 deletions turbopack/crates/turbopack-node/src/transforms/webpack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use async_trait::async_trait;
use either::Either;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value as JsonValue};
use serde_with::serde_as;
use turbo_tasks::{
trace::TraceRawVcs, Completion, RcStr, TaskInput, TryJoinIterExt, Value, ValueToString, Vc,
};
Expand Down Expand Up @@ -53,13 +54,20 @@ use crate::{
AssetsForSourceMapping,
};

#[serde_as]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
struct BytesBase64 {
#[serde_as(as = "serde_with::base64::Base64")]
binary: Vec<u8>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
#[turbo_tasks::value(serialization = "custom")]
struct WebpackLoadersProcessingResult {
#[serde(with = "either::serde_untagged")]
#[turbo_tasks(trace_ignore)]
source: Either<RcStr, Vec<u8>>,
#[turbo_tasks(debug_ignore, trace_ignore)]
source: Either<RcStr, BytesBase64>,
map: Option<RcStr>,
#[turbo_tasks(trace_ignore)]
assets: Option<Vec<EmittedAsset>>,
Expand Down Expand Up @@ -253,7 +261,10 @@ impl WebpackLoadersProcessedAsset {
} else {
None
};
let file = either::for_both!(processed.source, source => File::from(source));
let file = match processed.source {
Either::Left(str) => File::from(str),
Either::Right(bytes) => File::from(bytes.binary),
};
let assets = emitted_assets_to_virtual_sources(processed.assets);
let content = AssetContent::File(FileContent::Content(file).cell()).cell();
Ok(ProcessWebpackLoadersResult {
Expand Down

0 comments on commit 5c142be

Please sign in to comment.