Skip to content

Commit 36d6630

Browse files
authored
fix(gatsby): Use import for gatsby-browser.js instead of require (#7889)
1 parent 57cb2fc commit 36d6630

File tree

6 files changed

+25
-8
lines changed

6 files changed

+25
-8
lines changed

packages/gatsby/.eslintrc.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ module.exports = {
1515
project: ['../../tsconfig.dev.json'],
1616
},
1717
},
18+
{
19+
files: ['./gatsby-browser.js'],
20+
env: {
21+
browser: true,
22+
node: false,
23+
},
24+
parserOptions: {
25+
sourceType: 'module',
26+
},
27+
},
1828
],
1929
extends: ['../../.eslintrc.js'],
2030
};

packages/gatsby/gatsby-browser.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-console */
2-
const Sentry = require('@sentry/gatsby');
2+
import { init } from '@sentry/gatsby';
33

4-
exports.onClientEntry = function (_, pluginParams) {
4+
export function onClientEntry(_, pluginParams) {
55
const isIntialized = isSentryInitialized();
66
const areOptionsDefined = areSentryOptionsDefined(pluginParams);
77

@@ -24,12 +24,12 @@ exports.onClientEntry = function (_, pluginParams) {
2424
return;
2525
}
2626

27-
Sentry.init({
27+
init({
2828
// eslint-disable-next-line no-undef
2929
dsn: __SENTRY_DSN__,
3030
...pluginParams,
3131
});
32-
};
32+
}
3333

3434
function isSentryInitialized() {
3535
// Although `window` should exist because we're in the browser (where this script

packages/gatsby/jest.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ module.exports = {
44
...baseConfig,
55
setupFiles: ['<rootDir>/test/setEnvVars.ts'],
66
testEnvironment: 'jsdom',
7+
transform: {
8+
'^.+\\.js$': 'ts-jest',
9+
...baseConfig.transform,
10+
},
711
};

packages/gatsby/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@
5050
"clean": "rimraf build coverage *.d.ts sentry-gatsby-*.tgz",
5151
"fix": "run-s fix:eslint fix:prettier",
5252
"fix:eslint": "eslint . --format stylish --fix",
53-
"fix:prettier": "prettier --write \"{src,test,scripts}/**/**.ts\"",
53+
"fix:prettier": "prettier --write \"{src,test,scripts}/**/**.{ts,tsx,js}\"",
5454
"lint": "run-s lint:prettier lint:eslint",
5555
"lint:eslint": "eslint . --format stylish",
56-
"lint:prettier": "prettier --check \"{src,test,scripts}/**/**.ts\"",
56+
"lint:prettier": "prettier --check \"{src,test,scripts}/**/**.{ts,tsx,js}\"",
5757
"test": "yarn ts-node scripts/pretest.ts && yarn jest",
5858
"test:watch": "yarn ts-node scripts/pretest.ts && yarn jest --watch",
5959
"yalc:publish": "ts-node ../../scripts/prepack.ts && yalc publish build --push"

packages/gatsby/test/integration.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { render } from '@testing-library/react';
33
import { useEffect } from 'react';
44
// eslint-disable-next-line @typescript-eslint/no-unused-vars
55
import * as React from 'react';
6-
import { TextDecoder,TextEncoder } from 'util';
6+
import { TextDecoder, TextEncoder } from 'util';
77

88
import { onClientEntry } from '../gatsby-browser';
99
import * as Sentry from '../src';

packages/gatsby/tsconfig.test.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66
"compilerOptions": {
77
// should include all types from `./tsconfig.json` plus types for all test frameworks used
8-
"types": ["node", "jest"]
8+
"types": ["node", "jest"],
99

1010
// other package-specific, test-specific options
11+
12+
// Needed to parse ESM from gatsby-browser.js
13+
"allowJs": true
1114
}
1215
}

0 commit comments

Comments
 (0)