Convert a webpack/vue-cli project to vite project. 将 webpack/vue-cli 项目转换为 vite 项目。
Convert a webpack/vue-cli project to vite project. 将 webpack/vue-cli 项目转换为 vite 项目。
English | 简体中文
convert a webpack project to a vite project.
It has been offically included, detail
Use it directly with npx:
$ npx @originjs/webpack-to-vite ...or install it globally:
$ npm install @originjs/webpack-to-vite -g
$ webpack-to-vite The converted Vite project could be found in a new directory filename-toVite.
Note: the default conversion is vue-cli project. Pass in the
-t webpackoption to convert a webpack project.
The CLI provides the following options:
…The following is a list of projects that successfully converted from a webpack project to a vite project using the tool
The following is a list of configuration items that need to convert
Legend of annotations:
| Mark | Description |
|---|---|
| ✅ | auto convert by webpack-to-vite |
| ⚠️ | need manual convert |
| ❌ | not support now |
package.jsonvite-plugin-env-compatible, vite-plugin-html, vite,vite-plugin-vue2@vue/compiler-sfc, @vitejs/plugin-vue, @vitejs/plugin-vue-jsxindex.html to root directorypages option in vue.config.js is supported . There's no need to add dev-client entry point cause vite supports HMR by defaultvite.config.js to root directoryvite.config.jsvite-plugin-env-compatiblevite-plugin-vue2, we set { jsx: true } option to enable jsx support by default@vitejs/plugin-vue, @vitejs/plugin-vue-jsx.vue extension is supportedresolve.extensions is set to be ['.mjs','.js','.ts','.jsx','.tsx','.json ','.vue'], in vite.config.js,
then you may encounter errors like 'Problems caused by using alisaes and omitting file suffixes at the same time'.
We use a patch to fix this issue, in case of vite didn't accept relate PR^2.5.0sass is supportednode-sass is used in dependency, then we'll convert it to sass to dependenciespostcss 8 is supportedpostcss 8 is used, then we'll add postcss to dependenciesUncaught SyntaxError: The requested module '/src/app/reducers/state.ts' does not provide an export named 'RootState'Hot Module Replacement(aka HMR) related code because vite supports HMR by default.index.tsx:6 Uncaught ReferenceError: module is not defined
at index.tsx:6.module.css is considered a CSS modules file.css to files with extension of .module.css to implement CSS Moduleshtml-webpack-plugin is supportedvite-plugin-htmlindex.html will be transformed. for example, ->createHtmlPlugin from html-webpack-plugin and use it like this:plugins: [
createHtmlPlugin({
inject: {
data: {
title: value,
},
},
minify: {
minifyCss: true
}
})
]windi.css was used before, you should check if Vite has supported it, and then import related plugin (vite-plugin-windicss) manually refered to its plugin guide.Vue-CLI conversion is based on
vue.config.js. Configurations will be transformed and written tovite.config.js
✅ V01: base public path
process.env.PUBLIC_URL or publicPath or baseUrl -> base✅ V02: css options
css.loaderOptions -> css.preprocessorOptionscss.loaderOptions.less.lessOptions.modifyVars -> css.preprocessorOptions.less.modifyVarssass configuration will influence both sass and scss in Vue-CLI, but in vite we need to configure them respectively.
So with only css.loaderOptions.sass option is set in Vue-CLI, it will be converted to css.preprocessorOptions.sass and css.preprocessorOptions.scss.
On the other hand, with only css.loaderOptions.scss option is set in Vue-CLI, it will be converted to css.preprocessorOptions.scss.✅ V03: server options
server.strictPort = false is set by defaultprocess.env.PORT or devServer.port -> server.portprocess.env.DEV_HOST or devServer.public or devServer.host -> server.host, and convert http:// or https:// to ''devServer.open, devServer.https -> server.open, server.httpsdevServer.proxy -> server.proxy is transformed in proxy configuration, we'll also pathRewrite -> rewrite✅ V04: build options
outputDir -> build.outDircss.extract -> build.cssCodeSplitprocess.env.MODERN === 'true' is set, we'll also set build.minify = esbuildprocess.env.GENERATE_SOURCEMAP === 'true' or vueConfig.productionSourceMap or css.sourceMap -> build.sourcemap✅ V05: resolve.alias options
resolve: {
alias: [
{ find: '/^~/', replacement: ''},
{ find: '@', replacement: path.resolve(__dirname,'src') }
]
}✅ V06: client-side env variables
VUE_APP_VARIABLE -> process.env['VUE_APP_VARIABLE']✅ V07: css automatic imports
pluginOptions['style-resources-loader']. Configurations will be transformed and written to css.preprocessorOptionspluginOptions: {
'style-resources-loader': {
preProcessor: 'less',
patterns: [
resolve('src/styles/var.less'),
resolve('src/styles/mixin.less')
]
}
}->
css: {
preprocessorOptions: {
less: {
additionalData: `@import "src/styles/var.less";@import "src/styles/mixin.less";`
}
}
}✅ V08: transform functional webpack config
webpackConfigure and chainWebpack options could be defined as object or function in vue.config.js modulevue.temp.config.js) to reconfig html-webpack-pluginWebpack conversion is based on
webpack.config.jsorwebpack.base.js/webpack.dev.js/webpack.prod.jsorwebpack.build.js/webpack.production.js, map configuration tovite.config.js
Note: if you are not using configuration files above, you need to convert configurations manually
entry is string type: entry -> build.rollupOptions.inputentry is object type: the properties of entry will be converted set to build.rollupOptions.inputentry is function type: execute result of entry will be set to build.rollupOptions.inputoutput optionsoutput.path -> build.outDiroutput.filename -> build.rollupOptions.output.entryFileNamesoutput.chunkFilename -> build.rollupOptions.output.chunkFileNamesresolve.alias optionsresolve: {
alias: [
{ find: '@', replacement: path.resolve(__dirname,'src') }
]
}resolve.alias configurations trailing with $, we'll remove the trailing '$' and set an accurate valuedevServer.host, devServer.port, devServer.proxy, devServer.https, devServer.contentBase -> server.host, server.port, server.proxy, server.https, server.basenew webpack.DefinePlugin() -> definerequire('./')@originjs/vite-plugin-commonjs, see also here.
Please note that the plugin only supports part of CommonJS syntax. That means some syntax is not supported. You need to covert them to ES Modules syntax manuallyrequire('@assets/images/' + options.src)), you can refer to the following stepsnew URL
...or use Vite's API import.meta.glob
….vue SFC// img1.vue
Element-UI, see also here…@import '~@/styles/global.scss', which includes alias ~ and @ at the same time { find: /^~@/, replacement: path.resolve(__dirname, 'src') } to resolve.alias options, and place it as the first alias configurationjsx syntax in .vue filejsx support : In Vue2, add plugin vite-plugin-vue2 and set { jsx: true } option. In Vue3, add plugin @vitejs/plugin-vue-jsxlang="jsx" to script label if jsx syntax is used, e.g. … you can try to update configuration of babel.config.js like
暂无开放 Issues,或尚未同步最近议题。