Skip to content

Commit 46afdf5

Browse files
authored
fix(lazy-load): fix potential TMZ violation (#243)
1 parent e4568d9 commit 46afdf5

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/plugins/lazy-load.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,14 @@ export const LazyLoadHintPlugin = createUnplugin(() => {
140140
}
141141
}
142142

143-
m.prepend(wrapperStatements + '\n')
143+
const lastImport = imports[imports.length - 1]
144+
// See https://github.com/nuxt/hints/issues/241
145+
if (lastImport) {
146+
m.appendRight(lastImport.end, '\n' + wrapperStatements)
147+
}
148+
else {
149+
m.prepend(wrapperStatements + '\n')
150+
}
144151

145152
if (m.hasChanged()) {
146153
return {

test/unit/hydration/lazy-hydration-plugin.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,11 @@ describe('LazyLoadHintPlugin', () => {
179179
expect(result.code).toContain('useLazyComponentTracking(')
180180
expect(result.code).toContain(`componentName: 'ChildComp'`)
181181
expect(result.code).toMatchInlineSnapshot(`
182-
"const ChildComp = __wrapImportedComponent(__original_ChildComp, 'ChildComp', './ChildComp.vue', '/src/Parent.ts')
183-
import { __wrapImportedComponent, __wrapMainComponent } from "@nuxt/hints/runtime/lazy-load/composables";
182+
"import { __wrapImportedComponent, __wrapMainComponent } from "@nuxt/hints/runtime/lazy-load/composables";
184183
import { useLazyComponentTracking } from "@nuxt/hints/runtime/lazy-load/composables";
185184
import { defineComponent } from 'vue'
186185
import __original_ChildComp from './ChildComp.vue'
186+
const ChildComp = __wrapImportedComponent(__original_ChildComp, 'ChildComp', './ChildComp.vue', '/src/Parent.ts')
187187
export default defineComponent({
188188
setup() {
189189
const lazyHydrationState = useLazyComponentTracking([{ componentName: 'ChildComp', importSource: './ChildComp.vue', importedBy: '/src/Parent.ts', rendered: false }]);
@@ -207,11 +207,11 @@ describe('LazyLoadHintPlugin', () => {
207207
const result = await transform(code, '/src/Parent.ts')
208208
expect(result.code).toContain('useLazyComponentTracking(')
209209
expect(result.code).toMatchInlineSnapshot(`
210-
"const ChildComp = __wrapImportedComponent(__original_ChildComp, 'ChildComp', './ChildComp.vue', '/src/Parent.ts')
211-
import { __wrapImportedComponent, __wrapMainComponent } from "@nuxt/hints/runtime/lazy-load/composables";
210+
"import { __wrapImportedComponent, __wrapMainComponent } from "@nuxt/hints/runtime/lazy-load/composables";
212211
import { useLazyComponentTracking } from "@nuxt/hints/runtime/lazy-load/composables";
213212
import { defineComponent } from 'vue'
214213
import __original_ChildComp from './ChildComp.vue'
214+
const ChildComp = __wrapImportedComponent(__original_ChildComp, 'ChildComp', './ChildComp.vue', '/src/Parent.ts')
215215
export default defineComponent({
216216
setup: () => {
217217
const lazyHydrationState = useLazyComponentTracking([{ componentName: 'ChildComp', importSource: './ChildComp.vue', importedBy: '/src/Parent.ts', rendered: false }]);

0 commit comments

Comments
 (0)