Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ exports[`convert-esmodule can convert function exports 1`] = `
Object.defineProperty(exports, \\"__esModule\\", {
value: true
});
function test() {}
exports.test = test;
function test() {}
const test2 = () => {};
exports.test2 = test2;
class Test {}
Expand Down Expand Up @@ -236,6 +236,19 @@ const short = {
"
`;

exports[`convert-esmodule defines its exports before requires 1`] = `
"\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", {
value: true
});
exports.get = get;
var $csb__colorsvalues = require(\\"./colors-values\\");
function get() {
return 5;
}
"
`;

exports[`convert-esmodule does empty exports 1`] = `
"\\"use strict\\";
Object.defineProperty(exports, \\"__esModule\\", {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,16 @@ describe('convert-esmodule', () => {

expect(convertEsModule(code)).toMatchSnapshot();
});

it('defines its exports before requires', () => {
const code = `
import { COLORS } from './colors-values';

export function get() {
return 5;
}
`;

expect(convertEsModule(code)).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,15 @@ export function convertEsModule(code: string) {

const varName = statement.declaration.id.name;
i++;
program.body.splice(i, 0, generateExportStatement(varName, varName));
// Add to start of the file, after the defineModule for __esModule. This way this export is already
// defined before requiring other modules. This is only possible for function exports.
const positionToInsert =
statement.declaration.type === n.FunctionDeclaration ? 1 : i;
program.body.splice(
positionToInsert,
0,
generateExportStatement(varName, varName)
);
} else {
// export const a = {}

Expand Down