Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: support plugin inside of a vite project #1362

Open
SlexAxton opened this issue Feb 17, 2023 · 5 comments
Open

Feature request: support plugin inside of a vite project #1362

SlexAxton opened this issue Feb 17, 2023 · 5 comments

Comments

@SlexAxton
Copy link

Apparently not all rollup plugins are compatible with vite, but I'd love to use this one with vite.

Inspired by #1361

Some docs here: https://vitejs.dev/guide/api-plugin.html#rollup-plugin-compatibility

Will try to look into it, but figured I'd log here in case anyone had any quick info. 🙏

@SlexAxton
Copy link
Author

I think it'd be possible to get first class support with some effort, but I was able to hack what I need with the following vite config. I used the rollup plugin as a vite plugin, but wrote a custom filter to only run it on the client pass, and not the server pass.

import license from 'rollup-plugin-license';
import {defineConfig} from 'vite';

const year = new Date().getFullYear();

export default defineConfig({
  plugins: [
    {
      ...license({
        thirdParty: {
          output: 'dist/client/assets/vendor.LICENSE.txt',
        },
      }),
      apply: (config, configEnv) => {
        // We only want to generate the file when not doing the ssr build
        return !configEnv.ssrBuild;
      },
      enforce: 'post',
    },
  ],
  base: process.env.CDN_URL,
  esbuild: {
    legalComments: 'none',
    banner: `/**
* Copyright me 2022-${year}
* See /assets/vendor.LICENSE.txt for 3rd party license information.
**/
`,
  },
  build: {
    minify: true,
    emptyOutDir: true,
    sourcemap: false,
  },
});

@mjeanroy
Copy link
Owner

Hi @SlexAxton,

I would be more than happy to add compatibility with vite. I've never used vite so far, but I thought rollup plugins were compatible, so I never looked at it.

Could you explain what needs to be changed to support vite?

@SlexAxton
Copy link
Author

SlexAxton commented Feb 28, 2023

Hi! Thanks for the response 🙏

I'm not totally sure what the differences are. I kinda thought the same as you.

The specific thing that was happening though, was that vite was running the plugin twice, once for the 'client' and once for the 'server'. Because the output path was the same for both passes, the file would be written once, and then overwritten. For whatever reason the second pass didn't include the same amount of modules as the first pass.

Presumably there's either some control to only run the build on one of the passes, or it's possible to pass in the build type (e.g. server vs client, or ssrBuild as a boolean) as a prop to an output function. That way I could output the client licenses into the dist/client/assets folder and the server licenses into the dist/server/assets folder.

If you look at this list that they have https://vite-rollup-plugins.patak.dev/ they give various plugins that are compatible with vite and others that aren't. I wonder if there's a way to determine what makes one compatible vs another. There's also this section of the docs: https://vitejs.dev/guide/api-plugin.html#rollup-plugin-compatibility

@mitar
Copy link

mitar commented Feb 29, 2024

To me it works for generating thirdParty out of the box with Vite, but I do not use SSR, just client side.

But I was not able to see where would generated banner be. I tried config from the README. I do not care about generated banner though, so it is not a problem for me.

@mitar
Copy link

mitar commented Feb 29, 2024

I only noticed that during development, file is not made available somehow. Only for production build. This is probably fine and by design, it is just that it makes testing a bit clumsy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants