1
- import type { RollupSucraseOptions } from '@rollup/plugin-sucrase' ;
2
1
import sucrase from '@rollup/plugin-sucrase' ;
3
- import { logger } from '@sentry/utils' ;
4
2
import * as path from 'path' ;
5
3
import type { InputOptions as RollupInputOptions , OutputOptions as RollupOutputOptions } from 'rollup' ;
6
4
import { rollup } from 'rollup' ;
7
5
8
- const getRollupInputOptions : ( proxyPath : string , resourcePath : string ) => RollupInputOptions = (
9
- proxyPath ,
10
- resourcePath ,
11
- ) => ( {
6
+ const getRollupInputOptions = ( proxyPath : string , userModulePath : string ) : RollupInputOptions => ( {
12
7
input : proxyPath ,
8
+
13
9
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.)'
18
10
sucrase ( {
19
11
transforms : [ 'jsx' , 'typescript' ] ,
20
- } as unknown as RollupSucraseOptions ) ,
12
+ } ) ,
21
13
] ,
22
14
23
15
// 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
24
16
// proxy module (living in the temporary file we've created) and the file we're wrapping not to be external, because
25
17
// otherwise they won't be processed. (We need Rollup to process the former so that we can use the code, and we need
26
18
// it to process the latter so it knows what exports to re-export from the proxy module.) Past that, we don't care, so
27
19
// don't bother to process anything else.
28
- external : importPath => importPath !== proxyPath && importPath !== resourcePath ,
20
+ external : importPath => importPath !== proxyPath && importPath !== userModulePath ,
29
21
30
22
// Prevent rollup from stressing out about TS's use of global `this` when polyfilling await. (TS will polyfill if the
31
23
// 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
48
40
// rather than the expected `export { helperFunc } from '../../utils/helper'`, thereby causing a build error in
49
41
// nextjs..
50
42
//
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).
54
45
makeAbsoluteExternalsRelative : false ,
55
46
} ) ;
56
47
@@ -65,23 +56,15 @@ const rollupOutputOptions: RollupOutputOptions = {
65
56
* Use Rollup to process the proxy module file (located at `tempProxyFilePath`) in order to split its `export * from
66
57
* '<wrapped file>'` call into individual exports (which nextjs seems to need).
67
58
*
59
+ * Note: Any errors which occur are handled by the proxy loader which calls this function.
60
+ *
68
61
* @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
71
64
*/
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 } . An error occurred while processing the proxy module template:\n${ 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 ) ;
85
68
86
69
// The module at index 0 is always the entrypoint, which in this case is the proxy module.
87
70
let { code } = finalBundle . output [ 0 ] ;
@@ -92,12 +75,12 @@ export async function rollupize(tempProxyFilePath: string, resourcePath: string)
92
75
// square brackets into underscores. Further, Rollup adds file extensions to bare-path-type import and export sources.
93
76
// Because it assumes that everything will have already been processed, it always uses `.js` as the added extension.
94
77
// 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
97
80
// `[\\[\\]]` is the character class containing `[` and `]`
98
81
. replace ( new RegExp ( '[\\[\\]]' , 'g' ) , '_' )
99
82
. 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 ) ;
101
84
102
85
return code ;
103
86
}
0 commit comments