1
+ import type { RollupSucraseOptions } from '@rollup/plugin-sucrase' ;
1
2
import sucrase from '@rollup/plugin-sucrase' ;
2
- import virtual from '@rollup/plugin-virtual' ;
3
3
import { logger } from '@sentry/utils' ;
4
4
import * as path from 'path' ;
5
5
import type { InputOptions as RollupInputOptions , OutputOptions as RollupOutputOptions } from 'rollup' ;
6
6
import { rollup } from 'rollup' ;
7
7
8
- const SENTRY_PROXY_MODULE_NAME = 'sentry-proxy-module' ;
9
-
10
- const getRollupInputOptions = ( userModulePath : string , proxyTemplateCode : string ) : RollupInputOptions => ( {
11
- input : SENTRY_PROXY_MODULE_NAME ,
12
-
8
+ const getRollupInputOptions : ( proxyPath : string , resourcePath : string ) => RollupInputOptions = (
9
+ proxyPath ,
10
+ resourcePath ,
11
+ ) => ( {
12
+ input : proxyPath ,
13
13
plugins : [
14
- virtual ( {
15
- [ SENTRY_PROXY_MODULE_NAME ] : proxyTemplateCode ,
16
- } ) ,
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.)'
17
18
sucrase ( {
18
19
transforms : [ 'jsx' , 'typescript' ] ,
19
- } ) ,
20
+ } as unknown as RollupSucraseOptions ) ,
20
21
] ,
21
22
22
23
// 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
23
24
// proxy module (living in the temporary file we've created) and the file we're wrapping not to be external, because
24
25
// otherwise they won't be processed. (We need Rollup to process the former so that we can use the code, and we need
25
26
// it to process the latter so it knows what exports to re-export from the proxy module.) Past that, we don't care, so
26
27
// don't bother to process anything else.
27
- external : importPath => importPath !== SENTRY_PROXY_MODULE_NAME && importPath !== userModulePath ,
28
+ external : importPath => importPath !== proxyPath && importPath !== resourcePath ,
28
29
29
30
// Prevent rollup from stressing out about TS's use of global `this` when polyfilling await. (TS will polyfill if the
30
31
// user's tsconfig `target` is set to anything before `es2017`. See https://stackoverflow.com/a/72822340 and
@@ -65,19 +66,19 @@ const rollupOutputOptions: RollupOutputOptions = {
65
66
* '<wrapped file>'` call into individual exports (which nextjs seems to need).
66
67
*
67
68
* @param tempProxyFilePath The path to the temporary file containing the proxy module code
68
- * @param userModulePath The path to the file being wrapped
69
+ * @param resourcePath The path to the file being wrapped
69
70
* @returns The processed proxy module code, or undefined if an error occurs
70
71
*/
71
- export async function rollupize ( userModulePath : string , templateCode : string ) : Promise < string | undefined > {
72
+ export async function rollupize ( tempProxyFilePath : string , resourcePath : string ) : Promise < string | undefined > {
72
73
let finalBundle ;
73
74
74
75
try {
75
- const intermediateBundle = await rollup ( getRollupInputOptions ( userModulePath , templateCode ) ) ;
76
+ const intermediateBundle = await rollup ( getRollupInputOptions ( tempProxyFilePath , resourcePath ) ) ;
76
77
finalBundle = await intermediateBundle . generate ( rollupOutputOptions ) ;
77
78
} catch ( err ) {
78
79
__DEBUG_BUILD__ &&
79
80
logger . warn (
80
- `Could not wrap ${ userModulePath } . An error occurred while processing the proxy module template:\n${ err } ` ,
81
+ `Could not wrap ${ resourcePath } . An error occurred while processing the proxy module template:\n${ err } ` ,
81
82
) ;
82
83
return undefined ;
83
84
}
@@ -91,7 +92,7 @@ export async function rollupize(userModulePath: string, templateCode: string): P
91
92
// square brackets into underscores. Further, Rollup adds file extensions to bare-path-type import and export sources.
92
93
// Because it assumes that everything will have already been processed, it always uses `.js` as the added extension.
93
94
// We need to restore the original name and extension so that Webpack will be able to find the wrapped file.
94
- const resourceFilename = path . basename ( userModulePath ) ;
95
+ const resourceFilename = path . basename ( resourcePath ) ;
95
96
const mutatedResourceFilename = resourceFilename
96
97
// `[\\[\\]]` is the character class containing `[` and `]`
97
98
. replace ( new RegExp ( '[\\[\\]]' , 'g' ) , '_' )
0 commit comments