-
-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathindex.js
More file actions
159 lines (136 loc) · 4.59 KB
/
index.js
File metadata and controls
159 lines (136 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { declare } from "@babel/helper-plugin-utils";
import syntaxDynamicImport from '@babel/plugin-syntax-dynamic-import'
import chunkNameProperty from './properties/chunkName'
import isReadyProperty from './properties/isReady'
import importAsyncProperty from './properties/importAsync'
import requireAsyncProperty from './properties/requireAsync'
import requireSyncProperty from './properties/requireSync'
import resolveProperty from './properties/resolve'
import stateProperty from './properties/state'
const properties = [
stateProperty,
chunkNameProperty,
isReadyProperty,
importAsyncProperty,
requireAsyncProperty,
requireSyncProperty,
resolveProperty,
]
const LOADABLE_COMMENT = '#__LOADABLE__'
const loadablePlugin = declare((api, babelOptions) => {
const { defaultImportSpecifier = 'loadable' } = babelOptions
const { types: t } = api
function collectImportCallPaths(startPath) {
const imports = []
startPath.traverse({
Import(importPath) {
imports.push(importPath.parentPath)
},
})
return imports
}
const propertyFactories = properties.map(init => init(api, babelOptions))
function isValidIdentifier(path, loadableImportSpecifier, lazyImportSpecifier) {
// `loadable()`
if (loadableImportSpecifier && path.get('callee').isIdentifier({ name: loadableImportSpecifier })) {
return true
}
// `lazy()`
if (lazyImportSpecifier && path.get('callee').isIdentifier({ name: lazyImportSpecifier })) {
return true
}
// `loadable.lib()`
return (
loadableImportSpecifier &&
path.get('callee').isMemberExpression() &&
path.get('callee.object').isIdentifier({ name: loadableImportSpecifier }) &&
path.get('callee.property').isIdentifier({ name: 'lib' })
)
}
function hasLoadableComment(path) {
const comments = path.get('leadingComments')
const comment = comments.find(
({ node }) =>
node && node.value && String(node.value).includes(LOADABLE_COMMENT),
)
if (!comment) return false
comment.remove()
return true
}
function getFuncPath(path) {
const funcPath = path.isCallExpression() ? path.get('arguments.0') : path
if (
!funcPath.isFunctionExpression() &&
!funcPath.isArrowFunctionExpression() &&
!funcPath.isObjectMethod()
) {
return null
}
return funcPath
}
function transformImport(path) {
const callPaths = collectImportCallPaths(path)
// Ignore loadable function that does not have any "import" call
if (callPaths.length === 0) return
// Multiple imports call is not supported
if (callPaths.length > 1) {
throw new Error(
'loadable: multiple import calls inside `loadable()` function are not supported.',
)
}
const [callPath] = callPaths
const funcPath = getFuncPath(path)
if (!funcPath) return
funcPath.node.params = funcPath.node.params || []
const object = t.objectExpression(
propertyFactories.map(getProperty =>
getProperty({ path, callPath, funcPath }),
),
)
if (funcPath.isObjectMethod()) {
funcPath.replaceWith(
t.objectProperty(funcPath.node.key, object, funcPath.node.computed),
)
} else {
funcPath.replaceWith(object)
}
}
return {
inherits: syntaxDynamicImport,
visitor: {
Program: {
enter(programPath) {
let loadableImportSpecifier = defaultImportSpecifier
let lazyImportSpecifier = false
programPath.traverse({
ImportDefaultSpecifier(path) {
if (!loadableImportSpecifier) {
const { parent } = path
const { local } = path.node
loadableImportSpecifier = parent.source.value == '@loadable/component' &&
local && local.name
}
},
ImportSpecifier(path) {
if (!lazyImportSpecifier) {
const { parent } = path
const { imported, local } = path.node
lazyImportSpecifier = parent.source.value == '@loadable/component' &&
imported && imported.name == 'lazy' && local && local.name
}
},
CallExpression(path) {
if (!isValidIdentifier(path, loadableImportSpecifier, lazyImportSpecifier)) return
transformImport(path)
},
'ArrowFunctionExpression|FunctionExpression|ObjectMethod': path => {
if (!hasLoadableComment(path)) return
transformImport(path)
},
})
},
},
},
}
})
export default loadablePlugin