Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/plugins/lazy-load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,14 @@ export const LazyLoadHintPlugin = createUnplugin(() => {
}
}

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

if (m.hasChanged()) {
return {
Expand Down
8 changes: 4 additions & 4 deletions test/unit/hydration/lazy-hydration-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ describe('LazyLoadHintPlugin', () => {
expect(result.code).toContain('useLazyComponentTracking(')
expect(result.code).toContain(`componentName: 'ChildComp'`)
expect(result.code).toMatchInlineSnapshot(`
"const ChildComp = __wrapImportedComponent(__original_ChildComp, 'ChildComp', './ChildComp.vue', '/src/Parent.ts')
import { __wrapImportedComponent, __wrapMainComponent } from "@nuxt/hints/runtime/lazy-load/composables";
"import { __wrapImportedComponent, __wrapMainComponent } from "@nuxt/hints/runtime/lazy-load/composables";
import { useLazyComponentTracking } from "@nuxt/hints/runtime/lazy-load/composables";
import { defineComponent } from 'vue'
import __original_ChildComp from './ChildComp.vue'
const ChildComp = __wrapImportedComponent(__original_ChildComp, 'ChildComp', './ChildComp.vue', '/src/Parent.ts')
export default defineComponent({
setup() {
const lazyHydrationState = useLazyComponentTracking([{ componentName: 'ChildComp', importSource: './ChildComp.vue', importedBy: '/src/Parent.ts', rendered: false }]);
Expand All @@ -207,11 +207,11 @@ describe('LazyLoadHintPlugin', () => {
const result = await transform(code, '/src/Parent.ts')
expect(result.code).toContain('useLazyComponentTracking(')
expect(result.code).toMatchInlineSnapshot(`
"const ChildComp = __wrapImportedComponent(__original_ChildComp, 'ChildComp', './ChildComp.vue', '/src/Parent.ts')
import { __wrapImportedComponent, __wrapMainComponent } from "@nuxt/hints/runtime/lazy-load/composables";
"import { __wrapImportedComponent, __wrapMainComponent } from "@nuxt/hints/runtime/lazy-load/composables";
import { useLazyComponentTracking } from "@nuxt/hints/runtime/lazy-load/composables";
import { defineComponent } from 'vue'
import __original_ChildComp from './ChildComp.vue'
const ChildComp = __wrapImportedComponent(__original_ChildComp, 'ChildComp', './ChildComp.vue', '/src/Parent.ts')
export default defineComponent({
setup: () => {
const lazyHydrationState = useLazyComponentTracking([{ componentName: 'ChildComp', importSource: './ChildComp.vue', importedBy: '/src/Parent.ts', rendered: false }]);
Expand Down
Loading