Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit polyfill-nomodule.js into the build manifest polyfillFiles #65223

Merged
merged 4 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions packages/next-swc/crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use next_core::{
get_entrypoints, Entrypoint as AppEntrypoint, Entrypoints as AppEntrypoints, LoaderTree,
MetadataItem,
},
get_edge_resolve_options_context,
get_edge_resolve_options_context, get_next_package,
next_app::{
app_client_references_chunks::get_app_server_reference_modules,
get_app_client_references_chunks, get_app_client_shared_chunk_group, get_app_page_entry,
Expand Down Expand Up @@ -45,10 +45,14 @@ use turbopack_binding::{
turbopack::{
core::{
asset::{Asset, AssetContent},
chunk::{availability_info::AvailabilityInfo, ChunkingContextExt, EvaluatableAssets},
chunk::{
availability_info::AvailabilityInfo, ChunkingContext, ChunkingContextExt,
EvaluatableAssets,
},
file_source::FileSource,
module::Module,
output::{OutputAsset, OutputAssets},
source::Source,
virtual_output::VirtualOutputAsset,
},
nodejs::EntryChunkGroupResult,
Expand Down Expand Up @@ -894,8 +898,26 @@ impl AppEndpoint {
));
server_assets.push(app_build_manifest_output);

// polyfill-nomodule.js is a pre-compiled asset distributed as part of next,
// load it as a RawModule.
let next_package = get_next_package(this.app_project.project().project_path());
let polyfill_source = FileSource::new(
next_package.join("dist/build/polyfills/polyfill-nomodule.js".to_string()),
);
let polyfill_output_path =
client_chunking_context.chunk_path(polyfill_source.ident(), ".js".to_string());
let polyfill_output_asset =
VirtualOutputAsset::new(polyfill_output_path, polyfill_source.content());
let polyfill_client_path = client_relative_path_ref
.get_path_to(&*polyfill_output_path.await?)
.context("failed to resolve client-relative path to polyfill")?
.to_string();
let polyfill_client_paths = vec![polyfill_client_path];
client_assets.push(Vc::upcast(polyfill_output_asset));

let build_manifest = BuildManifest {
root_main_files: client_shared_chunks_paths,
polyfill_files: polyfill_client_paths,
..Default::default()
};
let build_manifest_output = Vc::upcast(VirtualOutputAsset::new(
Expand Down
1 change: 1 addition & 0 deletions packages/next-swc/crates/next-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub use next_edge::context::{
get_edge_chunking_context, get_edge_chunking_context_with_client_assets,
get_edge_compile_time_info, get_edge_resolve_options_context,
};
pub use next_import_map::get_next_package;
pub use page_loader::{create_page_loader_entry_module, PageLoaderAsset};
use turbopack_binding::{turbo, turbopack};
pub use util::{get_asset_path_from_pathname, pathname_for_path, PathType};
Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/server/dev/turbopack/manifest-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ export class TurbopackManifestLoader {
for (const m of manifests) {
Object.assign(manifest.pages, m.pages)
if (m.rootMainFiles.length) manifest.rootMainFiles = m.rootMainFiles
// polyfillFiles should always be the same, so we can overwrite instead of actually merging
if (m.polyfillFiles.length) manifest.polyfillFiles = m.polyfillFiles
}
return manifest
}
Expand Down
4 changes: 3 additions & 1 deletion test/e2e/app-dir/app/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,9 @@ describe('app dir - basic', () => {
it('should serve polyfills for browsers that do not support modules', async () => {
const html = await next.render('/dashboard/index')
expect(html).toMatch(
/<script src="\/_next\/static\/chunks\/polyfills(-\w+)?\.js" noModule="">/
isTurbopack
? /<script src="\/_next\/static\/chunks\/[\w-]*polyfill-nomodule\.js" noModule="">/
: /<script src="\/_next\/static\/chunks\/polyfills(-\w+)?\.js" noModule="">/
)
})
}
Expand Down
Loading