|
5 | 5 | ExpressionNode,
|
6 | 6 | NodeTransform,
|
7 | 7 | NodeTypes,
|
| 8 | + SimpleExpressionNode, |
8 | 9 | SourceLocation,
|
9 | 10 | TransformContext
|
10 | 11 | } from '@vue/compiler-core'
|
@@ -153,30 +154,42 @@ function getImportsExpressionExp(
|
153 | 154 | context: TransformContext
|
154 | 155 | ): ExpressionNode {
|
155 | 156 | if (path) {
|
156 |
| - const existing = context.imports.find(i => i.path === path) |
157 |
| - if (existing) { |
158 |
| - return existing.exp as ExpressionNode |
159 |
| - } |
160 |
| - const name = `_imports_${context.imports.length}` |
161 |
| - const exp = createSimpleExpression( |
162 |
| - name, |
163 |
| - false, |
164 |
| - loc, |
165 |
| - ConstantTypes.CAN_HOIST |
166 |
| - ) |
167 |
| - context.imports.push({ exp, path }) |
168 |
| - if (hash && path) { |
169 |
| - return context.hoist( |
170 |
| - createSimpleExpression( |
171 |
| - `${name} + '${hash}'`, |
172 |
| - false, |
173 |
| - loc, |
174 |
| - ConstantTypes.CAN_HOIST |
175 |
| - ) |
176 |
| - ) |
| 157 | + let name: string |
| 158 | + let exp: SimpleExpressionNode |
| 159 | + const existingIndex = context.imports.findIndex(i => i.path === path) |
| 160 | + if (existingIndex > -1) { |
| 161 | + name = `_imports_${existingIndex}` |
| 162 | + exp = context.imports[existingIndex].exp as SimpleExpressionNode |
177 | 163 | } else {
|
| 164 | + name = `_imports_${context.imports.length}` |
| 165 | + exp = createSimpleExpression(name, false, loc, ConstantTypes.CAN_HOIST) |
| 166 | + context.imports.push({ exp, path }) |
| 167 | + } |
| 168 | + |
| 169 | + if (!hash) { |
178 | 170 | return exp
|
179 | 171 | }
|
| 172 | + |
| 173 | + const hashExp = `${name} + '${hash}'` |
| 174 | + const existingHoistIndex = context.hoists.findIndex(h => { |
| 175 | + return ( |
| 176 | + h && |
| 177 | + h.type === NodeTypes.SIMPLE_EXPRESSION && |
| 178 | + !h.isStatic && |
| 179 | + h.content === hashExp |
| 180 | + ) |
| 181 | + }) |
| 182 | + if (existingHoistIndex > -1) { |
| 183 | + return createSimpleExpression( |
| 184 | + `_hoisted_${existingHoistIndex + 1}`, |
| 185 | + false, |
| 186 | + loc, |
| 187 | + ConstantTypes.CAN_HOIST |
| 188 | + ) |
| 189 | + } |
| 190 | + return context.hoist( |
| 191 | + createSimpleExpression(hashExp, false, loc, ConstantTypes.CAN_HOIST) |
| 192 | + ) |
180 | 193 | } else {
|
181 | 194 | return createSimpleExpression(`''`, false, loc, ConstantTypes.CAN_HOIST)
|
182 | 195 | }
|
|
0 commit comments