Unverified Commit ff865573 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: content hash polyfill sourcemap file (#94548)

The sourcemap file of the polyfill chunk wasn't actually content hashed.
it just used the name of the corresponding JS chunk.
It was still emitted into `_next/static/immutable`. So it wasn't
actually immutable.

Now, it's content hashed (and this also removed the ad-hoc sourcemap
construction from app.rs and instead uses the usual pattern that all
other chunks use to reference their corresponding sourcemap chunks).


We didn't catch this because it only happens with
`nextConfig.productionBrowserSourceMaps: true` which isn't the default.
The test now covers this as well
parent 0bded2db
...@@ -52,7 +52,7 @@ use turbopack_core::{ ...@@ -52,7 +52,7 @@ use turbopack_core::{
asset::AssetContent, asset::AssetContent,
chunk::{ chunk::{
ChunkGroupResult, ChunkingContext, ChunkingContextExt, EvaluatableAsset, EvaluatableAssets, ChunkGroupResult, ChunkingContext, ChunkingContextExt, EvaluatableAsset, EvaluatableAssets,
SourceMapsType, availability_info::AvailabilityInfo, availability_info::AvailabilityInfo,
}, },
file_source::FileSource, file_source::FileSource,
ident::{AssetIdent, Layer}, ident::{AssetIdent, Layer},
...@@ -66,8 +66,6 @@ use turbopack_core::{ ...@@ -66,8 +66,6 @@ use turbopack_core::{
reference::all_assets_from_entries, reference::all_assets_from_entries,
reference_type::{CommonJsReferenceSubType, CssReferenceSubType, ReferenceTypeCondition}, reference_type::{CommonJsReferenceSubType, CssReferenceSubType, ReferenceTypeCondition},
resolve::{ResolveErrorMode, origin::PlainResolveOrigin, parse::Request, pattern::Pattern}, resolve::{ResolveErrorMode, origin::PlainResolveOrigin, parse::Request, pattern::Pattern},
source::Source,
source_map::SourceMapAsset,
virtual_output::VirtualOutputAsset, virtual_output::VirtualOutputAsset,
}; };
use turbopack_ecmascript::single_file_ecmascript_output::SingleFileEcmascriptOutput; use turbopack_ecmascript::single_file_ecmascript_output::SingleFileEcmascriptOutput;
...@@ -1395,43 +1393,20 @@ impl AppEndpoint { ...@@ -1395,43 +1393,20 @@ impl AppEndpoint {
let polyfill_output_asset = if matches!(this.ty, AppEndpointType::Page { .. }) { let polyfill_output_asset = if matches!(this.ty, AppEndpointType::Page { .. }) {
// polyfill-nomodule.js is a pre-compiled asset distributed as part of next // polyfill-nomodule.js is a pre-compiled asset distributed as part of next
let next_package = get_next_package(project.project_path().owned().await?).await?; let next_package = get_next_package(project.project_path().owned().await?).await?;
let polyfill_source_path = let polyfill_source =
next_package.join("dist/build/polyfills/polyfill-nomodule.js")?; FileSource::new(next_package.join("dist/build/polyfills/polyfill-nomodule.js")?);
let polyfill_source = FileSource::new(polyfill_source_path.clone());
let polyfill_output_path = client_chunking_context
.chunk_path(
Some(Vc::upcast(polyfill_source)),
polyfill_source.ident(),
None,
rcstr!(".js"),
)
.owned()
.await?;
let polyfill_output = SingleFileEcmascriptOutput::new( let polyfill_output_asset = ResolvedVc::upcast(
polyfill_output_path.clone(), SingleFileEcmascriptOutput::new(
polyfill_source_path, *client_chunking_context,
Vc::upcast(polyfill_source), Vc::upcast(polyfill_source),
) )
.to_resolved() .to_resolved()
.await?; .await?,
);
let polyfill_output_asset = ResolvedVc::upcast(polyfill_output);
client_assets.insert(polyfill_output_asset); client_assets.insert(polyfill_output_asset);
let client_source_maps = project
.next_config()
.client_source_maps(project.next_mode())
.await?;
if *client_source_maps != SourceMapsType::None {
let polyfill_source_map_asset = SourceMapAsset::new_fixed(
polyfill_output_path.clone(),
*ResolvedVc::upcast(polyfill_output),
)
.to_resolved()
.await?;
client_assets.insert(ResolvedVc::upcast(polyfill_source_map_asset));
}
Some(polyfill_output_asset) Some(polyfill_output_asset)
} else { } else {
None None
......
...@@ -9,7 +9,10 @@ module.exports = { ...@@ -9,7 +9,10 @@ module.exports = {
supportsImmutableAssets: process.env.NEXT_DEPLOYMENT_ID_IMMUTABLE supportsImmutableAssets: process.env.NEXT_DEPLOYMENT_ID_IMMUTABLE
? true ? true
: false, : false,
serverSourceMaps: true,
}, },
adapterPath: adapterPath:
process.env.NEXT_ADAPTER_PATH ?? require.resolve('./my-adapter.mjs'), process.env.NEXT_ADAPTER_PATH ?? require.resolve('./my-adapter.mjs'),
// To generate all sourcemaps and ensure that they are content hashed
productionBrowserSourceMaps: true,
} }
...@@ -201,7 +201,10 @@ describe.each([ ...@@ -201,7 +201,10 @@ describe.each([
expect(immutableAssets).not.toBeEmpty() expect(immutableAssets).not.toBeEmpty()
expect(immutableAssets).toSatisfyAll( expect(immutableAssets).toSatisfyAll(
(f) => (f) =>
// Should be same hash as in the filename, for better build performance // Should be same hash as in the filename, for better build performance.
// This check also ensure that we don't accidentally forget to content hash sourcemap
// files (i.e. 0cz1d0mv5g_q7.js is content hashed, but 0cz1d0mv5g_q7.js.map is not a
// content hash of itself)..
f.immutableHash && f.pathname.includes(f.immutableHash.slice(0, 13)) f.immutableHash && f.pathname.includes(f.immutableHash.slice(0, 13))
) )
}) })
......
...@@ -3,13 +3,15 @@ use std::sync::Arc; ...@@ -3,13 +3,15 @@ use std::sync::Arc;
use anyhow::Result; use anyhow::Result;
use swc_core::common::{BytePos, FileName, LineCol, SourceMap}; use swc_core::common::{BytePos, FileName, LineCol, SourceMap};
use tokio::io::AsyncReadExt; use tokio::io::AsyncReadExt;
use turbo_tasks::{ResolvedVc, Vc}; use turbo_rcstr::rcstr;
use turbo_tasks::{ResolvedVc, ValueToStringRef, Vc};
use turbo_tasks_fs::{File, FileContent, FileSystemPath, rope::Rope}; use turbo_tasks_fs::{File, FileContent, FileSystemPath, rope::Rope};
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
output::{OutputAsset, OutputAssetsReference}, chunk::ChunkingContext,
output::{OutputAsset, OutputAssets, OutputAssetsReference, OutputAssetsWithReferenced},
source::Source, source::Source,
source_map::GenerateSourceMap, source_map::{GenerateSourceMap, SourceMapAsset},
}; };
use crate::parse::generate_js_source_map; use crate::parse::generate_js_source_map;
...@@ -18,21 +20,35 @@ use crate::parse::generate_js_source_map; ...@@ -18,21 +20,35 @@ use crate::parse::generate_js_source_map;
/// map to the original file. /// map to the original file.
#[turbo_tasks::value] #[turbo_tasks::value]
pub struct SingleFileEcmascriptOutput { pub struct SingleFileEcmascriptOutput {
output_path: FileSystemPath, chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
source_path: FileSystemPath,
source: ResolvedVc<Box<dyn Source>>, source: ResolvedVc<Box<dyn Source>>,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl OutputAsset for SingleFileEcmascriptOutput { impl SingleFileEcmascriptOutput {
#[turbo_tasks::function] #[turbo_tasks::function]
fn path(&self) -> Vc<FileSystemPath> { async fn source_map(self: Vc<Self>) -> Result<Vc<SourceMapAsset>> {
self.output_path.clone().cell() let this = self.await?;
Ok(SourceMapAsset::new(
*this.chunking_context,
this.source.ident(),
Vc::upcast(self),
))
} }
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl OutputAssetsReference for SingleFileEcmascriptOutput {} impl OutputAsset for SingleFileEcmascriptOutput {
#[turbo_tasks::function]
fn path(&self) -> Vc<FileSystemPath> {
self.chunking_context.chunk_path(
Some(Vc::upcast(*self.source)),
self.source.ident(),
None,
rcstr!(".js"),
)
}
}
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl Asset for SingleFileEcmascriptOutput { impl Asset for SingleFileEcmascriptOutput {
...@@ -46,14 +62,12 @@ impl Asset for SingleFileEcmascriptOutput { ...@@ -46,14 +62,12 @@ impl Asset for SingleFileEcmascriptOutput {
impl SingleFileEcmascriptOutput { impl SingleFileEcmascriptOutput {
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn new( pub fn new(
output_path: FileSystemPath, chunking_context: ResolvedVc<Box<dyn ChunkingContext>>,
source_path: FileSystemPath,
source: ResolvedVc<Box<dyn Source>>, source: ResolvedVc<Box<dyn Source>>,
) -> Vc<SingleFileEcmascriptOutput> { ) -> Vc<SingleFileEcmascriptOutput> {
SingleFileEcmascriptOutput { SingleFileEcmascriptOutput {
output_path,
source_path,
source, source,
chunking_context,
} }
.cell() .cell()
} }
...@@ -87,11 +101,17 @@ impl GenerateSourceMap for SingleFileEcmascriptOutput { ...@@ -87,11 +101,17 @@ impl GenerateSourceMap for SingleFileEcmascriptOutput {
pos += line.len() as u32; pos += line.len() as u32;
} }
let source_path = self
.source
.ident()
.await?
.path
.to_string_ref()
.await?
.to_string();
let sm: Arc<SourceMap> = Default::default(); let sm: Arc<SourceMap> = Default::default();
sm.new_source_file( sm.new_source_file(FileName::Custom(source_path).into(), file_source);
FileName::Custom(self.source_path.to_string()).into(),
file_source,
);
let map = generate_js_source_map( let map = generate_js_source_map(
&*sm, &*sm,
...@@ -104,3 +124,26 @@ impl GenerateSourceMap for SingleFileEcmascriptOutput { ...@@ -104,3 +124,26 @@ impl GenerateSourceMap for SingleFileEcmascriptOutput {
Ok(FileContent::Content(File::from(map)).cell()) Ok(FileContent::Content(File::from(map)).cell())
} }
} }
#[turbo_tasks::value_impl]
impl OutputAssetsReference for SingleFileEcmascriptOutput {
#[turbo_tasks::function]
async fn references(self: Vc<Self>) -> Result<Vc<OutputAssetsWithReferenced>> {
let this = self.await?;
let include_source_map = *this
.chunking_context
.reference_chunk_source_maps(Vc::upcast(self))
.await?;
let references = if include_source_map {
Vc::cell(vec![ResolvedVc::upcast(
self.source_map().to_resolved().await?,
)])
} else {
OutputAssets::empty()
};
Ok(OutputAssetsWithReferenced::from_assets(references))
}
}
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