4

Is it possible to exclude a complete (lazy loaded) folder from a vite build? If so how.

import { defineConfig } from 'vite'
import { resolve as resolver } from 'path'

import vue from '@vitejs/plugin-vue'
import ViteComponents from 'vite-plugin-components'

const resolve = {
    alias: {
        'vue': 'vue/dist/vue.esm-bundler.js',
        '@': resolver(__dirname, './src')
    }
}

const plugins = [
    vue(),
    ViteComponents({
        dirs: ['src/shared/components'],
        extensions: ['vue'],
        deep: true,
    })
]

const build = {
        outDir: 'dist',
        assetsDir: 'assets',
        assetsInlineLimit: '4096',

        cssCodeSplit: false, // single CSS

        sourcemap: false,
        manifest: false,
        chunkSizeWarningLimit: 1000,
    }

// https://vitejs.dev/config/

export default defineConfig(({ command, mode }) => {
    if (command === 'serve') {
        return {
            // serve specific config
            plugins,
            resolve,
        }
    } else {
        return {
            // build specific config
            plugins,
            resolve,
            build
        }
    }

})

npm run build

is building my Examples. But I don't need them in production.

...
dist/assets/ExampleListGroup.58bffc95.js        0.70 KiB / gzip: 0.43 KiB
dist/assets/ExampleMainTitle.4841825d.js        0.66 KiB / gzip: 0.39 KiB
dist/assets/ExampleMarkdownEditor.05ed02a3.js   0.67 KiB / gzip: 0.39 KiB
dist/assets/ExampleMenuStructure.36b10505.js    0.68 KiB / gzip: 0.41 KiB
dist/assets/ExampleMarkDown.ef4418d4.js         0.68 KiB / gzip: 0.40 KiB
dist/assets/ExamplePopupMenu.9c9d49e2.js        0.60 KiB / gzip: 0.38 KiB
dist/assets/ExampleObjectSelect.bc2a5189.js     4.86 KiB / gzip: 1.36 KiB
...

I'd like to exclude them before build, not remove them after the build.

Thanks.

2

2 Answers 2

3

Ended up making 2 routers router.js and router-dev.js And an alias to include one of them

vite.config.js:

import { defineConfig } from 'vite'
import { resolve as resolver } from 'path'

import vue from '@vitejs/plugin-vue'
import ViteComponents from 'vite-plugin-components'

const resolve = {
    development: {
        alias: {
            'vue': 'vue/dist/vue.esm-bundler.js',
            '@': resolver(__dirname, './src'),
            '@dynarouter': resolver(__dirname, './src/router-dev.js')
        }
    },
    production: {
        alias: {
            'vue': 'vue/dist/vue.esm-bundler.js',
            '@': resolver(__dirname, './src'),
            '@dynarouter': resolver(__dirname, './src/router.js')
        }
    }
}

const plugins = [
    vue(),
    ViteComponents({
        dirs: ['src/shared/components'],
        extensions: ['vue'],
        deep: true,
    }),
]

const build = {
    outDir: 'dist',
    assetsDir: 'assets',
    assetsInlineLimit: '4096',

    cssCodeSplit: false, // single CSS

    sourcemap: false,
    manifest: false,
    chunkSizeWarningLimit: 1000,
    rollupOptions: {}
}

// https://vitejs.dev/config/

export default defineConfig(({ command, mode }) => {
    if (command === 'serve') {
        return {
            // serve specific config
            plugins,
            resolve: resolve.development,
        }
    } else {
        return {
            // build specific config
            plugins,
            resolve: resolve.production,
            build,
        }
    }
})

main.js:

import { createApp } from 'vue'
import App from '@/App.vue'


// import router from '@/router-dev.js'
// import router from '@/router.js'
import router from '@dynarouter'

import global from '@/global.js'
import store  from '@/shared/stores' 
import click  from '@/shared/directives/clickOutside.js'

const app = createApp(App)
    .use(router)
    .use(store)
    .directive('click-outside', click)

app.config.devtools = true
app.mount('#app')
Sign up to request clarification or add additional context in comments.

Comments

0

I had a similar problem. I check the environment and use lazy import to solve it. Something like this could work for you:

if (process.env.NODE_ENV !== "production") {
  routes.push({
    path: "some-route",
    name: "SomeName",
    component: () => import("@/views/app/ExampleListGroup"),
  });
}

3 Comments

That should work fine for the manual included routes but not for the autoloaded (vite-plugin-components) components.
The way I autoload components is with a small script. You can get the names. What about making a list of components you don't want to load in production? If the component being loaded by the autoloader is in the list you don't load it in production.
I guess I could add a meta property to not include the defined route in the router, but I think rollup will still compile all the code and thus upload it to the serve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.