@@ -32,6 +32,7 @@ use turbopack_core::{
3232 asset:: { children_from_asset_references, content_to_details, IntrospectableAssetVc } ,
3333 Introspectable , IntrospectableChildrenVc , IntrospectableVc ,
3434 } ,
35+ issue:: { code_gen:: CodeGenerationIssue , IssueSeverity } ,
3536 reference:: { AssetReferenceVc , AssetReferencesVc } ,
3637 source_map:: { GenerateSourceMap , GenerateSourceMapVc , OptionSourceMapVc , SourceMapVc } ,
3738 version:: {
@@ -490,7 +491,36 @@ impl EcmascriptChunkContentEntryVc {
490491 #[ turbo_tasks:: function]
491492 async fn new ( chunk_item : EcmascriptChunkItemVc ) -> Result < Self > {
492493 let content = chunk_item. content ( ) ;
493- let factory = module_factory ( content) ;
494+ let factory = match module_factory ( content) . resolve ( ) . await {
495+ Ok ( factory) => factory,
496+ Err ( error) => {
497+ let id = chunk_item. id ( ) . to_string ( ) . await ;
498+ let id = id. as_ref ( ) . map_or_else ( |_| "unknown" , |id| & * * id) ;
499+ let mut error_message =
500+ format ! ( "An error occurred while generating the chunk item {}" , id) ;
501+ for err in error. chain ( ) {
502+ write ! ( error_message, "\n at {}" , err) ?;
503+ }
504+ let js_error_message = serde_json:: to_string ( & error_message) ?;
505+ let issue = CodeGenerationIssue {
506+ severity : IssueSeverity :: Error . cell ( ) ,
507+ path : chunk_item. related_path ( ) ,
508+ title : StringVc :: cell ( "Code generation for chunk item errored" . to_string ( ) ) ,
509+ message : StringVc :: cell ( error_message) ,
510+ }
511+ . cell ( ) ;
512+ issue. as_issue ( ) . emit ( ) ;
513+ let mut code = CodeBuilder :: default ( ) ;
514+ code += "(() => {{\n \n " ;
515+ write ! (
516+ code,
517+ "throw new Error({error});\n " ,
518+ error = & js_error_message
519+ ) ?;
520+ code += "\n }})" ;
521+ code. build ( ) . cell ( )
522+ }
523+ } ;
494524 let id = chunk_item. id ( ) . await ?;
495525 let code = factory. await ?;
496526 let hash = hash_xxh3_hash64 ( code. source_code ( ) ) ;
@@ -1196,6 +1226,7 @@ pub struct EcmascriptChunkItemOptions {
11961226
11971227#[ turbo_tasks:: value_trait]
11981228pub trait EcmascriptChunkItem : ChunkItem + ValueToString {
1229+ fn related_path ( & self ) -> FileSystemPathVc ;
11991230 fn content ( & self ) -> EcmascriptChunkItemContentVc ;
12001231 fn chunking_context ( & self ) -> ChunkingContextVc ;
12011232 fn id ( & self ) -> ModuleIdVc {
0 commit comments