Skip to content
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

fix(nextjs): Use absolute path for distDir in webpack plugin options #6214

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 6 additions & 6 deletions packages/nextjs/src/config/webpack.ts
Expand Up @@ -452,22 +452,22 @@ export function getWebpackPluginOptions(
const { buildId, isServer, webpack, config, dev: isDev, dir: projectDir } = buildContext;
const userNextConfig = config as NextConfigObject;

const distDir = userNextConfig.distDir ?? '.next'; // `.next` is the default directory
const distDirAbsPath = path.resolve(projectDir, userNextConfig.distDir || '.next'); // `.next` is the default directory

const isWebpack5 = webpack.version.startsWith('5');
const isServerless = userNextConfig.target === 'experimental-serverless-trace';
const hasSentryProperties = fs.existsSync(path.resolve(projectDir, 'sentry.properties'));
const urlPrefix = userNextConfig.basePath ? `~${userNextConfig.basePath}/_next` : '~/_next';

const serverInclude = isServerless
? [{ paths: [`${distDir}/serverless/`], urlPrefix: `${urlPrefix}/serverless` }]
: [{ paths: [`${distDir}/server/pages/`], urlPrefix: `${urlPrefix}/server/pages` }].concat(
isWebpack5 ? [{ paths: [`${distDir}/server/chunks/`], urlPrefix: `${urlPrefix}/server/chunks` }] : [],
? [{ paths: [`${distDirAbsPath}/serverless/`], urlPrefix: `${urlPrefix}/serverless` }]
: [{ paths: [`${distDirAbsPath}/server/pages/`], urlPrefix: `${urlPrefix}/server/pages` }].concat(
isWebpack5 ? [{ paths: [`${distDirAbsPath}/server/chunks/`], urlPrefix: `${urlPrefix}/server/chunks` }] : [],
);

const clientInclude = userSentryOptions.widenClientFileUpload
? [{ paths: [`${distDir}/static/chunks`], urlPrefix: `${urlPrefix}/static/chunks` }]
: [{ paths: [`${distDir}/static/chunks/pages`], urlPrefix: `${urlPrefix}/static/chunks/pages` }];
? [{ paths: [`${distDirAbsPath}/static/chunks`], urlPrefix: `${urlPrefix}/static/chunks` }]
: [{ paths: [`${distDirAbsPath}/static/chunks/pages`], urlPrefix: `${urlPrefix}/static/chunks/pages` }];

const defaultPluginOptions = dropUndefinedKeys({
include: isServer ? serverInclude : clientInclude,
Expand Down
48 changes: 30 additions & 18 deletions packages/nextjs/test/config/webpack/sentryWebpackPlugin.test.ts
Expand Up @@ -77,16 +77,18 @@ describe('Sentry webpack plugin config', () => {
) as SentryWebpackPlugin;

expect(sentryWebpackPluginInstance.options.include).toEqual([
{ paths: ['.next/static/chunks/pages'], urlPrefix: '~/_next/static/chunks/pages' },
{ paths: [`${clientBuildContext.dir}/.next/static/chunks/pages`], urlPrefix: '~/_next/static/chunks/pages' },
]);
});

it('has the correct value when building client bundles using `widenClientFileUpload` option', async () => {
const exportedNextConfigWithWidening = { ...exportedNextConfig, sentry: { widenClientFileUpload: true } };
const buildContext = getBuildContext('client', exportedNextConfigWithWidening);

const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig: exportedNextConfigWithWidening,
incomingWebpackConfig: clientWebpackConfig,
incomingWebpackBuildContext: getBuildContext('client', exportedNextConfigWithWidening),
incomingWebpackBuildContext: buildContext,
});

const sentryWebpackPluginInstance = findWebpackPlugin(
Expand All @@ -95,7 +97,7 @@ describe('Sentry webpack plugin config', () => {
) as SentryWebpackPlugin;

expect(sentryWebpackPluginInstance.options.include).toEqual([
{ paths: ['.next/static/chunks'], urlPrefix: '~/_next/static/chunks' },
{ paths: [`${buildContext.dir}/.next/static/chunks`], urlPrefix: '~/_next/static/chunks' },
]);
});

Expand All @@ -104,11 +106,12 @@ describe('Sentry webpack plugin config', () => {
...exportedNextConfig,
target: 'experimental-serverless-trace' as const,
};
const buildContext = getBuildContext('server', exportedNextConfigServerless);

const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig: exportedNextConfigServerless,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: getBuildContext('server', exportedNextConfigServerless),
incomingWebpackBuildContext: buildContext,
});

const sentryWebpackPluginInstance = findWebpackPlugin(
Expand All @@ -117,7 +120,7 @@ describe('Sentry webpack plugin config', () => {
) as SentryWebpackPlugin;

expect(sentryWebpackPluginInstance.options.include).toEqual([
{ paths: ['.next/serverless/'], urlPrefix: '~/_next/serverless' },
{ paths: [`${buildContext.dir}/.next/serverless/`], urlPrefix: '~/_next/serverless' },
]);
});

Expand All @@ -137,7 +140,7 @@ describe('Sentry webpack plugin config', () => {
) as SentryWebpackPlugin;

expect(sentryWebpackPluginInstance.options.include).toEqual([
{ paths: ['.next/server/pages/'], urlPrefix: '~/_next/server/pages' },
{ paths: [`${serverBuildContextWebpack4.dir}/.next/server/pages/`], urlPrefix: '~/_next/server/pages' },
]);
});

Expand All @@ -154,8 +157,8 @@ describe('Sentry webpack plugin config', () => {
) as SentryWebpackPlugin;

expect(sentryWebpackPluginInstance.options.include).toEqual([
{ paths: ['.next/server/pages/'], urlPrefix: '~/_next/server/pages' },
{ paths: ['.next/server/chunks/'], urlPrefix: '~/_next/server/chunks' },
{ paths: [`${serverBuildContext.dir}/.next/server/pages/`], urlPrefix: '~/_next/server/pages' },
{ paths: [`${serverBuildContext.dir}/.next/server/chunks/`], urlPrefix: '~/_next/server/chunks' },
]);
});
});
Expand Down Expand Up @@ -206,10 +209,11 @@ describe('Sentry webpack plugin config', () => {
};

it('has the correct value when building client bundles', async () => {
const buildContext = getBuildContext('client', exportedNextConfigWithBasePath);
const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig: exportedNextConfigWithBasePath,
incomingWebpackConfig: clientWebpackConfig,
incomingWebpackBuildContext: getBuildContext('client', exportedNextConfigWithBasePath),
incomingWebpackBuildContext: buildContext,
});

const sentryWebpackPluginInstance = findWebpackPlugin(
Expand All @@ -218,7 +222,10 @@ describe('Sentry webpack plugin config', () => {
) as SentryWebpackPlugin;

expect(sentryWebpackPluginInstance.options.include).toEqual([
{ paths: ['.next/static/chunks/pages'], urlPrefix: '~/city-park/_next/static/chunks/pages' },
{
paths: [`${buildContext.dir}/.next/static/chunks/pages`],
urlPrefix: '~/city-park/_next/static/chunks/pages',
},
]);
});

Expand All @@ -227,11 +234,12 @@ describe('Sentry webpack plugin config', () => {
...exportedNextConfigWithBasePath,
target: 'experimental-serverless-trace' as const,
};
const buildContext = getBuildContext('server', exportedNextConfigServerless);

const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig: exportedNextConfigServerless,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: getBuildContext('server', exportedNextConfigServerless),
incomingWebpackBuildContext: buildContext,
});

const sentryWebpackPluginInstance = findWebpackPlugin(
Expand All @@ -240,7 +248,7 @@ describe('Sentry webpack plugin config', () => {
) as SentryWebpackPlugin;

expect(sentryWebpackPluginInstance.options.include).toEqual([
{ paths: ['.next/serverless/'], urlPrefix: '~/city-park/_next/serverless' },
{ paths: [`${buildContext.dir}/.next/serverless/`], urlPrefix: '~/city-park/_next/serverless' },
]);
});

Expand All @@ -260,15 +268,19 @@ describe('Sentry webpack plugin config', () => {
) as SentryWebpackPlugin;

expect(sentryWebpackPluginInstance.options.include).toEqual([
{ paths: ['.next/server/pages/'], urlPrefix: '~/city-park/_next/server/pages' },
{
paths: [`${serverBuildContextWebpack4.dir}/.next/server/pages/`],
urlPrefix: '~/city-park/_next/server/pages',
},
]);
});

it('has the correct value when building serverful server bundles using webpack 5', async () => {
const buildContext = getBuildContext('server', exportedNextConfigWithBasePath);
const finalWebpackConfig = await materializeFinalWebpackConfig({
exportedNextConfig: exportedNextConfigWithBasePath,
incomingWebpackConfig: serverWebpackConfig,
incomingWebpackBuildContext: getBuildContext('server', exportedNextConfigWithBasePath),
incomingWebpackBuildContext: buildContext,
});

const sentryWebpackPluginInstance = findWebpackPlugin(
Expand All @@ -277,8 +289,8 @@ describe('Sentry webpack plugin config', () => {
) as SentryWebpackPlugin;

expect(sentryWebpackPluginInstance.options.include).toEqual([
{ paths: ['.next/server/pages/'], urlPrefix: '~/city-park/_next/server/pages' },
{ paths: ['.next/server/chunks/'], urlPrefix: '~/city-park/_next/server/chunks' },
{ paths: [`${buildContext.dir}/.next/server/pages/`], urlPrefix: '~/city-park/_next/server/pages' },
{ paths: [`${buildContext.dir}/.next/server/chunks/`], urlPrefix: '~/city-park/_next/server/chunks' },
]);
});
});
Expand Down Expand Up @@ -450,7 +462,7 @@ describe('Sentry webpack plugin config', () => {

for (const pathDescriptor of includePaths) {
for (const path of pathDescriptor.paths) {
expect(path).toMatch(new RegExp(`^${expectedDistDir}.*`));
expect(path).toMatch(new RegExp(`${buildContext.dir}/${expectedDistDir}.*`));
}
}
});
Expand All @@ -469,7 +481,7 @@ describe('Sentry webpack plugin config', () => {

for (const pathDescriptor of includePaths) {
for (const path of pathDescriptor.paths) {
expect(path).toMatch(new RegExp(`^${expectedDistDir}.*`));
expect(path).toMatch(new RegExp(`${buildContext.dir}/${expectedDistDir}.*`));
}
}
});
Expand Down