Skip to content

Commit 233c9e6

Browse files
authored
ref(nextjs): Simplify rollupize in proxy loader (#6020)
This makes a few small changes to the `rollupize` function used by the nextjs SDK's proxy loader: - Consolidate error handling in the proxy loader, rather than handling errors inside of `rollupize` and then handling an undefined `rollupize` return value in the proxy loader. - Slightly simplify explanation of `makeAbsoluteExternalsRelative` - Remove type hack in `getRollupInputOptions` which seems no longer to be needed. (It was never clear why it was necessary in the first place, and TS no longer seems mad when it's removed.) - Rename `resourcePath` to `userModulePath` for clarity The latter two changes are drawn from #5960, to preserve them even though it is being reverted.
1 parent db15649 commit 233c9e6

File tree

2 files changed

+27
-39
lines changed

2 files changed

+27
-39
lines changed

packages/nextjs/src/config/loaders/proxyLoader.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { escapeStringForRegex } from '@sentry/utils';
1+
import { escapeStringForRegex, logger } from '@sentry/utils';
22
import * as fs from 'fs';
33
import * as path from 'path';
44

@@ -67,12 +67,17 @@ export default async function proxyLoader(this: LoaderThis<LoaderOptions>, userC
6767

6868
// Run the proxy module code through Rollup, in order to split the `export * from '<wrapped file>'` out into
6969
// individual exports (which nextjs seems to require), then delete the tempoary file.
70-
let proxyCode = await rollupize(tempFilePath, this.resourcePath);
71-
fs.unlinkSync(tempFilePath);
72-
73-
if (!proxyCode) {
74-
// We will already have thrown a warning in `rollupize`, so no need to do it again here
70+
let proxyCode;
71+
try {
72+
proxyCode = await rollupize(tempFilePath, this.resourcePath);
73+
} catch (err) {
74+
__DEBUG_BUILD__ &&
75+
logger.warn(
76+
`Could not wrap ${this.resourcePath}. An error occurred while processing the proxy module template:\n${err}`,
77+
);
7578
return userCode;
79+
} finally {
80+
fs.unlinkSync(tempFilePath);
7681
}
7782

7883
// Add a query string onto all references to the wrapped file, so that webpack will consider it different from the
Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
1-
import type { RollupSucraseOptions } from '@rollup/plugin-sucrase';
21
import sucrase from '@rollup/plugin-sucrase';
3-
import { logger } from '@sentry/utils';
42
import * as path from 'path';
53
import type { InputOptions as RollupInputOptions, OutputOptions as RollupOutputOptions } from 'rollup';
64
import { 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}. 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);
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(/(jsx?|tsx?)$/, '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

Comments
 (0)