Skip to content

Commit

Permalink
Separating Swiper into chunks from entry bundle
Browse files Browse the repository at this point in the history
Swiper is a relatively large library. When compiling Nuxt, it increases the main entry.js by about 200kb, entry.css by about 100kb. With this modification, swiper will be separated into its own chunk, improving the loading speed of the application.
  • Loading branch information
misaon committed Jan 24, 2023
1 parent 8d6d61d commit 0b0b695
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import * as swiper from 'swiper'
import {
defineNuxtModule,
addPluginTemplate,
addImports
addImports,
extendViteConfig
} from '@nuxt/kit'
import { name, version } from '../package.json'

Expand Down Expand Up @@ -36,6 +37,19 @@ export default defineNuxtModule<SwiperModuleOptions>({
}
]

// Separating Swiper into chunks from Nuxt entry bundle
extendViteConfig((viteConfig) => {
if (viteConfig.build?.rollupOptions?.manualChunks) {
return
}

viteConfig.build!.rollupOptions!.manualChunks = function (id) {
if (id.includes('/node_modules/swiper')) {
return `vendor-swiper`
}
}
})

// Import Each Swiper Module & CSS if it exists.
for (const [key, _] of Object.entries(swiper)) {
// Turn key to snake-case.
Expand Down

0 comments on commit 0b0b695

Please sign in to comment.