-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
1,009 additions
and
40 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
use crate::{asset::AssetVc, context::AssetContextVc, resolve::ModulePartVc}; | ||
|
||
#[turbo_tasks::value_trait] | ||
pub trait CustomModuleType { | ||
fn create_module( | ||
&self, | ||
source: AssetVc, | ||
context: AssetContextVc, | ||
part: Option<ModulePartVc>, | ||
) -> AssetVc; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
[package] | ||
name = "turbopack-image" | ||
version = "0.1.0" | ||
description = "TBD" | ||
license = "MPL-2.0" | ||
edition = "2021" | ||
autobenches = false | ||
|
||
[lib] | ||
bench = false | ||
|
||
[features] | ||
avif = ["image/avif-decoder", "image/avif-encoder"] | ||
|
||
[dependencies] | ||
anyhow = { workspace = true } | ||
base64 = "0.21.0" | ||
image = { workspace = true, default-features = false, features = [ | ||
"webp", | ||
"png", | ||
"jpeg", | ||
"webp-encoder", | ||
] } | ||
indexmap = { workspace = true } | ||
serde = { workspace = true } | ||
turbo-tasks = { workspace = true } | ||
turbo-tasks-fs = { workspace = true } | ||
turbopack-core = { workspace = true } | ||
turbopack-ecmascript = { workspace = true } | ||
turbopack-static = { workspace = true } | ||
|
||
[build-dependencies] | ||
turbo-tasks-build = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
use turbo_tasks_build::generate_register; | ||
|
||
fn main() { | ||
generate_register(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
pub mod module_type; | ||
mod process; | ||
mod source; | ||
|
||
pub fn register() { | ||
turbo_tasks::register(); | ||
turbo_tasks_fs::register(); | ||
turbopack_core::register(); | ||
turbopack_ecmascript::register(); | ||
turbopack_static::register(); | ||
include!(concat!(env!("OUT_DIR"), "/register.rs")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use anyhow::Result; | ||
use indexmap::indexmap; | ||
use turbo_tasks::Value; | ||
use turbopack_core::{ | ||
asset::AssetVc, | ||
context::{AssetContext, AssetContextVc}, | ||
plugin::{CustomModuleType, CustomModuleTypeVc}, | ||
resolve::ModulePartVc, | ||
}; | ||
use turbopack_ecmascript::{ | ||
EcmascriptInputTransformsVc, EcmascriptModuleAssetType, EcmascriptModuleAssetVc, | ||
EcmascriptOptions, InnerAssetsVc, | ||
}; | ||
use turbopack_static::StaticModuleAssetVc; | ||
|
||
use crate::source::StructuredImageSourceAsset; | ||
|
||
#[turbo_tasks::value] | ||
pub struct StructuredImageModuleType {} | ||
|
||
#[turbo_tasks::value_impl] | ||
impl StructuredImageModuleTypeVc { | ||
#[turbo_tasks::function] | ||
pub fn new() -> Self { | ||
StructuredImageModuleTypeVc::cell(StructuredImageModuleType {}) | ||
} | ||
} | ||
|
||
#[turbo_tasks::value_impl] | ||
impl CustomModuleType for StructuredImageModuleType { | ||
#[turbo_tasks::function] | ||
async fn create_module( | ||
&self, | ||
source: AssetVc, | ||
context: AssetContextVc, | ||
_part: Option<ModulePartVc>, | ||
) -> Result<AssetVc> { | ||
let static_asset = StaticModuleAssetVc::new(source, context); | ||
Ok(EcmascriptModuleAssetVc::new_with_inner_assets( | ||
StructuredImageSourceAsset { image: source }.cell().into(), | ||
context, | ||
Value::new(EcmascriptModuleAssetType::Ecmascript), | ||
EcmascriptInputTransformsVc::empty(), | ||
Value::new(EcmascriptOptions { | ||
..Default::default() | ||
}), | ||
context.compile_time_info(), | ||
InnerAssetsVc::cell(indexmap!( | ||
"IMAGE".to_string() => static_asset.into() | ||
)), | ||
) | ||
.into()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
use std::io::Cursor; | ||
|
||
use anyhow::{bail, Result}; | ||
use base64::{display::Base64Display, engine::general_purpose::STANDARD}; | ||
use image::{ | ||
codecs::{ | ||
jpeg::JpegEncoder, | ||
png::{CompressionType, PngEncoder}, | ||
webp::{WebPEncoder, WebPQuality}, | ||
}, | ||
imageops::FilterType, | ||
GenericImageView, ImageEncoder, ImageFormat, | ||
}; | ||
use turbo_tasks_fs::{FileContent, FileContentVc}; | ||
|
||
#[turbo_tasks::value] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct ImageMetaData { | ||
width: u32, | ||
height: u32, | ||
#[serde(rename = "blurDataURL")] | ||
blur_data_url: Option<String>, | ||
blur_width: u32, | ||
blur_height: u32, | ||
} | ||
|
||
const BLUR_IMG_SIZE: u32 = 8; | ||
const BLUR_QUALITY: u8 = 70; | ||
|
||
#[turbo_tasks::function] | ||
pub async fn get_meta_data_and_blur_placeholder(content: FileContentVc) -> Result<ImageMetaDataVc> { | ||
let FileContent::Content(content) = &*content.await? else { | ||
bail!("Input image not found"); | ||
}; | ||
let bytes = content.content().to_bytes()?; | ||
let reader = image::io::Reader::new(Cursor::new(&bytes)); | ||
let reader = reader.with_guessed_format()?; | ||
let format = reader.format(); | ||
let image = reader.decode()?; | ||
let (width, height) = image.dimensions(); | ||
let (blur_data_url, blur_width, blur_height) = if matches!( | ||
format, | ||
// list should match next/client/image.tsx | ||
Some(ImageFormat::Png) | ||
| Some(ImageFormat::Jpeg) | ||
| Some(ImageFormat::WebP) | ||
| Some(ImageFormat::Avif) | ||
) { | ||
let small_image = image.resize(BLUR_IMG_SIZE, BLUR_IMG_SIZE, FilterType::Triangle); | ||
let mut buf = Vec::new(); | ||
let blur_width = small_image.width(); | ||
let blur_height = small_image.height(); | ||
let url = match format { | ||
Some(ImageFormat::Png) => { | ||
PngEncoder::new_with_quality( | ||
&mut buf, | ||
CompressionType::Best, | ||
image::codecs::png::FilterType::NoFilter, | ||
) | ||
.write_image( | ||
small_image.as_bytes(), | ||
blur_width, | ||
blur_height, | ||
small_image.color(), | ||
)?; | ||
format!( | ||
"data:image/png;base64,{}", | ||
Base64Display::new(&buf, &STANDARD) | ||
) | ||
} | ||
Some(ImageFormat::Jpeg) => { | ||
JpegEncoder::new_with_quality(&mut buf, BLUR_QUALITY).write_image( | ||
small_image.as_bytes(), | ||
blur_width, | ||
blur_height, | ||
small_image.color(), | ||
)?; | ||
format!( | ||
"data:image/jpeg;base64,{}", | ||
Base64Display::new(&buf, &STANDARD) | ||
) | ||
} | ||
Some(ImageFormat::WebP) => { | ||
WebPEncoder::new_with_quality(&mut buf, WebPQuality::lossy(BLUR_QUALITY)) | ||
.write_image( | ||
small_image.as_bytes(), | ||
blur_width, | ||
blur_height, | ||
small_image.color(), | ||
)?; | ||
format!( | ||
"data:image/webp;base64,{}", | ||
Base64Display::new(&buf, &STANDARD) | ||
) | ||
} | ||
#[cfg(feature = "avif")] | ||
Some(ImageFormat::Avif) => { | ||
use image::codecs::avif::AvifEncoder; | ||
AvifEncoder::new_with_speed_quality(&mut buf, 6, BLUR_QUALITY).write_image( | ||
small_image.as_bytes(), | ||
blur_width, | ||
blur_height, | ||
small_image.color(), | ||
)?; | ||
format!( | ||
"data:image/avif;base64,{}", | ||
Base64Display::new(&buf, &STANDARD) | ||
) | ||
} | ||
_ => unreachable!(), | ||
}; | ||
|
||
(Some(url), blur_width, blur_height) | ||
} else { | ||
(None, 0, 0) | ||
}; | ||
|
||
Ok(ImageMetaData { | ||
width, | ||
height, | ||
blur_data_url, | ||
blur_width, | ||
blur_height, | ||
} | ||
.cell()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::io::Write; | ||
|
||
use anyhow::{bail, Result}; | ||
use turbo_tasks::primitives::StringVc; | ||
use turbo_tasks_fs::{rope::RopeBuilder, FileContent}; | ||
use turbopack_core::{ | ||
asset::{Asset, AssetContent, AssetContentVc, AssetVc}, | ||
ident::AssetIdentVc, | ||
}; | ||
use turbopack_ecmascript::utils::StringifyJs; | ||
|
||
use crate::process::get_meta_data_and_blur_placeholder; | ||
|
||
fn modifier() -> StringVc { | ||
StringVc::cell("structured image object".to_string()) | ||
} | ||
|
||
#[turbo_tasks::value(shared)] | ||
pub struct StructuredImageSourceAsset { | ||
pub image: AssetVc, | ||
} | ||
|
||
#[turbo_tasks::value_impl] | ||
impl Asset for StructuredImageSourceAsset { | ||
#[turbo_tasks::function] | ||
fn ident(&self) -> AssetIdentVc { | ||
self.image.ident().with_modifier(modifier()) | ||
} | ||
|
||
#[turbo_tasks::function] | ||
async fn content(&self) -> Result<AssetContentVc> { | ||
let content = self.image.content().await?; | ||
let AssetContent::File(content) = *content else { | ||
bail!("Input source is not a file and can't be transformed into image information"); | ||
}; | ||
let mut result = RopeBuilder::from(""); | ||
let info = get_meta_data_and_blur_placeholder(content); | ||
let info = info.await?; | ||
writeln!(result, "import src from \"IMAGE\";",)?; | ||
writeln!( | ||
result, | ||
"export default {{ src, ...{} }}", | ||
StringifyJs(&*info) | ||
)?; | ||
Ok(AssetContent::File(FileContent::Content(result.build().into()).cell()).cell()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.