Skip to content

Commit 72e8307

Browse files
committed
use virtual instead of temporary file for proxy module code
1 parent 32d7a03 commit 72e8307

File tree

2 files changed

+18
-24
lines changed

2 files changed

+18
-24
lines changed

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

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,34 +50,21 @@ export default async function proxyLoader(this: LoaderThis<LoaderOptions>, userC
5050
// Make sure the template is included when runing `webpack watch`
5151
this.addDependency(templatePath);
5252

53-
// Inject the route into the template
53+
// Inject the route and the path to the file we're wrapping into the template
5454
templateCode = templateCode.replace(/__ROUTE__/g, parameterizedRoute);
55-
56-
// Fill in the path to the file we're wrapping and save the result as a temporary file in the same folder (so that
57-
// relative imports and exports are calculated correctly).
58-
//
59-
// TODO: We're saving the filled-in template to disk, however temporarily, because Rollup expects a path to a code
60-
// file, not code itself. There is a rollup plugin which can fake this (`@rollup/plugin-virtual`) but the virtual file
61-
// seems to be inside of a virtual directory (in other words, one level down from where you'd expect it) and that
62-
// messes up relative imports and exports. Presumably there's a way to make it work, though, and if we can, it would
63-
// be cleaner than having to first write and then delete a temporary file each time we run this loader.
6455
templateCode = templateCode.replace(/__RESOURCE_PATH__/g, this.resourcePath);
65-
const tempFilePath = path.resolve(path.dirname(this.resourcePath), `temp${Math.random()}.js`);
66-
fs.writeFileSync(tempFilePath, templateCode);
6756

6857
// Run the proxy module code through Rollup, in order to split the `export * from '<wrapped file>'` out into
69-
// individual exports (which nextjs seems to require), then delete the tempoary file.
58+
// individual exports (which nextjs seems to require).
7059
let proxyCode;
7160
try {
72-
proxyCode = await rollupize(tempFilePath, this.resourcePath);
61+
proxyCode = await rollupize(templateCode, this.resourcePath);
7362
} catch (err) {
7463
__DEBUG_BUILD__ &&
7564
logger.warn(
7665
`Could not wrap ${this.resourcePath}. An error occurred while processing the proxy module template:\n${err}`,
7766
);
7867
return userCode;
79-
} finally {
80-
fs.unlinkSync(tempFilePath);
8168
}
8269

8370
// Add a query string onto all references to the wrapped file, so that webpack will consider it different from the

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import sucrase from '@rollup/plugin-sucrase';
2+
import virtual from '@rollup/plugin-virtual';
23
import * as path from 'path';
34
import type { InputOptions as RollupInputOptions, OutputOptions as RollupOutputOptions } from 'rollup';
45
import { rollup } from 'rollup';
56

6-
const getRollupInputOptions = (proxyPath: string, userModulePath: string): RollupInputOptions => ({
7-
input: proxyPath,
7+
const SENTRY_PROXY_MODULE_NAME = 'sentry-proxy-module';
8+
9+
const getRollupInputOptions = (templateCode: string, userModulePath: string): RollupInputOptions => ({
10+
input: SENTRY_PROXY_MODULE_NAME,
811

912
plugins: [
13+
virtual({
14+
[SENTRY_PROXY_MODULE_NAME]: templateCode,
15+
}),
16+
1017
sucrase({
1118
transforms: ['jsx', 'typescript'],
1219
}),
@@ -17,7 +24,7 @@ const getRollupInputOptions = (proxyPath: string, userModulePath: string): Rollu
1724
// otherwise they won't be processed. (We need Rollup to process the former so that we can use the code, and we need
1825
// it to process the latter so it knows what exports to re-export from the proxy module.) Past that, we don't care, so
1926
// don't bother to process anything else.
20-
external: importPath => importPath !== proxyPath && importPath !== userModulePath,
27+
external: importPath => importPath !== SENTRY_PROXY_MODULE_NAME && importPath !== userModulePath,
2128

2229
// Prevent rollup from stressing out about TS's use of global `this` when polyfilling await. (TS will polyfill if the
2330
// user's tsconfig `target` is set to anything before `es2017`. See https://stackoverflow.com/a/72822340 and
@@ -53,17 +60,17 @@ const rollupOutputOptions: RollupOutputOptions = {
5360
};
5461

5562
/**
56-
* Use Rollup to process the proxy module file (located at `tempProxyFilePath`) in order to split its `export * from
57-
* '<wrapped file>'` call into individual exports (which nextjs seems to need).
63+
* Use Rollup to process the proxy module code, in order to split its `export * from '<wrapped file>'` call into
64+
* individual exports (which nextjs seems to need).
5865
*
5966
* Note: Any errors which occur are handled by the proxy loader which calls this function.
6067
*
61-
* @param tempProxyFilePath The path to the temporary file containing the proxy module code
68+
* @param templateCode The proxy module code
6269
* @param userModulePath The path to the file being wrapped
6370
* @returns The processed proxy module code
6471
*/
65-
export async function rollupize(tempProxyFilePath: string, userModulePath: string): Promise<string> {
66-
const intermediateBundle = await rollup(getRollupInputOptions(tempProxyFilePath, userModulePath));
72+
export async function rollupize(templateCode: string, userModulePath: string): Promise<string> {
73+
const intermediateBundle = await rollup(getRollupInputOptions(templateCode, userModulePath));
6774
const finalBundle = await intermediateBundle.generate(rollupOutputOptions);
6875

6976
// The module at index 0 is always the entrypoint, which in this case is the proxy module.

0 commit comments

Comments
 (0)