Skip to content
Merged
17 changes: 17 additions & 0 deletions packages/gatsby/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ module.exports = {
],
};
```
Additionally, you can delete source map files after they have been uploaded by setting the `deleteSourcemapsAfterUpload`
option to be `true`.

```javascript
module.exports = {
// ...
plugins: [
{
resolve: '@sentry/gatsby',
options: {
deleteSourcemapsAfterUpload: true,
},
},
// ...
],
};
```

## Links

Expand Down
3 changes: 3 additions & 0 deletions packages/gatsby/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ const SENTRY_USER_CONFIG = ['./sentry.config.js', './sentry.config.ts'];
exports.onCreateWebpackConfig = ({ getConfig, actions }, options) => {
const enableClientWebpackPlugin = options.enableClientWebpackPlugin !== false;
if (process.env.NODE_ENV === 'production' && enableClientWebpackPlugin) {
const deleteSourcemapsAfterUpload = options.deleteSourcemapsAfterUpload === true;
actions.setWebpackConfig({
plugins: [
sentryWebpackPlugin({
sourcemaps: {
// Only include files from the build output directory
assets: ['./public/**'],
// Delete source files after uploading
filesToDeleteAfterUpload: deleteSourcemapsAfterUpload ? ['./public/**/*.map'] : undefined,
// Ignore files that aren't users' source code related
ignore: [
'polyfill-*', // related to polyfills
Expand Down
31 changes: 31 additions & 0 deletions packages/gatsby/test/gatsby-node.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { onCreateWebpackConfig } from '../gatsby-node';
import { sentryWebpackPlugin } from '@sentry/webpack-plugin';

jest.mock('@sentry/webpack-plugin', () => ({
sentryWebpackPlugin: jest.fn().mockReturnValue({
apply: jest.fn(),
}),
}));

describe('onCreateWebpackConfig', () => {
let originalNodeEnv: string | undefined;
Expand All @@ -12,6 +19,10 @@ describe('onCreateWebpackConfig', () => {
process.env.NODE_ENV = originalNodeEnv;
});

afterEach(() => {
jest.clearAllMocks();
});

it('sets a webpack config', () => {
const actions = {
setWebpackConfig: jest.fn(),
Expand All @@ -36,4 +47,24 @@ describe('onCreateWebpackConfig', () => {

expect(actions.setWebpackConfig).toHaveBeenCalledTimes(0);
});

it('sets sourceMapFilesToDeleteAfterUpload when provided in options', () => {
const actions = {
setWebpackConfig: jest.fn(),
};

const getConfig = jest.fn();

onCreateWebpackConfig({ actions, getConfig }, { deleteSourcemapsAfterUpload: true });

expect(actions.setWebpackConfig).toHaveBeenCalledTimes(1);

expect(sentryWebpackPlugin).toHaveBeenCalledWith(
expect.objectContaining({
sourcemaps: expect.objectContaining({
filesToDeleteAfterUpload: ['./public/**/*.map'],
}),
})
);
});
});