File tree Expand file tree Collapse file tree 5 files changed +110
-619
lines changed Expand file tree Collapse file tree 5 files changed +110
-619
lines changed Original file line number Diff line number Diff line change @@ -860,6 +860,63 @@ exports[`plugin simple import should work with + concatenation 1`] = `
860
860
} );"
861
861
` ;
862
862
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
+
863
920
exports [` plugin simple import should work with template literal 1` ] = `
864
921
"loadable({
865
922
resolved : {},
Original file line number Diff line number Diff line change @@ -34,12 +34,17 @@ const loadablePlugin = api => {
34
34
35
35
const propertyFactories = properties . map ( init => init ( api ) )
36
36
37
- function isValidIdentifier ( path ) {
37
+ function isValidIdentifier ( path , hasLazyImport ) {
38
38
// `loadable()`
39
39
if ( path . get ( 'callee' ) . isIdentifier ( { name : 'loadable' } ) ) {
40
40
return true
41
41
}
42
42
43
+ // `lazy()`
44
+ if ( path . get ( 'callee' ) . isIdentifier ( { name : 'lazy' } ) && hasLazyImport ) {
45
+ return true
46
+ }
47
+
43
48
// `loadable.lib()`
44
49
return (
45
50
path . get ( 'callee' ) . isMemberExpression ( ) &&
@@ -106,14 +111,20 @@ const loadablePlugin = api => {
106
111
}
107
112
}
108
113
114
+
109
115
return {
110
116
inherits : syntaxDynamicImport ,
111
117
visitor : {
112
118
Program : {
113
119
enter ( programPath ) {
120
+ let hasLazyImport = false
121
+
114
122
programPath . traverse ( {
123
+ ImportDeclaration ( path ) {
124
+ hasLazyImport = hasLazyImport || path . node . source . value == '@loadable/component' && path . node . specifiers . some ( ( { imported } ) => imported . name == 'lazy' )
125
+ } ,
115
126
CallExpression ( path ) {
116
- if ( ! isValidIdentifier ( path ) ) return
127
+ if ( ! isValidIdentifier ( path , hasLazyImport ) ) return
117
128
transformImport ( path )
118
129
} ,
119
130
'ArrowFunctionExpression|FunctionExpression|ObjectMethod' : path => {
Original file line number Diff line number Diff line change @@ -20,7 +20,25 @@ describe('plugin', () => {
20
20
21
21
expect ( result ) . toMatchSnapshot ( )
22
22
} )
23
+ it ( 'should work with lazy if imported' , ( ) => {
24
+ const result = testPlugin ( `
25
+ import { lazy } from '@loadable/component'
26
+ lazy(() => import(\`./ModA\`))
27
+ ` )
23
28
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
+ } )
24
42
it ( 'should work with + concatenation' , ( ) => {
25
43
const result = testPlugin ( `
26
44
loadable(() => import('./Mod' + 'A'))
Original file line number Diff line number Diff line change 1
1
{
2
2
"dist/loadable.cjs.js" : {
3
- "bundled" : 16760 ,
4
- "minified" : 7400 ,
5
- "gzipped" : 2617
3
+ "bundled" : 16813 ,
4
+ "minified" : 7394 ,
5
+ "gzipped" : 2621
6
6
},
7
7
"dist/loadable.esm.js" : {
8
- "bundled" : 16381 ,
9
- "minified" : 7096 ,
10
- "gzipped" : 2550 ,
8
+ "bundled" : 16434 ,
9
+ "minified" : 7090 ,
10
+ "gzipped" : 2552 ,
11
11
"treeshaked" : {
12
12
"rollup" : {
13
13
"code" : 276 ,
14
14
"import_statements" : 276
15
15
},
16
16
"webpack" : {
17
- "code" : 5969
17
+ "code" : 5958
18
18
}
19
19
}
20
20
}
You can’t perform that action at this time.
0 commit comments