vitejs / vite

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

build.chunkSizeLimit #18496

Open mfulton26 opened 1 week ago

mfulton26 commented 1 week ago

Description

I appreciate the warning from build.chunkSizeWarningLimit but I want to fail my build in CI if the limit is exceeded… in fact, I might want to fail the build in CI if vite prints any warnings but at a minimum for this one.

Suggested solution

Have a flag to fail on warnings, a flag to fail on chunk size warning limit exceeded, or a separate build option to set a threshold/error limit on chunk size.

I think this flag could be enabled automatically whenever the CI env var is set but that might start to fail builds unexpectedly so an opt-in approach may be best for now.

Alternative

Use separate solutions like bundlesize.

Additional context

https://github.com/vitejs/vite/discussions/16050

Validations

sapphi-red commented 1 week ago

As a workaround, you can use customLogger option to achieve this:

import { createLogger, defineConfig } from 'vite';

const logger = createLogger('warn');
const loggerWarn = logger.warn;
logger.warn = (msg, options) => {
  if (msg.includes('Some chunks are larger than')) {
    throw new Error(msg);
  }
  loggerWarn(msg, options);
};

export default defineConfig({
  build: {
    chunkSizeWarningLimit: 1,
  },
  customLogger: logger,
});

https://stackblitz.com/edit/vitejs-vite-6cfnvs?file=vite.config.ts&terminal=build

mfulton26 commented 1 week ago

Thank you @sapphi-red, that looks very promising. I'll try it out.