Skip to content

Commit 3a12ba5

Browse files
Luca ForstnerAbhiPrasad
andauthored
ref(build): Remove constToVarPlugin (#5970)
Co-authored-by: Abhijeet Prasad <[email protected]>
1 parent 4080ee9 commit 3a12ba5

File tree

6 files changed

+6
-158
lines changed

6 files changed

+6
-158
lines changed

jest/transformers/constReplacer.ts

Lines changed: 0 additions & 84 deletions
This file was deleted.

packages/browser/src/integrations/trycatch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ function _wrapXHR(originalSend: () => void): () => void {
183183
/** JSDoc */
184184
function _wrapEventTarget(target: string): void {
185185
// eslint-disable-next-line @typescript-eslint/no-explicit-any
186-
const global = WINDOW as { [key: string]: any };
186+
const globalObject = WINDOW as { [key: string]: any };
187187
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
188-
const proto = global[target] && global[target].prototype;
188+
const proto = globalObject[target] && globalObject[target].prototype;
189189

190190
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, no-prototype-builtins
191191
if (!proto || !proto.hasOwnProperty || !proto.hasOwnProperty('addEventListener')) {

packages/nextjs/src/performance/client.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type { NEXT_DATA as NextData } from 'next/dist/next-server/lib/utils';
1111
import { default as Router } from 'next/router';
1212
import type { ParsedUrlQuery } from 'querystring';
1313

14-
const global = WINDOW as typeof WINDOW & {
14+
const globalObject = WINDOW as typeof WINDOW & {
1515
__BUILD_MANIFEST?: {
1616
sortedPages?: string[];
1717
};
@@ -57,7 +57,7 @@ function extractNextDataTagInformation(): NextDataTagInfo {
5757
let nextData: SentryEnhancedNextData | undefined;
5858
// Let's be on the safe side and actually check first if there is really a __NEXT_DATA__ script tag on the page.
5959
// Theoretically this should always be the case though.
60-
const nextDataTag = global.document.getElementById('__NEXT_DATA__');
60+
const nextDataTag = globalObject.document.getElementById('__NEXT_DATA__');
6161
if (nextDataTag && nextDataTag.innerHTML) {
6262
try {
6363
nextData = JSON.parse(nextDataTag.innerHTML);
@@ -122,7 +122,7 @@ export function nextRouterInstrumentation(
122122
startTransactionOnLocationChange: boolean = true,
123123
): void {
124124
const { route, traceParentData, baggage, params } = extractNextDataTagInformation();
125-
prevLocationName = route || global.location.pathname;
125+
prevLocationName = route || globalObject.location.pathname;
126126

127127
if (startTransactionOnPageLoad) {
128128
const source = route ? 'route' : 'url';
@@ -197,7 +197,7 @@ export function nextRouterInstrumentation(
197197
}
198198

199199
function getNextRouteFromPathname(pathname: string): string | undefined {
200-
const pageRoutes = (global.__BUILD_MANIFEST || {}).sortedPages;
200+
const pageRoutes = (globalObject.__BUILD_MANIFEST || {}).sortedPages;
201201

202202
// Page route should in 99.999% of the cases be defined by now but just to be sure we make a check here
203203
if (!pageRoutes) {

rollup/npmHelpers.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import * as path from 'path';
88
import deepMerge from 'deepmerge';
99

1010
import {
11-
makeConstToVarPlugin,
1211
makeExtractPolyfillsPlugin,
1312
makeNodeResolvePlugin,
1413
makeCleanupPlugin,
@@ -30,7 +29,6 @@ export function makeBaseNPMConfig(options = {}) {
3029
const nodeResolvePlugin = makeNodeResolvePlugin();
3130
const sucrasePlugin = makeSucrasePlugin();
3231
const debugBuildStatementReplacePlugin = makeDebugBuildStatementReplacePlugin();
33-
const constToVarPlugin = makeConstToVarPlugin();
3432
const cleanupPlugin = makeCleanupPlugin();
3533
const extractPolyfillsPlugin = makeExtractPolyfillsPlugin();
3634

@@ -81,7 +79,6 @@ export function makeBaseNPMConfig(options = {}) {
8179
nodeResolvePlugin,
8280
sucrasePlugin,
8381
debugBuildStatementReplacePlugin,
84-
constToVarPlugin,
8582
cleanupPlugin,
8683
extractPolyfillsPlugin,
8784
],

rollup/plugins/npmPlugins.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,6 @@ export function makeSucrasePlugin() {
2222
});
2323
}
2424

25-
/**
26-
* Create a plugin to switch all instances of `const` to `var`, both to prevent problems when we shadow `global` and
27-
* because it's fewer characters.
28-
*
29-
* Note that the generated plugin runs the replacement both before and after rollup does its code manipulation, to
30-
* increase the chances that nothing is missed.
31-
*
32-
* TODO This is pretty brute-force-y. Perhaps we could switch to using a parser, the way we (will) do for both our jest
33-
* transformer and the polyfill build script.
34-
*
35-
* @returns An instance of the `@rollup/plugin-replace` plugin
36-
*/
37-
export function makeConstToVarPlugin() {
38-
return replace({
39-
// TODO `preventAssignment` will default to true in version 5.x of the replace plugin, at which point we can get rid
40-
// of this. (It actually makes no difference in this case whether it's true or false, since we never assign to
41-
// `const`, but if we don't give it a value, it will spam with warnings.)
42-
preventAssignment: true,
43-
values: {
44-
// Include a space at the end to guarantee we're not accidentally catching the beginning of the words "constant,"
45-
// "constantly," etc.
46-
'const ': 'var ',
47-
},
48-
});
49-
}
50-
5125
/**
5226
* Create a plugin which can be used to pause the build process at the given hook.
5327
*

scripts/test.ts

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as childProcess from 'child_process';
22
import * as fs from 'fs';
3-
import * as path from 'path';
43

54
const CURRENT_NODE_VERSION = process.version.replace('v', '').split('.')[0];
65

@@ -62,42 +61,6 @@ function installLegacyDeps(legacyDeps: string[] = []): void {
6261
run(`yarn add --dev --ignore-engines --ignore-scripts --ignore-workspace-root-check ${legacyDeps.join(' ')}`);
6362
}
6463

65-
/**
66-
* Add a tranformer to our jest config, to do the same `const`-to-`var` replacement as our rollup plugin does.
67-
*
68-
* This is needed because Node 8 doesn't like the way we shadow `global` (`const global = getGlobalObject()`). Changing
69-
* it to a `var` solves this by making it redeclarable.
70-
*
71-
*/
72-
function addJestTransformer(): void {
73-
// Though newer `ts-jest` versions support transformers written in TS, the legacy version does not.
74-
run('yarn tsc --skipLibCheck jest/transformers/constReplacer.ts');
75-
76-
// Loading the existing Jest config will error out unless the config file has an accompanying types file, so we have
77-
// to create that before we can load it.
78-
run('yarn tsc --allowJs --skipLibCheck --declaration --emitDeclarationOnly jest/jest.config.js');
79-
// eslint-disable-next-line @typescript-eslint/no-var-requires
80-
const jestConfig = require('../jest/jest.config.js');
81-
82-
// Inject the transformer
83-
jestConfig.globals['ts-jest'].astTransformers = ['<rootDir>/../../jest/transformers/constReplacer.js'];
84-
85-
// When we required the jest config file above, all expressions it contained were evaluated. Specifically, the
86-
// `rootDir: process.cwd()`
87-
// entry was replaced with
88-
// `rootDir: "<hard-coded string result of running `process.cwd()` in the current process>"`,
89-
// Though it's a little brute-force-y, the easiest way to fix this is to just stringify the code and perform the
90-
// substitution in reverse.
91-
const stringifiedConfig = JSON.stringify(jestConfig, null, 2).replace(
92-
`"rootDir": "${process.cwd()}"`,
93-
'rootDir: process.cwd()',
94-
);
95-
96-
// Now we just have to convert it back to a module and write it to disk
97-
const code = `module.exports = ${stringifiedConfig}`;
98-
fs.writeFileSync(path.resolve('jest/jest.config.js'), code);
99-
}
100-
10164
/**
10265
* Modify a json file on disk.
10366
*
@@ -151,8 +114,6 @@ function runWithIgnores(skipPackages: string[] = []): void {
151114
function runTests(): void {
152115
if (CURRENT_NODE_VERSION === '8') {
153116
installLegacyDeps(NODE_8_LEGACY_DEPENDENCIES);
154-
// Inject a `const`-to-`var` transformer, in order to stop Node 8 from complaining when we shadow `global`
155-
addJestTransformer();
156117
// TODO Right now, this just skips incompatible tests, but it could be skipping more (hence the aspirational name),
157118
// and not just in Node 8. See `skipNonNodeTests`'s docstring.
158119
skipNonNodeTests();

0 commit comments

Comments
 (0)