vite/docs/guide/migration.md
renovate[bot] 0c658de41f
fix(deps)!: update dependency dotenv-expand to v12 (#18697)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: sapphi-red <49056869+sapphi-red@users.noreply.github.com>
2024-11-20 21:21:12 +08:00

9.2 KiB

Migration from v5

Environment API

As part of the new experimental Environment API, a big internal refactoring was needed. Vite 6 strives to avoid breaking changes to ensure most projects can quickly upgrade to the new major. We'll wait until a big portion of the ecosystem has moved to stabilize and start recommending the use of the new APIs. There may be some edge cases but these should only affect low level usage by frameworks and tools. We have worked with maintainers in the ecosystem to mitigate these differences before the release. Please open an issue if you spot a regression.

Some internal APIs have been removed due to changes in Vite's implementation. If you were relying on one of them, please create a feature request.

Vite Runtime API

The experimental Vite Runtime API evolved into the Module Runner API, released in Vite 6 as part of the new experimental Environment API. Given that the feature was experimental the removal of the previous API introduced in Vite 5.1 isn't a breaking change, but users will need to update their use to the Module Runner equivalent as part of migrating to Vite 6.

General Changes

Default value for resolve.conditions

This change does not affect users that did not configure resolve.conditions / ssr.resolve.conditions / ssr.resolve.externalConditions.

In Vite 5, the default value for resolve.conditions was [] and some conditions were added internally. The default value for ssr.resolve.conditions was the value of resolve.conditions.

From Vite 6, some of the conditions are no longer added internally and need to be included in the config values. The conditions that are no longer added internally for

  • resolve.conditions are ['module', 'browser', 'development|production']
  • ssr.resolve.conditions are ['module', 'node', 'development|production']

The default values for those options are updated to the corresponding values and ssr.resolve.conditions no longer uses resolve.conditions as the default value. Note that development|production is a special variable that is replaced with production or development depending on the value of process.env.NODE_ENV. These default values are exported from vite as defaultClientConditions and defaultServerConditions.

If you specified a custom value for resolve.conditions or ssr.resolve.conditions, you need to update it to include the new conditions. For example, if you previously specified ['custom'] for resolve.conditions, you need to specify ['custom', ...defaultClientConditions] instead.

JSON stringify

In Vite 5, when json.stringify: true is set, json.namedExports was disabled.

From Vite 6, even when json.stringify: true is set, json.namedExports is not disabled and the value is respected. If you wish to achieve the previous behavior, you can set json.namedExports: false.

Vite 6 also introduces a new default value for json.stringify which is 'auto', which will only stringify large JSON files. To disable this behavior, set json.stringify: false.

Extended support of asset references in HTML elements

In Vite 5, only a few supported HTML elements were able to reference assets that will be processed and bundled by Vite, such as <link href>, <img src>, etc.

Vite 6 extends the support to even more HTML elements. The full list can be found at the HTML features docs.

To opt-out of HTML processing on certain elements, you can add the vite-ignore attribute on the element.

postcss-load-config

postcss-load-config has been updated to v6 from v4. tsx or jiti is now required to load TypeScript postcss config files instead of ts-node. Also yaml is now required to load YAML postcss config files.

Sass now uses modern API by default

In Vite 5, the legacy API was used by default for Sass. Vite 5.4 added support for the modern API.

From Vite 6, the modern API is used by default for Sass. If you wish to still use the legacy API, you can set css.preprocessorOptions.sass.api: 'legacy' / css.preprocessorOptions.scss.api: 'legacy'. But note that the legacy API support will be removed in Vite 7.

To migrate to the modern API, see the Sass documentation.

Customize CSS output file name in library mode

In Vite 5, the CSS output file name in library mode was always style.css and cannot be easily changed through the Vite config.

From Vite 6, the default file name now uses "name" in package.json similar to the JS output files. If build.lib.fileName is set with a string, the value will also be used for the CSS output file name. To explicitly set a different CSS file name, you can use the new build.lib.cssFileName to configure it.

To migrate, if you had relied on the style.css file name, you should update references to it to the new name based on your package name. For example:

{
  "name": "my-lib",
  "exports": {
    "./style.css": "./dist/style.css" // [!code --]
    "./style.css": "./dist/my-lib.css" // [!code ++]
  }
}

If you prefer to stick with style.css like in Vite 5, you can set build.lib.cssFileName: 'style' instead.

Advanced

There are other breaking changes which only affect few users.

Migration from v4

Check the Migration from v4 Guide in the Vite v5 docs first to see the needed changes to port your app to Vite 5, and then proceed with the changes on this page.