1- import  type  {  RollupSucraseOptions  }  from  '@rollup/plugin-sucrase' ; 
21import  sucrase  from  '@rollup/plugin-sucrase' ; 
3- import  {  logger  }  from  '@sentry/utils' ; 
42import  *  as  path  from  'path' ; 
53import  type  {  InputOptions  as  RollupInputOptions ,  OutputOptions  as  RollupOutputOptions  }  from  'rollup' ; 
64import  {  rollup  }  from  'rollup' ; 
75
8- const  getRollupInputOptions : ( proxyPath : string ,  resourcePath : string )  =>  RollupInputOptions  =  ( 
9-   proxyPath , 
10-   resourcePath , 
11- )  =>  ( { 
6+ const  getRollupInputOptions  =  ( proxyPath : string ,  userModulePath : string ) : RollupInputOptions  =>  ( { 
127  input : proxyPath , 
8+ 
139  plugins : [ 
14-     // For some reason, even though everything in `RollupSucraseOptions` besides `transforms` is supposed to be 
15-     // optional, TS complains that there are a bunch of missing properties (hence the typecast). Similar to 
16-     // https://github.com/microsoft/TypeScript/issues/20722, though that's been fixed. (In this case it's an interface 
17-     // exporting a `Pick` picking optional properties which is turning them required somehow.)' 
1810    sucrase ( { 
1911      transforms : [ 'jsx' ,  'typescript' ] , 
20-     }   as   unknown   as   RollupSucraseOptions ) , 
12+     } ) , 
2113  ] , 
2214
2315  // We want to process as few files as possible, so as not to slow down the build any more than we have to. We need the 
2416  // proxy module (living in the temporary file we've created) and the file we're wrapping not to be external, because 
2517  // otherwise they won't be processed. (We need Rollup to process the former so that we can use the code, and we need 
2618  // it to process the latter so it knows what exports to re-export from the proxy module.) Past that, we don't care, so 
2719  // don't bother to process anything else. 
28-   external : importPath  =>  importPath  !==  proxyPath  &&  importPath  !==  resourcePath , 
20+   external : importPath  =>  importPath  !==  proxyPath  &&  importPath  !==  userModulePath , 
2921
3022  // Prevent rollup from stressing out about TS's use of global `this` when polyfilling await. (TS will polyfill if the 
3123  // user's tsconfig `target` is set to anything before `es2017`. See https://stackoverflow.com/a/72822340 and 
@@ -48,9 +40,8 @@ const getRollupInputOptions: (proxyPath: string, resourcePath: string) => Rollup
4840  //     rather than the expected `export { helperFunc } from '../../utils/helper'`, thereby causing a build error in 
4941  //     nextjs.. 
5042  // 
51-   // It's not 100% clear why, but telling it not to do the conversion back from absolute to relative (by setting 
52-   // `makeAbsoluteExternalsRelative` to `false`) seems to also prevent it from going from relative to absolute in the 
53-   // first place, with the result that the path remains untouched (which is what we want.) 
43+   // Setting `makeAbsoluteExternalsRelative` to `false` prevents all of the above by causing Rollup to ignore imports of 
44+   // externals entirely, with the result that their paths remain untouched (which is what we want). 
5445  makeAbsoluteExternalsRelative : false , 
5546} ) ; 
5647
@@ -65,23 +56,15 @@ const rollupOutputOptions: RollupOutputOptions = {
6556 * Use Rollup to process the proxy module file (located at `tempProxyFilePath`) in order to split its `export * from 
6657 * '<wrapped file>'` call into individual exports (which nextjs seems to need). 
6758 * 
59+  * Note: Any errors which occur are handled by the proxy loader which calls this function. 
60+  * 
6861 * @param  tempProxyFilePath The path to the temporary file containing the proxy module code 
69-  * @param  resourcePath  The path to the file being wrapped 
70-  * @returns  The processed proxy module code, or undefined if an error occurs  
62+  * @param  userModulePath  The path to the file being wrapped 
63+  * @returns  The processed proxy module code 
7164 */ 
72- export  async  function  rollupize ( tempProxyFilePath : string ,  resourcePath : string ) : Promise < string  |  undefined >  { 
73-   let  finalBundle ; 
74- 
75-   try  { 
76-     const  intermediateBundle  =  await  rollup ( getRollupInputOptions ( tempProxyFilePath ,  resourcePath ) ) ; 
77-     finalBundle  =  await  intermediateBundle . generate ( rollupOutputOptions ) ; 
78-   }  catch  ( err )  { 
79-     __DEBUG_BUILD__  && 
80-       logger . warn ( 
81-         `Could not wrap ${ resourcePath } ${ err }  , 
82-       ) ; 
83-     return  undefined ; 
84-   } 
65+ export  async  function  rollupize ( tempProxyFilePath : string ,  userModulePath : string ) : Promise < string >  { 
66+   const  intermediateBundle  =  await  rollup ( getRollupInputOptions ( tempProxyFilePath ,  userModulePath ) ) ; 
67+   const  finalBundle  =  await  intermediateBundle . generate ( rollupOutputOptions ) ; 
8568
8669  // The module at index 0 is always the entrypoint, which in this case is the proxy module. 
8770  let  {  code }  =  finalBundle . output [ 0 ] ; 
@@ -92,12 +75,12 @@ export async function rollupize(tempProxyFilePath: string, resourcePath: string)
9275  // square brackets into underscores. Further, Rollup adds file extensions to bare-path-type import and export sources. 
9376  // Because it assumes that everything will have already been processed, it always uses `.js` as the added extension. 
9477  // We need to restore the original name and extension so that Webpack will be able to find the wrapped file. 
95-   const  resourceFilename  =  path . basename ( resourcePath ) ; 
96-   const  mutatedResourceFilename  =  resourceFilename 
78+   const  userModuleFilename  =  path . basename ( userModulePath ) ; 
79+   const  mutatedUserModuleFilename  =  userModuleFilename 
9780    // `[\\[\\]]` is the character class containing `[` and `]` 
9881    . replace ( new  RegExp ( '[\\[\\]]' ,  'g' ) ,  '_' ) 
9982    . replace ( / ( j s x ? | t s x ? ) $ / ,  'js' ) ; 
100-   code  =  code . replace ( new  RegExp ( mutatedResourceFilename ,  'g' ) ,  resourceFilename ) ; 
83+   code  =  code . replace ( new  RegExp ( mutatedUserModuleFilename ,  'g' ) ,  userModuleFilename ) ; 
10184
10285  return  code ; 
10386} 
0 commit comments