Unverified Commit ddc8f50d authored by Leah's avatar Leah Committed by GitHub

enable @vercel/og support for turbopack (#53917)

### What?

Now that we have wasm support (and support for `?module`) `@vercel/og` should be supported

### Turbopack updates

* https://github.com/vercel/turbo/pull/5677 
* https://github.com/vercel/turbo/pull/5660 

Closes WEB-138
parent 424b2053
......@@ -10,6 +10,7 @@ env:
TURBO_VERSION: 1.10.9
NODE_MAINTENANCE_VERSION: 16
NODE_LTS_VERSION: 18.16.1
CARGO_PROFILE_RELEASE_LTO: 'true'
jobs:
build:
......@@ -231,6 +232,7 @@ jobs:
options: >-
-e RUST_TOOLCHAIN=${{ env.RUST_TOOLCHAIN }}
-e CARGO_INCREMENTAL=${{ env.CARGO_INCREMENTAL }}
-e CARGO_PROFILE_RELEASE_LTO=${{ env.CARGO_PROFILE_RELEASE_LTO }}
-e CARGO_TERM_COLOR=${{ env.CARGO_TERM_COLOR }}
-e RUST_BACKTRACE=${{ env.RUST_BACKTRACE }}
-e CARGO_REGISTRIES_CRATES_IO_PROTOCOL=${{ env.CARGO_REGISTRIES_CRATES_IO_PROTOCOL }}
......
This diff is collapsed.
[workspace]
resolver = "2"
members = [
"scripts/send-trace-to-jaeger",
......@@ -22,8 +23,9 @@ opt-level = 2
[profile.dev.package."*"]
debug-assertions = false
[profile.release]
lto = true
# Set the settings for build scripts and proc-macros.
[profile.dev.build-override]
opt-level = 3
[workspace.dependencies]
# Workspace crates
......@@ -44,11 +46,11 @@ swc_core = { version = "0.79.40" }
testing = { version = "0.33.21" }
# Turbo crates
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230809.2" }
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230811.2" }
# [TODO]: need to refactor embed_directory! macro usages, as well as resolving turbo_tasks::function, macros..
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230809.2" }
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230811.2" }
# [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230809.2" }
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230811.2" }
# General Deps
......
......@@ -10,8 +10,8 @@
"check": "tsc --noEmit"
},
"dependencies": {
"@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230809.2",
"@vercel/turbopack-node": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230809.2",
"@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230811.2",
"@vercel/turbopack-node": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230811.2",
"anser": "^2.1.1",
"css.escape": "^1.5.1",
"next": "*",
......
......@@ -26,7 +26,6 @@ use turbopack_binding::{
resolve::{
options::{ImportMapResult, ImportMapping, ImportMappingReplacement},
parse::Request,
pattern::QueryMap,
ResolveResult,
},
virtual_source::VirtualSource,
......@@ -134,7 +133,7 @@ impl ImportMappingReplacement for NextFontGoogleReplacer {
export default fontData;
"#,
// Pass along whichever options we received to the css handler
qstring::QString::new(query.as_ref().unwrap().iter().collect()),
qstring::QString::from(&**query),
properties.font_family.await?,
properties
.weight
......@@ -366,25 +365,20 @@ async fn get_font_css_properties(
#[turbo_tasks::function]
async fn font_options_from_query_map(
query: Vc<QueryMap>,
query: Vc<String>,
font_data: Vc<FontData>,
) -> Result<Vc<NextFontGoogleOptions>> {
let query_map = &*query.await?;
// These are invariants from the next/font swc transform. Regular errors instead
// of Issues should be okay.
let query_map = query_map
.as_ref()
.context("next/font/google queries must exist")?;
let query_map = qstring::QString::from(&**query.await?);
if query_map.len() != 1 {
bail!("next/font/google queries must only have one entry");
bail!("next/font/google queries must have exactly one entry");
}
let Some((json, _)) = query_map.iter().next() else {
let Some((json, _)) = query_map.into_iter().next() else {
bail!("Expected one entry");
};
options_from_request(&parse_json_with_source_context(json)?, &*font_data.await?)
options_from_request(&parse_json_with_source_context(&json)?, &*font_data.await?)
.map(|o| NextFontGoogleOptions::new(Value::new(o)))
}
......
use anyhow::{bail, Context, Result};
use anyhow::{bail, Result};
use indoc::formatdoc;
use turbo_tasks::Vc;
use turbopack_binding::{
......@@ -11,7 +11,6 @@ use turbopack_binding::{
resolve::{
options::{ImportMapResult, ImportMapping, ImportMappingReplacement},
parse::Request,
pattern::QueryMap,
ResolveResult,
},
virtual_source::VirtualSource,
......@@ -97,7 +96,7 @@ impl ImportMappingReplacement for NextFontLocalReplacer {
export default fontData;
"#,
// Pass along whichever options we received to the css handler
qstring::QString::new(query_vc.await?.as_ref().unwrap().iter().collect()),
qstring::QString::from(&**query_vc.await?),
properties.font_family.await?,
properties
.weight
......@@ -222,22 +221,17 @@ async fn get_font_css_properties(
}
#[turbo_tasks::function]
async fn font_options_from_query_map(query: Vc<QueryMap>) -> Result<Vc<NextFontLocalOptions>> {
let query_map = &*query.await?;
// These are invariants from the next/font swc transform. Regular errors instead
// of Issues should be okay.
let query_map = query_map
.as_ref()
.context("next/font/local queries must exist")?;
async fn font_options_from_query_map(query: Vc<String>) -> Result<Vc<NextFontLocalOptions>> {
let query_map = qstring::QString::from(&**query.await?);
if query_map.len() != 1 {
bail!("next/font/local queries must only have one entry");
bail!("next/font/local queries have exactly one entry");
}
let Some((json, _)) = query_map.iter().next() else {
let Some((json, _)) = query_map.into_iter().next() else {
bail!("Expected one entry");
};
options_from_request(&parse_json_with_source_context(json)?)
options_from_request(&parse_json_with_source_context(&json)?)
.map(|o| NextFontLocalOptions::new(Value::new(o)))
}
use anyhow::{Context, Result};
use anyhow::Result;
use turbo_tasks::Vc;
use turbopack_binding::{
turbo::tasks_hash::hash_xxh3_hash64, turbopack::core::resolve::pattern::QueryMap,
};
use turbopack_binding::turbo::tasks_hash::hash_xxh3_hash64;
/// CSS properties and values for a given font variation. These are rendered as
/// values in both the returned JavaScript object and in the referenced css
......@@ -19,9 +17,8 @@ pub(crate) struct FontCssProperties {
/// next/font. Used to uniquely identify font requests for generated filenames
/// and scoped font family names.
#[turbo_tasks::function]
pub(crate) async fn get_request_hash(query_vc: Vc<QueryMap>) -> Result<Vc<u32>> {
let query = &*query_vc.await?;
let query = query.as_ref().context("Query map must be present")?;
pub(crate) async fn get_request_hash(query_vc: Vc<String>) -> Result<Vc<u32>> {
let query = qstring::QString::from(&**query_vc.await?);
let mut to_hash = vec![];
for (k, v) in query {
to_hash.push(k);
......
......@@ -21,7 +21,7 @@ use turbopack_binding::{
use crate::next_telemetry::ModuleFeatureTelemetry;
lazy_static! {
static ref UNSUPPORTED_PACKAGES: HashSet<&'static str> = ["@vercel/og"].into();
static ref UNSUPPORTED_PACKAGES: HashSet<&'static str> = [].into();
static ref UNSUPPORTED_PACKAGE_PATHS: HashSet<(&'static str, &'static str)> = [].into();
// Set of the features we want to track, following existing references in webpack/plugins/telemetry-plugin.
static ref FEATURE_MODULES: HashMap<&'static str, Vec<&'static str>> = HashMap::from([
......
......@@ -60,7 +60,7 @@ use turbopack_binding::{
core::{
environment::ServerAddr,
issue::{IssueReporter, IssueSeverity},
resolve::{parse::Request, pattern::QueryMap},
resolve::parse::Request,
server_fs::ServerFileSystem,
PROJECT_FILESYSTEM_NAME,
},
......@@ -349,10 +349,14 @@ async fn source(
let entry_requests = entry_requests
.iter()
.map(|r| match r {
EntryRequest::Relative(p) => Request::relative(Value::new(p.clone().into()), false),
EntryRequest::Module(m, p) => {
Request::module(m.clone(), Value::new(p.clone().into()), QueryMap::none())
EntryRequest::Relative(p) => {
Request::relative(Value::new(p.clone().into()), Vc::<String>::empty(), false)
}
EntryRequest::Module(m, p) => Request::module(
m.clone(),
Value::new(p.clone().into()),
Vc::<String>::empty(),
),
})
.collect();
......
......@@ -5,54 +5,48 @@ export class ImageResponse {
typeof import('next/dist/compiled/@vercel/og').ImageResponse
>
) {
// @ts-expect-error - process.turbopack is a custom property
if (process.turbopack) {
// TODO(sokra) enable this again when turbopack supports wasm
throw new Error("Turbopack doesn't support ImageResponse currently")
} else {
const readable = new ReadableStream({
async start(controller) {
const OGImageResponse: typeof import('next/dist/compiled/@vercel/og').ImageResponse =
// So far we have to manually determine which build to use,
// as the auto resolving is not working
(
await import(
process.env.NEXT_RUNTIME === 'edge'
? 'next/dist/compiled/@vercel/og/index.edge.js'
: 'next/dist/compiled/@vercel/og/index.node.js'
)
).ImageResponse
const imageResponse = new OGImageResponse(...args) as Response
const readable = new ReadableStream({
async start(controller) {
const OGImageResponse: typeof import('next/dist/compiled/@vercel/og').ImageResponse =
// So far we have to manually determine which build to use,
// as the auto resolving is not working
(
await import(
process.env.NEXT_RUNTIME === 'edge'
? 'next/dist/compiled/@vercel/og/index.edge.js'
: 'next/dist/compiled/@vercel/og/index.node.js'
)
).ImageResponse
const imageResponse = new OGImageResponse(...args) as Response
if (!imageResponse.body) {
return controller.close()
}
if (!imageResponse.body) {
return controller.close()
}
const reader = imageResponse.body!.getReader()
while (true) {
const { done, value } = await reader.read()
if (done) {
return controller.close()
}
controller.enqueue(value)
const reader = imageResponse.body!.getReader()
while (true) {
const { done, value } = await reader.read()
if (done) {
return controller.close()
}
},
})
controller.enqueue(value)
}
},
})
const options = args[1] || {}
const options = args[1] || {}
return new Response(readable, {
headers: {
'content-type': 'image/png',
'cache-control':
process.env.NODE_ENV === 'development'
? 'no-cache, no-store'
: 'public, immutable, no-transform, max-age=31536000',
...options.headers,
},
status: options.status,
statusText: options.statusText,
})
}
return new Response(readable, {
headers: {
'content-type': 'image/png',
'cache-control':
process.env.NODE_ENV === 'development'
? 'no-cache, no-store'
: 'public, immutable, no-transform, max-age=31536000',
...options.headers,
},
status: options.status,
statusText: options.statusText,
})
}
}
......@@ -1419,11 +1419,11 @@ importers:
packages/next-swc/crates/next-core/js:
dependencies:
'@vercel/turbopack-ecmascript-runtime':
specifier: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230809.2
version: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230809.2(react-refresh@0.12.0)(webpack@5.86.0)'
specifier: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230811.2
version: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230811.2(react-refresh@0.12.0)(webpack@5.86.0)'
'@vercel/turbopack-node':
specifier: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230809.2
version: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230809.2'
specifier: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230811.2
version: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230811.2'
anser:
specifier: ^2.1.1
version: 2.1.1
......@@ -26413,9 +26413,9 @@ packages:
/zwitch@2.0.4:
resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==}
'@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230809.2(react-refresh@0.12.0)(webpack@5.86.0)':
resolution: {tarball: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230809.2}
id: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230809.2'
'@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230811.2(react-refresh@0.12.0)(webpack@5.86.0)':
resolution: {tarball: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230811.2}
id: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-230811.2'
name: '@vercel/turbopack-ecmascript-runtime'
version: 0.0.0
dependencies:
......@@ -26426,8 +26426,8 @@ packages:
- webpack
dev: false
'@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230809.2':
resolution: {tarball: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230809.2}
'@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230811.2':
resolution: {tarball: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-node/js?turbopack-230811.2}
name: '@vercel/turbopack-node'
version: 0.0.0
dependencies:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment