Home > Net >  Vite: Including files in build output
Vite: Including files in build output

Time:12-11

This is a Vue 3 Vuetify TS Vite VSCode project.

I'm trying to bundle an XML file in the production build. Some transformation needs to be applied on the file before spitting it out. Found this Vite plug-in that can do transformations. But unfortunately, it doesn't seem to touch XML files in any way. If I put my XML file in public folder, it gets copied to the build output, but is not processed by the transformation plugin. If I put it in assets or somewhere else under src, it is simply ignored.

How can I ask Vite to include certain file(s) in the build output and pass it through transformation?

Note: Before I migrated the project to Vite, I was using Vue 2 and WebPack, where I could use the well-known CopyWebpackPlugin to perform this transformation. Haven't been able to find locate its Vite equivalent till now.

CodePudding user response:

You may want to just write a script to do the transformation and add it to your npm scripts. I created a simple chrome extension to play around with VITE. Having multiple html files was pretty simple:

import { defineConfig, BuildOptions } from 'vite'
import vue from '@vitejs/plugin-vue'
const { resolve } = require('path')

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  build: {
    rollupOptions: {
      input: {
        main: resolve(__dirname, 'index.html'),
        popup: resolve(__dirname, 'popup/index.html'),
        options: resolve(__dirname, 'options/index.html'),
      },
    }
  }
})

But I had to create a separate vite config file to process the background script since it had special configuration (didn't want hashing so I could specify the name in my manifest, esm module format), and it takes the typescript and outputs 'background.js' in the public folder:

import { defineConfig } from 'vite'
const { resolve } = require('path')

// https://vitejs.dev/config/
export default defineConfig({
  build: {
    emptyOutDir: false,
    rollupOptions: {
      input: resolve(__dirname, 'background.ts'),
      output: {
        format: "esm",
        file: "public/background.js",
        dir: null,
      }
    }
  }
})

You could simply have the xml file in your src folder and run a special script (create a 'scripts' folder maybe) to do the transform and store the result in the public folder where vite will pick it up and copy it to the dist folder. Your 'build' script in package.json could look something like this:

  "scripts": {
    "build": "node scripts/transform-xml.mjs && vite build",
  },

CodePudding user response:

Author of the package has introduced a new option named replaceFiles in the version 2.0.1 using which you can specify the files that will be passed through the transform pipeline. I can now do the following in my vite.config.js to replace variables in my output manifest.xml file after build:

const replaceFiles = [resolve(join(__dirname, '/dist/manifest.xml'))];

return defineConfig({
  ...
  plugins: [
  vue(),
  transformPlugin({
    replaceFiles,
    replace: {
      VERSION_NUMBER: process.env.VITE_APP_VERSION,
      SERVER_URL: process.env.VITE_SERVER_URL,
    },
    ...
  }),
  ...
});
  • Related