Skip to content

ref(nextjs): Replace multiplexer with conditional exports #11442

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions packages/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,48 @@
},
"main": "build/cjs/index.server.js",
"module": "build/esm/index.server.js",
"browser": "build/esm/index.client.js",
"types": "build/types/index.types.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"edge": {
"import": "./build/esm/edge/index.js",
"require": "./build/cjs/edge/index.js",
"default": "./build/esm/edge/index.js"
},
"edge-light": {
"import": "./build/esm/edge/index.js",
"require": "./build/cjs/edge/index.js",
"default": "./build/esm/edge/index.js"
},
"worker": {
"import": "./build/esm/edge/index.js",
"require": "./build/cjs/edge/index.js",
"default": "./build/esm/edge/index.js"
},
"workerd": {
"import": "./build/esm/edge/index.js",
"require": "./build/cjs/edge/index.js",
"default": "./build/esm/edge/index.js"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are always esm only?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point, in general I think nextjs can cope with only having esm but I'm gonna try to add cjs exports too.

},
"browser": {
"import": "./build/esm/index.client.js",
"require": "./build/cjs/index.client.js"
},
"node": "./build/cjs/index.server.js",
"import": "./build/esm/index.server.js",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @lforst
I noticed in this PR that node exports take precedence over import. Is this expected? This causes the resolution to always fall back to the CommonJS version in a Node environment, regardless of whether ESM is used.

"types": "./build/types/index.types.d.ts"
},
"./register": {
"import": {
"default": "./build/register.mjs"
}
},
"./hook": {
"./hook": {
"import": {
"default": "./build/hook.mjs"
}
}
}
},
"typesVersions": {
"<4.9": {
Expand Down
1 change: 0 additions & 1 deletion packages/nextjs/src/config/loaders/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { default as valueInjectionLoader } from './valueInjectionLoader';
export { default as prefixLoader } from './prefixLoader';
export { default as wrappingLoader } from './wrappingLoader';
export { default as sdkMultiplexerLoader } from './sdkMultiplexerLoader';
24 changes: 0 additions & 24 deletions packages/nextjs/src/config/loaders/sdkMultiplexerLoader.ts

This file was deleted.

18 changes: 0 additions & 18 deletions packages/nextjs/src/config/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ import type {
} from './types';
import { getWebpackPluginOptions } from './webpackPluginOptions';

const RUNTIME_TO_SDK_ENTRYPOINT_MAP = {
client: './client',
server: './server',
edge: './edge',
} as const;

// Next.js runs webpack 3 times, once for the client, the server, and for edge. Because we don't want to print certain
// warnings 3 times, we keep track of them here.
let showedMissingGlobalErrorWarningMsg = false;
Expand Down Expand Up @@ -79,18 +73,6 @@ export function constructWebpackConfigFunction(
// Add a loader which will inject code that sets global values
addValueInjectionLoader(newConfig, userNextConfig, userSentryOptions, buildContext);

newConfig.module.rules.push({
test: /node_modules[/\\]@sentry[/\\]nextjs/,
use: [
{
loader: path.resolve(__dirname, 'loaders', 'sdkMultiplexerLoader.js'),
options: {
importTarget: RUNTIME_TO_SDK_ENTRYPOINT_MAP[runtime],
},
},
],
});

let pagesDirPath: string | undefined;
const maybePagesDirPath = path.join(projectDir, 'pages');
const maybeSrcPagesDirPath = path.join(projectDir, 'src', 'pages');
Expand Down
6 changes: 0 additions & 6 deletions packages/nextjs/src/index.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,3 @@ export * from './client';

// This file is the main entrypoint for non-Next.js build pipelines that use
// the package.json's "browser" field or the Edge runtime (Edge API routes and middleware)

/**
* This const serves no purpose besides being an identifier for this file that the SDK multiplexer loader can use to
* determine that this is in fact a file that wants to be multiplexed.
*/
export const _SENTRY_SDK_MULTIPLEXER = true;
8 changes: 0 additions & 8 deletions packages/nextjs/src/index.server.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,2 @@
export * from './config';
export * from './server';

// This file is the main entrypoint on the server and/or when the package is `require`d

/**
* This const serves no purpose besides being an identifier for this file that the SDK multiplexer loader can use to
* determine that this is in fact a file that wants to be multiplexed.
*/
export const _SENTRY_SDK_MULTIPLEXER = true;