Skip to content

Commit

Permalink
feat(nextjs): Create next.config.mjs when package.json has `type: "…
Browse files Browse the repository at this point in the history
…module"` (#699)
  • Loading branch information
lforst authored Oct 30, 2024
1 parent 5b27523 commit 3d6537a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- fix(Apple): Sentry-cli not found by build phase when installed with homebrew (#691)
- feat(nextjs): Create `next.config.mjs` when package.json has type: "module" (#699)

## 3.34.2

Expand Down
33 changes: 29 additions & 4 deletions src/nextjs/nextjs-wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
getSentryExamplePageContents,
getSimpleUnderscoreErrorCopyPasteSnippet,
getWithSentryConfigOptionsTemplate,
getNextjsConfigMjsTemplate,
} from './templates';
import { traceStep, withTelemetry } from '../telemetry';
import { getPackageVersion, hasPackageInstalled } from '../utils/package-json';
Expand Down Expand Up @@ -564,15 +565,39 @@ async function createOrMergeNextJsFiles(
if (!foundNextConfigFile) {
Sentry.setTag('next-config-strategy', 'create');

// Try to figure out whether the user prefers ESM
let isTypeModule = false;
try {
const packageJsonText = await fs.promises.readFile(
path.join(process.cwd(), 'package.json'),
'utf8',
);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const packageJson = JSON.parse(packageJsonText);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (packageJson.type === 'module') {
isTypeModule = true;
}
} catch {
// noop
}

// We are creating `next.config.(m)js` files by default as they are supported by the most Next.js versions
const configFilename = isTypeModule
? nextConfigPossibleFilesMap.mjs
: nextConfigPossibleFilesMap.js;
const configContent = isTypeModule
? getNextjsConfigMjsTemplate(withSentryConfigOptionsTemplate)
: getNextjsConfigCjsTemplate(withSentryConfigOptionsTemplate);

await fs.promises.writeFile(
// We are creating a `next.config.js` file by default as it is supported by the most Next.js versions
path.join(process.cwd(), nextConfigPossibleFilesMap.js),
getNextjsConfigCjsTemplate(withSentryConfigOptionsTemplate),
path.join(process.cwd(), configFilename),
configContent,
{ encoding: 'utf8', flag: 'w' },
);

clack.log.success(
`Created ${chalk.cyan('next.config.js')} with Sentry configuration.`,
`Created ${chalk.cyan(configFilename)} with Sentry configuration.`,
);

return;
Expand Down
15 changes: 15 additions & 0 deletions src/nextjs/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,21 @@ module.exports = withSentryConfig(
`;
}

export function getNextjsConfigMjsTemplate(
withSentryConfigOptionsTemplate: string,
): string {
return `import { withSentryConfig } from "@sentry/nextjs";
/** @type {import('next').NextConfig} */
const nextConfig = {};
export default withSentryConfig(
nextConfig,
${withSentryConfigOptionsTemplate}
);
`;
}

export function getNextjsConfigCjsAppendix(
withSentryConfigOptionsTemplate: string,
): string {
Expand Down

0 comments on commit 3d6537a

Please sign in to comment.