Skip to content

Commit 57dee8e

Browse files
authored
ref(nextjs): Prework for wrapping data-fetching functions (#5508)
This is a number of boring structural changes pulled out of #5503 in order to make it easier to review. Included changes: - Create folders for loaders and templates, and move the existing loader and template into their respective new folders. - Convert the `LoaderThis` type into a generic, so that each loader can specify its own options but share everything specified by webpack. - Since we are only going to support wrapping for pages written in ESM, make sure that templates are ESM, even if the CJS version of the SDK is being used. This required creating a build script, which in turn required updating our ESLint config. - Add/clarify comments in existing loader files.
1 parent 6ea53ed commit 57dee8e

File tree

8 files changed

+56
-18
lines changed

8 files changed

+56
-18
lines changed

packages/nextjs/.eslintrc.js

+8
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,12 @@ module.exports = {
1111
rules: {
1212
'@sentry-internal/sdk/no-async-await': 'off',
1313
},
14+
overrides: [
15+
{
16+
files: ['scripts/**/*.ts'],
17+
parserOptions: {
18+
project: ['../../tsconfig.dev.json'],
19+
},
20+
},
21+
],
1422
};

packages/nextjs/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@
4545
"scripts": {
4646
"build": "run-p build:rollup build:types",
4747
"build:dev": "run-s build",
48-
"build:rollup": "rollup -c rollup.npm.config.js",
48+
"build:rollup": "ts-node scripts/buildRollup.ts",
4949
"build:types": "tsc -p tsconfig.types.json",
5050
"build:watch": "run-p build:rollup:watch build:types:watch",
5151
"build:dev:watch": "run-s build:watch",
52-
"build:rollup:watch": "rollup -c rollup.npm.config.js --watch",
52+
"build:rollup:watch": "nodemon --ext ts --watch src scripts/buildRollup.ts",
5353
"build:types:watch": "tsc -p tsconfig.types.json --watch",
5454
"build:npm": "ts-node ../../scripts/prepack.ts && npm pack ./build",
5555
"circularDepCheck": "madge --circular src/index.client.ts && madge --circular --exclude 'config/types\\.ts' src/index.server.ts # see https://github.com/pahen/madge/issues/306",

packages/nextjs/rollup.npm.config.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ export default [
1414
),
1515
...makeNPMConfigVariants(
1616
makeBaseNPMConfig({
17-
entrypoints: ['src/config/prefixLoaderTemplate.ts'],
17+
entrypoints: ['src/config/templates/prefixLoaderTemplate.ts'],
1818

1919
packageSpecificConfig: {
2020
output: {
2121
// preserve the original file structure (i.e., so that everything is still relative to `src`)
22-
entryFileNames: 'config/[name].js',
22+
entryFileNames: 'config/templates/[name].js',
2323

2424
// this is going to be add-on code, so it doesn't need the trappings of a full module (and in fact actively
2525
// shouldn't have them, lest they muck with the module to which we're adding it)
@@ -31,15 +31,15 @@ export default [
3131
),
3232
...makeNPMConfigVariants(
3333
makeBaseNPMConfig({
34-
entrypoints: ['src/config/prefixLoader.ts'],
34+
entrypoints: ['src/config/loaders/prefixLoader.ts'],
3535

3636
packageSpecificConfig: {
3737
output: {
3838
// make it so Rollup calms down about the fact that we're doing `export { loader as default }`
3939
exports: 'default',
4040

4141
// preserve the original file structure (i.e., so that everything is still relative to `src`)
42-
entryFileNames: 'config/[name].js',
42+
entryFileNames: 'config/loaders/[name].js',
4343
},
4444
},
4545
}),
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as childProcess from 'child_process';
2+
import * as fs from 'fs';
3+
import * as path from 'path';
4+
5+
/**
6+
* Run the given shell command, piping the shell process's `stdin`, `stdout`, and `stderr` to that of the current
7+
* process. Returns contents of `stdout`.
8+
*/
9+
function run(cmd: string, options?: childProcess.ExecSyncOptions): string | Buffer {
10+
return childProcess.execSync(cmd, { stdio: 'inherit', ...options });
11+
}
12+
13+
run('yarn rollup -c rollup.npm.config.js');
14+
15+
// Regardless of whether nextjs is using the CJS or ESM version of our SDK, we want the code from our templates to be in
16+
// ESM (since we'll be adding it onto page files which are themselves written in ESM), so copy the ESM versions of the
17+
// templates over into the CJS build directory. (Building only the ESM version and sticking it in both locations is
18+
// something which in theory Rollup could do, but it would mean refactoring our Rollup helper functions, which isn't
19+
// worth it just for this.)
20+
const cjsTemplateDir = 'build/cjs/config/templates/';
21+
const esmTemplateDir = 'build/esm/config/templates/';
22+
fs.readdirSync(esmTemplateDir).forEach(templateFile =>
23+
fs.copyFileSync(path.join(esmTemplateDir, templateFile), path.join(cjsTemplateDir, templateFile)),
24+
);

packages/nextjs/src/config/prefixLoader.ts renamed to packages/nextjs/src/config/loaders/prefixLoader.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
33

4+
import { LoaderThis } from './types';
5+
46
type LoaderOptions = {
57
distDir: string;
68
};
7-
// TODO Use real webpack types
8-
type LoaderThis = {
9-
// Webpack 4
10-
query?: LoaderOptions;
11-
// Webpack 5
12-
getOptions?: () => LoaderOptions;
13-
addDependency: (filepath: string) => void;
14-
};
159

1610
/**
1711
* Inject templated code into the beginning of a module.
1812
*/
19-
function prefixLoader(this: LoaderThis, userCode: string): string {
13+
function prefixLoader(this: LoaderThis<LoaderOptions>, userCode: string): string {
2014
// We know one or the other will be defined, depending on the version of webpack being used
2115
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
2216
const { distDir } = this.getOptions ? this.getOptions() : this.query!;
2317

24-
const templatePath = path.resolve(__dirname, 'prefixLoaderTemplate.js');
18+
const templatePath = path.resolve(__dirname, '../templates/prefixLoaderTemplate.js');
19+
// make sure the template is included when runing `webpack watch`
2520
this.addDependency(templatePath);
2621

2722
// Fill in the placeholder
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// TODO Use real webpack types
2+
export type LoaderThis<Options> = {
3+
// Loader options in Webpack 4
4+
query?: Options;
5+
// Loader options in Webpack 5
6+
getOptions?: () => Options;
7+
8+
// Function to add outside file used by loader to `watch` process
9+
addDependency: (filepath: string) => void;
10+
};
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-explicit-any
22
(global as any).__rewriteFramesDistDir__ = '__DIST_DIR__';
33

4-
// We need this to make this file an ESM module, which TS requires when using `isolatedModules`.
4+
// We need this to make this file an ESM module, which TS requires when using `isolatedModules`, but it doesn't affect
5+
// the end result - Rollup recognizes that it's a no-op and doesn't include it when building our code.
56
export {};

packages/nextjs/src/config/webpack.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export function constructWebpackConfigFunction(
6565
// Support non-default output directories by making the output path (easy to get here at build-time)
6666
// available to the server SDK's default `RewriteFrames` instance (which needs it at runtime), by
6767
// injecting code to attach it to `global`.
68-
loader: path.resolve(__dirname, 'prefixLoader.js'),
68+
loader: path.resolve(__dirname, 'loaders/prefixLoader.js'),
6969
options: {
7070
distDir: userNextConfig.distDir || '.next',
7171
},

0 commit comments

Comments
 (0)