-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(nextjs): Support distDir
Next.js option
#3990
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
Changes from 9 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
4d88272
feat(nextjs): Support `distDir` Next.js option
iker-barriocanal cb94618
Move the comment back to its original place
iker-barriocanal 042223b
Separate `includeSources` to another file
iker-barriocanal 8014413
Add unit tests to `includeDistDir`
iker-barriocanal 37e15ee
Rename mocks
iker-barriocanal 9295eaa
fix types
iker-barriocanal 1a3aed0
Use `??` instead of `||`
iker-barriocanal 344fd39
Add tests for `getWebpackPluginOptions`
iker-barriocanal a940c07
Remove redundant comments
iker-barriocanal f5f1f25
Separate tests by test name
iker-barriocanal ef4cffd
Generalize the use case
iker-barriocanal 3120b7c
Add note about overriding values
iker-barriocanal 718b269
Merge branch 'master' into iker/feat/nextjs-distdir
iker-barriocanal de8ff3e
Add test for duplicated props in includeNextjsProps
iker-barriocanal ca0a409
feedback
iker-barriocanal 3f1df7c
Use `Array.from` instead of spread array
iker-barriocanal 3ee1215
Fix type on test
iker-barriocanal 3ed2d01
Merge branch 'master' into iker/feat/nextjs-distdir
iker-barriocanal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
packages/nextjs/src/config/nextConfigToWebpackPluginConfig.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { NextConfigObject, SentryWebpackPluginOptions } from './types'; | ||
|
||
/** | ||
* Creates a new Sentry Webpack Plugin config with the `distDir` option from Next.js config | ||
* in the `include` property. | ||
* | ||
* If no `distDir` is provided, the Webpack Plugin config doesn't change. | ||
* If no `include` has been defined defined, the `distDir` value is assigned. | ||
* The `distDir` directory is merged to the directories in `include`, if defined. | ||
* Duplicated paths are removed while merging. | ||
* | ||
* @param nextConfig User's Next.js config | ||
* @param sentryWebpackPluginOptions User's Sentry Webpack Plugin config | ||
* @returns New Sentry Webpack Plugin config | ||
*/ | ||
export function includeDistDir( | ||
nextConfig: NextConfigObject, | ||
sentryWebpackPluginOptions: Partial<SentryWebpackPluginOptions>, | ||
): Partial<SentryWebpackPluginOptions> { | ||
if (!nextConfig.distDir) { | ||
return { ...sentryWebpackPluginOptions }; | ||
iker-barriocanal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
// It's assumed `distDir` is a string as that's what Next.js is expecting. If it's not, Next.js itself will complain | ||
const usersInclude = sentryWebpackPluginOptions.include; | ||
|
||
let sourcesToInclude; | ||
if (typeof usersInclude === 'undefined') { | ||
sourcesToInclude = nextConfig.distDir; | ||
} else if (typeof usersInclude === 'string') { | ||
sourcesToInclude = usersInclude === nextConfig.distDir ? usersInclude : [usersInclude, nextConfig.distDir]; | ||
} else if (Array.isArray(usersInclude)) { | ||
// @ts-ignore '__spreadArray' import from tslib, ts(2343) | ||
sourcesToInclude = [...new Set(usersInclude.concat(nextConfig.distDir))]; | ||
iker-barriocanal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
// Object | ||
if (Array.isArray(usersInclude.paths)) { | ||
const uniquePaths = [...new Set(usersInclude.paths.concat(nextConfig.distDir as string))]; | ||
sourcesToInclude = { ...usersInclude, paths: uniquePaths }; | ||
} else if (typeof usersInclude.paths === 'undefined') { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
'Sentry Logger [Warn]:', | ||
`An object was set in \`include\` but no \`paths\` was provided, so added the \`distDir\`: "${nextConfig.distDir}"\n` + | ||
'See https://github.com/getsentry/sentry-webpack-plugin#optionsinclude', | ||
); | ||
sourcesToInclude = { ...usersInclude, paths: [nextConfig.distDir] }; | ||
} else { | ||
// eslint-disable-next-line no-console | ||
console.error( | ||
'Sentry Logger [Error]:', | ||
'Found unexpected object in `include.paths`\n' + | ||
'See https://github.com/getsentry/sentry-webpack-plugin#optionsinclude', | ||
); | ||
// Keep the same object even if it's incorrect, so that the user can get a more precise error from sentry-cli | ||
// Casting to `any` for TS not complaining about it being `unknown` | ||
sourcesToInclude = usersInclude as any; | ||
} | ||
} | ||
|
||
return { ...sentryWebpackPluginOptions, include: sourcesToInclude }; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/nextjs/test/config/nextConfigToWebpackPluginConfig.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { includeDistDir } from '../../src/config/nextConfigToWebpackPluginConfig'; | ||
|
||
describe('next config to webpack plugin config', () => { | ||
describe('includeDistDir', () => { | ||
const consoleWarnMock = jest.fn(); | ||
const consoleErrorMock = jest.fn(); | ||
|
||
beforeAll(() => { | ||
global.console.warn = consoleWarnMock; | ||
global.console.error = consoleErrorMock; | ||
}); | ||
|
||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
test.each([ | ||
[{}, {}, {}], | ||
[{}, { include: 'path' }, { include: 'path' }], | ||
[{}, { include: [] }, { include: [] }], | ||
[{}, { include: ['path'] }, { include: ['path'] }], | ||
[{}, { include: { paths: ['path'] } }, { include: { paths: ['path'] } }], | ||
])('without `distDir`', (nextConfig, webpackPluginConfig, expectedConfig) => { | ||
expect(includeDistDir(nextConfig, webpackPluginConfig)).toMatchObject(expectedConfig); | ||
}); | ||
|
||
test.each([ | ||
[{ distDir: 'test' }, {}, { include: 'test' }], | ||
[{ distDir: 'test' }, { include: 'path' }, { include: ['path', 'test'] }], | ||
[{ distDir: 'test' }, { include: [] }, { include: ['test'] }], | ||
[{ distDir: 'test' }, { include: ['path'] }, { include: ['path', 'test'] }], | ||
[{ distDir: 'test' }, { include: { paths: ['path'] } }, { include: { paths: ['path', 'test'] } }], | ||
])('with `distDir`, different paths', (nextConfig, webpackPluginConfig, expectedConfig) => { | ||
expect(includeDistDir(nextConfig, webpackPluginConfig)).toMatchObject(expectedConfig); | ||
}); | ||
|
||
test.each([ | ||
[{ distDir: 'path' }, { include: 'path' }, { include: 'path' }], | ||
[{ distDir: 'path' }, { include: ['path'] }, { include: ['path'] }], | ||
[{ distDir: 'path' }, { include: { paths: ['path'] } }, { include: { paths: ['path'] } }], | ||
])('with `distDir`, same path', (nextConfig, webpackPluginConfig, expectedConfig) => { | ||
expect(includeDistDir(nextConfig, webpackPluginConfig)).toMatchObject(expectedConfig); | ||
}); | ||
|
||
test.each([ | ||
[{ distDir: 'path' }, { include: {} }, { include: { paths: ['path'] } }], | ||
[{ distDir: 'path' }, { include: { prop: 'val' } }, { include: { prop: 'val', paths: ['path'] } }], | ||
])('webpack plugin config as object with other prop', (nextConfig, webpackPluginConfig, expectedConfig) => { | ||
// @ts-ignore Other props don't match types | ||
expect(includeDistDir(nextConfig, webpackPluginConfig)).toMatchObject(expectedConfig); | ||
expect(consoleWarnMock).toHaveBeenCalledTimes(1); | ||
consoleWarnMock.mockClear(); | ||
}); | ||
|
||
test.each([ | ||
[{ distDir: 'path' }, { include: { paths: {} } }, { include: { paths: {} } }], | ||
[{ distDir: 'path' }, { include: { paths: { badObject: true } } }, { include: { paths: { badObject: true } } }], | ||
])('webpack plugin config as object with bad structure', (nextConfig, webpackPluginConfig, expectedConfig) => { | ||
// @ts-ignore Bad structures don't match types | ||
expect(includeDistDir(nextConfig, webpackPluginConfig)).toMatchObject(expectedConfig); | ||
expect(consoleErrorMock).toHaveBeenCalledTimes(1); | ||
consoleErrorMock.mockClear(); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.