Skip to content

Commit 03f4a0b

Browse files
committed
Fix esm var changes (#4262)
* Fix esm var changes * Fix object initiliazers * Add comment
1 parent 4cbdd1e commit 03f4a0b

File tree

3 files changed

+90
-42
lines changed

3 files changed

+90
-42
lines changed

packages/app/src/sandbox/eval/transpilers/babel/convert-esmodule/__snapshots__/index.test.ts.snap

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,23 @@ const short = {
150150
"
151151
`;
152152

153+
exports[`convert-esmodule doesn't remove object initializers 1`] = `
154+
"var $csb__shared = require(\\"@react-spring/shared\\");
155+
const createHost = (components, {a = () => {}} = {}) => {
156+
$csb__shared.is();
157+
};
158+
"
159+
`;
160+
161+
exports[`convert-esmodule doesn't set var definitions 1`] = `
162+
"Object.defineProperty(exports, \\"__esModule\\", {
163+
value: true
164+
});
165+
var global = typeof window !== \\"undefined\\" ? window : {};
166+
exports.global = global;
167+
"
168+
`;
169+
153170
exports[`convert-esmodule handles default as exports 1`] = `
154171
"Object.defineProperty(exports, \\"__esModule\\", {
155172
value: true
@@ -172,7 +189,7 @@ exports[`convert-esmodule handles export mutations 1`] = `
172189
});
173190
function test() {}
174191
exports.default = test;
175-
exports.default = test = 5;;
192+
exports.default = test = 5;
176193
"
177194
`;
178195

@@ -185,6 +202,21 @@ exports.default = $csb__default;
185202
"
186203
`;
187204

205+
exports[`convert-esmodule handles export mutations with variables 1`] = `
206+
"Object.defineProperty(exports, \\"__esModule\\", {
207+
value: true
208+
});
209+
var to;
210+
exports.to = to;
211+
function assign() {
212+
exports.to = to = \\"test\\";
213+
}
214+
function assign2(to) {
215+
to = \\"test\\";
216+
}
217+
"
218+
`;
219+
188220
exports[`convert-esmodule handles multiple aliased exports 1`] = `
189221
"Object.defineProperty(exports, \\"f\\", {
190222
enumerable: true,

packages/app/src/sandbox/eval/transpilers/babel/convert-esmodule/index.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,41 @@ describe('convert-esmodule', () => {
151151
expect(convertEsModule(code)).toMatchSnapshot();
152152
});
153153

154+
it('handles export mutations with variables', () => {
155+
const code = `
156+
export var to;
157+
158+
function assign() {
159+
to = "test"
160+
}
161+
162+
function assign2(to) {
163+
to = "test"
164+
}
165+
`;
166+
expect(convertEsModule(code)).toMatchSnapshot();
167+
});
168+
169+
it("doesn't remove object initializers", () => {
170+
const code = `
171+
import { defineHidden, is, createInterpolator, each, getFluidConfig, isAnimatedString, useForceUpdate } from '@react-spring/shared';
172+
173+
const createHost = (components, {
174+
a = () => {}
175+
} = {}) => {
176+
is()
177+
};
178+
`;
179+
expect(convertEsModule(code)).toMatchSnapshot();
180+
});
181+
182+
it("doesn't set var definitions", () => {
183+
const code = `
184+
export var global = typeof window !== 'undefined' ? window : {};
185+
`;
186+
expect(convertEsModule(code)).toMatchSnapshot();
187+
});
188+
154189
it('handles default as exports', () => {
155190
const code = `
156191
export { default as Field } from './Field';

packages/app/src/sandbox/eval/transpilers/babel/convert-esmodule/index.ts

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ import * as astring from 'astring';
55
import * as escope from 'escope';
66
import { basename } from 'path';
77
import { walk } from 'estree-walker';
8-
import {
9-
AssignmentExpression,
10-
ExpressionStatement,
11-
Property,
12-
} from 'meriyah/dist/estree';
8+
import { Property } from 'meriyah/dist/estree';
139
import { Syntax as n } from './syntax';
1410
import {
1511
generateRequireStatement,
@@ -29,6 +25,7 @@ import { customGenerator } from './generator';
2925
export function convertEsModule(code: string) {
3026
const usedVarNames = [];
3127
const varsToRename = {};
28+
const trackedExports = {};
3229

3330
const getVarName = (name: string) => {
3431
let usedName = name.replace(/[.-]/g, '');
@@ -144,6 +141,7 @@ export function convertEsModule(code: string) {
144141
continue;
145142
}
146143

144+
trackedExports[foundDeclaration.id.name] = foundDeclaration.id.name;
147145
varName = foundDeclaration.id.name;
148146
}
149147
i++;
@@ -232,40 +230,7 @@ export function convertEsModule(code: string) {
232230
newDeclaration.type === n.ClassDeclaration ||
233231
newDeclaration.type === n.FunctionExpression
234232
) {
235-
// @ts-ignore Different libraries with the same types
236-
program = walk(program, {
237-
enter(node, parent, prop, index) {
238-
if (node.type === n.AssignmentExpression) {
239-
const { left } = node as AssignmentExpression;
240-
if (
241-
left.type === n.Identifier &&
242-
left.name === newDeclaration.id.name
243-
) {
244-
this.replace({
245-
type: n.ExpressionStatement,
246-
expression: {
247-
type: n.AssignmentExpression,
248-
left: {
249-
type: n.MemberExpression,
250-
object: {
251-
type: n.Identifier,
252-
name: 'exports',
253-
},
254-
computed: false,
255-
property: {
256-
type: n.Identifier,
257-
name: 'default',
258-
},
259-
},
260-
operator: '=' as '=',
261-
right: node,
262-
},
263-
} as ExpressionStatement);
264-
this.skip();
265-
}
266-
}
267-
},
268-
});
233+
trackedExports[newDeclaration.id.name] = 'default';
269234
}
270235
}
271236
} else if (statement.type === n.ImportDeclaration) {
@@ -358,15 +323,21 @@ export function convertEsModule(code: string) {
358323
}
359324
}
360325

361-
if (Object.keys(varsToRename).length > 0) {
326+
if (
327+
Object.keys(varsToRename).length > 0 ||
328+
Object.keys(trackedExports).length > 0
329+
) {
362330
// Convert all the object shorthands to not shorthands, needed later when we rename variables so we
363331
// don't change to the key literals
364332
// @ts-ignore
365333
program = walk(program, {
366334
enter(node, parent, prop, index) {
367335
if (node.type === n.Property) {
368336
const property = node as Property;
369-
if (property.shorthand) {
337+
if (
338+
property.shorthand &&
339+
property.value.type !== n.AssignmentPattern // Not a default initializer
340+
) {
370341
property.value = {
371342
...property.key,
372343
};
@@ -387,6 +358,16 @@ export function convertEsModule(code: string) {
387358
if (varsToRename[ref.identifier.name] && ref.resolved === null) {
388359
ref.identifier.name = varsToRename[ref.identifier.name].join('.');
389360
}
361+
362+
if (
363+
trackedExports[ref.identifier.name] &&
364+
ref.isWrite() &&
365+
ref.resolved === null &&
366+
!ref.init
367+
) {
368+
const name = trackedExports[ref.identifier.name];
369+
ref.identifier.name = `exports.${name} = ${ref.identifier.name}`;
370+
}
390371
});
391372
});
392373
scopeManager.detach();

0 commit comments

Comments
 (0)