Skip to content

Commit

Permalink
fix: Accessing window in browser environments
Browse files Browse the repository at this point in the history
  • Loading branch information
valya authored and netroy committed Aug 1, 2024
1 parent 3dd3221 commit 7bdeb53
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
6 changes: 2 additions & 4 deletions src/ExpressionBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { parse, visit, print } from 'recast';
import { builders as b } from 'ast-types';

import type { ExpressionKind, StatementKind } from 'ast-types/lib/gen/kinds';
import { jsVariablePolyfill } from './VariablePolyfill';
import { globalIdentifier, jsVariablePolyfill } from './VariablePolyfill';
import type { DataNode } from './VariablePolyfill';
import { splitExpression } from './ExpressionSplitter';
import type { ExpressionCode, ExpressionText } from './ExpressionSplitter';
Expand Down Expand Up @@ -175,9 +175,7 @@ export const getExpressionCode = (
const chunks = getParsedExpression(expr);

const newProg = b.program([
b.variableDeclaration('var', [
b.variableDeclarator(b.identifier('global'), b.objectExpression([])),
]),
b.variableDeclaration('var', [b.variableDeclarator(globalIdentifier, b.objectExpression([]))]),
]);

// This is what's used to access that's passed in. For compatibility we us
Expand Down
2 changes: 1 addition & 1 deletion src/VariablePolyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function assertNever(value: never): value is never {
return true;
}

const globalIdentifier = b.identifier(
export const globalIdentifier = b.identifier(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
typeof window !== 'object' ? 'global' : 'window',
Expand Down

0 comments on commit 7bdeb53

Please sign in to comment.