vitejs / vite

Next generation frontend tooling. It's fast!
http://vite.dev
MIT License
68.51k stars 6.19k forks source link

rollup-plugin-banner2 doesn't work with vite #1623

Closed phiphou closed 3 years ago

phiphou commented 3 years ago

⚠️ IMPORTANT ⚠️ Please check the following list before proceeding. If you ignore this issue template, your issue will be directly closed.

Describe the bug

I'm trying to use rollup-plugin-banner2, added it the the rollup plugins in rollupOptions but I dont'see the banner. Is there a default rollup config option that override it ? Also tried with rollup-plugin-banner but with no luck.

Reproduction

https://github.com/stropho/rollup-plugin-banner2

System Info

yyx990803 commented 3 years ago
  1. The link is not a valid reproduction.
  2. The banner plugin uses renderChunk and will only take effect during build.
phiphou commented 3 years ago

Solved with a custom rollup plugin :

const bannerPlugin = (banner) => {
  return {
    name: 'banner',
    async writeBundle (NULL, bundle) {
      for (const fileName of Object.entries(bundle)) {
        const file = fileName[0]
        const extRegex = new RegExp(/\.(css|js)$/i)
        const vendorRegex = new RegExp(/vendor/)
        if (extRegex.test(file) && !vendorRegex.test(file)) {
          let data = fs.readFileSync(`./dist/${file}`, { encoding: 'utf8' })
          data = `/* ${banner} */ ${data}`
          fs.writeFileSync(`./dist/${file}`, data)
        }
      }
    }
  }
}

build: {
    rollupOptions: {
      plugins: [
        bannerPlugin('Your banner')
      ]
    }
  }
chengpeiquan commented 3 years ago

Solved with a custom rollup plugin :

const bannerPlugin = (banner) => {
  return {
    name: 'banner',
    async writeBundle (NULL, bundle) {
      for (const fileName of Object.entries(bundle)) {
        const file = fileName[0]
        const extRegex = new RegExp(/\.(css|js)$/i)
        const vendorRegex = new RegExp(/vendor/)
        if (extRegex.test(file) && !vendorRegex.test(file)) {
          let data = fs.readFileSync('./dist/' + file, { encoding: 'utf8' })
          data = `/* ${banner} */ ${data}`
          fs.writeFileSync('./dist/' + file, data)
        }
      }
    }
  }
}

build: {
    rollupOptions: {
      plugins: [
        bannerPlugin('Your banner')
      ]
    }
  }

Nice! Thanks for your solution!