Skip to content

Commit 113a9b4

Browse files
committed
Improve esmodules loading by writing a custom converter
1 parent fff61c4 commit 113a9b4

File tree

8 files changed

+677
-14
lines changed

8 files changed

+677
-14
lines changed

packages/app/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@
113113
"debug": "^2.6.8",
114114
"dot-object": "^1.9.0",
115115
"downshift": "^1.0.0-rc.14",
116+
"escodegen": "^1.13.0",
116117
"eslint-plugin-react-hooks": "1.6.0",
117118
"eslint-plugin-vue": "^4.2.2",
119+
"esprima": "^4.0.1",
118120
"file-saver": "^1.3.3",
119121
"fontfaceobserver": "^2.0.13",
120122
"fuse.js": "^3.2.1",
@@ -248,6 +250,8 @@
248250
"@sentry/cli": "^1.47.1",
249251
"@types/codemirror": "^0.0.72",
250252
"@types/debug": "^4.1.1",
253+
"@types/escodegen": "^0.0.6",
254+
"@types/esprima": "^4.0.2",
251255
"@types/gsap": "^1.20.1",
252256
"@types/jest": "24.0.13",
253257
"@types/lodash-es": "^4.17.2",
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`convert-esmodule can convert class default exports 1`] = `
4+
"Object.defineProperty(exports, '__esModule', { value: true });
5+
var $csb__default = class A {
6+
};
7+
exports.default = $csb__default;"
8+
`;
9+
10+
exports[`convert-esmodule can convert default exports 1`] = `
11+
"Object.defineProperty(exports, '__esModule', { value: true });
12+
var $csb__default = function test() {
13+
};
14+
exports.default = $csb__default;"
15+
`;
16+
17+
exports[`convert-esmodule can convert default imports 1`] = `
18+
"var $csb__b = require('./b');
19+
var default = $csb__b.default;"
20+
`;
21+
22+
exports[`convert-esmodule can convert function exports 1`] = `
23+
"Object.defineProperty(exports, '__esModule', { value: true });
24+
function test() {
25+
}
26+
exports.test = test;
27+
const test2 = () => {
28+
};
29+
exports.test2 = test2;
30+
class Test {
31+
}
32+
exports.Test = Test;"
33+
`;
34+
35+
exports[`convert-esmodule can convert named imports 1`] = `
36+
"var $csb__b = require('./b');
37+
var a = $csb__b.a;
38+
var b = $csb__b.b;
39+
var c = $csb__b.c;"
40+
`;
41+
42+
exports[`convert-esmodule can convert normal exports 1`] = `
43+
"Object.defineProperty(exports, '__esModule', { value: true });
44+
var $csb__store = require('./test/store.js');
45+
exports.test = $csb__store.test;
46+
exports.test2 = $csb__store.test2;"
47+
`;
48+
49+
exports[`convert-esmodule can convert reexports 1`] = `
50+
"Object.defineProperty(exports, '__esModule', { value: true });
51+
var $csb__store = require('./test/store.js');
52+
Object.keys($csb__store).forEach(function (key) {
53+
if (key === 'default' || key === '__esModule')
54+
return;
55+
exports[key] = $csb__store[key];
56+
});"
57+
`;
58+
59+
exports[`convert-esmodule can convert weird default exports 1`] = `
60+
"Object.defineProperty(exports, '__esModule', { value: true });
61+
var $csb__default = a = 'b';
62+
exports.default = $csb__default;"
63+
`;
64+
65+
exports[`convert-esmodule can handle as imports 1`] = `
66+
"var $csb__b = require('./b');
67+
var b = $csb__b.a;"
68+
`;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { convertEsModule } from './convert-esmodule';
2+
3+
describe('convert-esmodule', () => {
4+
it('can convert reexports', () => {
5+
const code = `
6+
export * from './test/store.js';
7+
`;
8+
expect(convertEsModule('/index.js', code)).toMatchSnapshot();
9+
});
10+
11+
it('can convert normal exports', () => {
12+
const code = `
13+
export { test, test2 } from './test/store.js';
14+
`;
15+
expect(convertEsModule('/index.js', code)).toMatchSnapshot();
16+
});
17+
18+
it('can convert function exports', () => {
19+
const code = `
20+
export function test() {}
21+
export const test2 = () => {}
22+
export class Test {}
23+
`;
24+
expect(convertEsModule('/index.js', code)).toMatchSnapshot();
25+
});
26+
27+
it('can convert default exports', () => {
28+
const code = `
29+
export default function test() {}
30+
`;
31+
expect(convertEsModule('/index.js', code)).toMatchSnapshot();
32+
});
33+
34+
it('can convert class default exports', () => {
35+
const code = `
36+
export default class A {}
37+
`;
38+
expect(convertEsModule('/index.js', code)).toMatchSnapshot();
39+
});
40+
41+
it('can convert weird default exports', () => {
42+
const code = `
43+
export default a = 'b';
44+
`;
45+
expect(convertEsModule('/index.js', code)).toMatchSnapshot();
46+
});
47+
48+
it('can convert default imports', () => {
49+
const code = `
50+
import a from './b';
51+
`;
52+
expect(convertEsModule('/index.js', code)).toMatchSnapshot();
53+
});
54+
55+
it('can convert named imports', () => {
56+
const code = `
57+
import {a, b, c} from './b';
58+
`;
59+
expect(convertEsModule('/index.js', code)).toMatchSnapshot();
60+
});
61+
62+
it('can handle as imports', () => {
63+
const code = `
64+
import { a as b } from './b';
65+
`;
66+
expect(convertEsModule('/index.js', code)).toMatchSnapshot();
67+
});
68+
});

0 commit comments

Comments
 (0)