Skip to content

Commit 3ec9b64

Browse files
authored
Added transformation of lazy (#925)
1 parent a32a7aa commit 3ec9b64

File tree

5 files changed

+110
-619
lines changed

5 files changed

+110
-619
lines changed

packages/babel-plugin/src/__snapshots__/index.test.js.snap

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,63 @@ exports[`plugin simple import should work with + concatenation 1`] = `
860860
});"
861861
`;
862862
863+
exports[`plugin simple import should work with lazy if imported 1`] = `
864+
"import { lazy } from '@loadable/component';
865+
lazy({
866+
resolved: {},
867+
868+
chunkName() {
869+
return \`ModA\`.replace(/[^a-zA-Z0-9_$()=\\\\-^°]+/g, \\"-\\");
870+
},
871+
872+
isReady(props) {
873+
const key = this.resolve(props);
874+
875+
if (this.resolved[key] !== true) {
876+
return false;
877+
}
878+
879+
if (typeof __webpack_modules__ !== 'undefined') {
880+
return !!__webpack_modules__[key];
881+
}
882+
883+
return false;
884+
},
885+
886+
importAsync: () => import(
887+
/* webpackChunkName: \\"ModA\\" */
888+
\`./ModA\`),
889+
890+
requireAsync(props) {
891+
const key = this.resolve(props);
892+
this.resolved[key] = false;
893+
return this.importAsync(props).then(resolved => {
894+
this.resolved[key] = true;
895+
return resolved;
896+
});
897+
},
898+
899+
requireSync(props) {
900+
const id = this.resolve(props);
901+
902+
if (typeof __webpack_require__ !== 'undefined') {
903+
return __webpack_require__(id);
904+
}
905+
906+
return eval('module.require')(id);
907+
},
908+
909+
resolve() {
910+
if (require.resolveWeak) {
911+
return require.resolveWeak(\`./ModA\`);
912+
}
913+
914+
return eval('require.resolve')(\`./ModA\`);
915+
}
916+
917+
});"
918+
`;
919+
863920
exports[`plugin simple import should work with template literal 1`] = `
864921
"loadable({
865922
resolved: {},

packages/babel-plugin/src/index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,17 @@ const loadablePlugin = api => {
3434

3535
const propertyFactories = properties.map(init => init(api))
3636

37-
function isValidIdentifier(path) {
37+
function isValidIdentifier(path, hasLazyImport) {
3838
// `loadable()`
3939
if (path.get('callee').isIdentifier({ name: 'loadable' })) {
4040
return true
4141
}
4242

43+
// `lazy()`
44+
if (path.get('callee').isIdentifier({ name: 'lazy' }) && hasLazyImport) {
45+
return true
46+
}
47+
4348
// `loadable.lib()`
4449
return (
4550
path.get('callee').isMemberExpression() &&
@@ -106,14 +111,20 @@ const loadablePlugin = api => {
106111
}
107112
}
108113

114+
109115
return {
110116
inherits: syntaxDynamicImport,
111117
visitor: {
112118
Program: {
113119
enter(programPath) {
120+
let hasLazyImport = false
121+
114122
programPath.traverse({
123+
ImportDeclaration(path) {
124+
hasLazyImport = hasLazyImport || path.node.source.value == '@loadable/component' && path.node.specifiers.some(({ imported })=>imported.name=='lazy')
125+
},
115126
CallExpression(path) {
116-
if (!isValidIdentifier(path)) return
127+
if (!isValidIdentifier(path, hasLazyImport)) return
117128
transformImport(path)
118129
},
119130
'ArrowFunctionExpression|FunctionExpression|ObjectMethod': path => {

packages/babel-plugin/src/index.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,25 @@ describe('plugin', () => {
2020

2121
expect(result).toMatchSnapshot()
2222
})
23+
it('should work with lazy if imported', () => {
24+
const result = testPlugin(`
25+
import { lazy } from '@loadable/component'
26+
lazy(() => import(\`./ModA\`))
27+
`)
2328

29+
expect(result).toMatchSnapshot()
30+
})
31+
it('should not work with lazy if not imported', () => {
32+
const result = testPlugin(`
33+
import React, { lazy } from 'react'
34+
lazy(() => import(\`./ModA\`))
35+
`)
36+
37+
expect(result).toMatchInlineSnapshot(`
38+
"import React, { lazy } from 'react';
39+
lazy(() => import(\`./ModA\`));"
40+
`)
41+
})
2442
it('should work with + concatenation', () => {
2543
const result = testPlugin(`
2644
loadable(() => import('./Mod' + 'A'))

packages/component/.size-snapshot.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
{
22
"dist/loadable.cjs.js": {
3-
"bundled": 16760,
4-
"minified": 7400,
5-
"gzipped": 2617
3+
"bundled": 16813,
4+
"minified": 7394,
5+
"gzipped": 2621
66
},
77
"dist/loadable.esm.js": {
8-
"bundled": 16381,
9-
"minified": 7096,
10-
"gzipped": 2550,
8+
"bundled": 16434,
9+
"minified": 7090,
10+
"gzipped": 2552,
1111
"treeshaked": {
1212
"rollup": {
1313
"code": 276,
1414
"import_statements": 276
1515
},
1616
"webpack": {
17-
"code": 5969
17+
"code": 5958
1818
}
1919
}
2020
}

0 commit comments

Comments
 (0)