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