vitejs / vite

Next generation frontend tooling. It's fast!
http://vitejs.dev
MIT License
66.78k stars 5.99k forks source link

Filter options for Vite built-in transformation plugins #12168

Open brawaru opened 1 year ago

brawaru commented 1 year ago

Description

I have developed a plugin for Rollup that automatically compiles ICU MessageFormat messages stored in JSON files of specific schema into an AST. It makes it possible to not bring a huge parser into runtime, which reduces bundle size and improves performance when interpreting the messages. The plugin is @braw/rollup-plugin-icu-messages.

Unfortunately, Vite's built-in JSON parsing plugin does not include any options to exclude specific files from parsing, which creates a conflict, since either Vite or icu-messages finds the file already parsed and fails to process it. In Rollup this conflict could've been easily solved by passing exclude: ['glob/to/locale-files/*.json'] option. Unfortunately, there is no such option in Vite.

Suggested solution

Provide filter options (include, exclude) like in common Rollup plugins to exclude or include certain files for transformation, like this:

import { defineConfig } from 'vite'

export default defineConfig({
  json: {
    exclude: ['./src/locales/*.json'], // <- makes vite:json avoid transformation of any JSON files in `./src/locales`.
  },
})

Alternative

Additional context

No response

Validations

bluwy commented 1 year ago

You could use Vite plugin's configResolved hook to access the list of plugins, including Vite's internal one, that patch the vite:json plugin too.

brawaru commented 1 year ago

Somehow missed the configResolved hook (perhaps because I tried config hook, which does not have built-in plugins) and used configureServer, but I suppose configResolve works better. Released a new version of my plugin that makes wrapping work in Vite too, but I still feel this issue is relevant, because having a way to exclude files from transformation without plugin patching is much better.