Skip to content

Commit 25b8873

Browse files
committed
feat: auto-resolve alias support (closes #4)
1 parent 7fcdbd4 commit 25b8873

File tree

10 files changed

+8031
-29
lines changed

10 files changed

+8031
-29
lines changed

.npmignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
yarn-error.log
33
logo.png
44
.github
5+
/temp
6+
.prettierignore

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/temp

config/index.ts

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import envCompatible from 'vite-plugin-env-compatible'
55
import vueCli, { VueCliOptions } from 'vite-plugin-vue-cli'
66
import mpa from 'vite-plugin-mpa'
77
import { Options } from './options'
8+
import Config from 'webpack-chain'
9+
import merge from 'webpack-merge'
10+
import { name } from '../package.json'
811

912
// vue.config.js
1013
let vueConfig: VueCliOptions = {}
@@ -14,36 +17,42 @@ try {
1417
/**/
1518
}
1619

17-
// vueConfig
18-
// @see https://cli.vuejs.org/config/#baseurl
19-
//{
20-
// publicPath: '/',
21-
// outputDir: 'build',
22-
// productionSourceMap: false,
23-
// css: {
24-
// sourceMap: false,
25-
// },
26-
// configureWebpack: Object | Function
27-
// chainWebpack: Function
28-
// devServer: {
29-
// port: Number
30-
// host: String
31-
// proxy: Object,
32-
// before: Function
33-
// },
34-
// pluginOptions: {
35-
// vite: {
36-
// alias: Record<string, string>,
37-
// plugins: Plugin[],
38-
// vitePluginVue2Options: { jsx: true }
39-
// },
40-
//},
41-
4220
const pluginOptions = vueConfig.pluginOptions || {}
4321
const viteOptions: Options = pluginOptions.vite || {}
44-
const alias = viteOptions.alias || {}
4522
const extraPlugins = viteOptions.plugins || []
4623

24+
if (viteOptions.alias) {
25+
console.log(
26+
`[${name}]: pluginOptions.vite.alias is deprecated, will auto resolved from chainWebpack / configureWebpack`,
27+
)
28+
}
29+
30+
const chainableConfig = new Config()
31+
vueConfig.chainWebpack(chainableConfig)
32+
// @see temp/webpack*.js & temp/vue.config.js
33+
const aliasOfChainWebpack = chainableConfig.resolve.alias.entries()
34+
const aliasOfConfigureWebpackObjectMode =
35+
(vueConfig.configureWebpack &&
36+
vueConfig.configureWebpack.resolve &&
37+
vueConfig.configureWebpack.resolve.alias) ||
38+
{}
39+
const aliasOfConfigureWebpackFunctionMode = (() => {
40+
if (typeof vueConfig.configureWebpack === 'function') {
41+
let originConfig = chainableConfig.toConfig()
42+
const res = vueConfig.configureWebpack(originConfig)
43+
originConfig = merge(originConfig, res)
44+
if (res) {
45+
return res.resolve.alias || {}
46+
}
47+
return originConfig.resolve.alias || {}
48+
}
49+
})()
50+
const alias = {
51+
...aliasOfConfigureWebpackObjectMode,
52+
...aliasOfConfigureWebpackFunctionMode,
53+
...aliasOfChainWebpack,
54+
}
55+
4756
const useMPA = Boolean(vueConfig.pages)
4857

4958
const plugins = [

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-cli-plugin-vite",
3-
"version": "0.1.4",
3+
"version": "0.2.0",
44
"description": "out-of-box vite dev for vue-cli project",
55
"main": "index.js",
66
"author": "[email protected]",
@@ -23,18 +23,21 @@
2323
],
2424
"dependencies": {
2525
"vite": "2.0.1",
26-
"vite-plugin-vue2": "1.2.1",
2726
"vite-plugin-env-compatible": "0.0.3",
27+
"vite-plugin-mpa": "0.1.0",
2828
"vite-plugin-vue-cli": "0.2.0",
29-
"vite-plugin-mpa": "0.1.0"
29+
"vite-plugin-vue2": "1.2.1"
3030
},
3131
"devDependencies": {
3232
"@commitlint/cli": "7.2.0",
3333
"@commitlint/config-conventional": "7.1.2",
3434
"@types/node": "14.14.25",
35+
"@types/webpack-merge": "^4.1.5",
3536
"commitizen": "2.10.1",
3637
"cz-customizable": "5.2.0",
3738
"lint-staged": "10.5.4",
39+
"webpack-chain": "^6.3.1",
40+
"webpack-merge": "^4.2.2",
3841
"yorkie": "2.0.0"
3942
},
4043
"prettier": {

temp/vue.config.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
const path = require('path')
2+
const appConfig = require('./src/app.config')
3+
4+
const resolve = (p) => path.resolve(__dirname, p)
5+
6+
/** @type import('@vue/cli-service').ProjectOptions */
7+
module.exports = {
8+
// mode-1
9+
// configureWebpack: {
10+
// resolve: {
11+
// alias: {
12+
// '@@': resolve('.'),
13+
// },
14+
// }
15+
// },
16+
// mode-2
17+
// configureWebpack: config => {
18+
// config.resolve.alias = {
19+
// '@@@': resolve('.'),
20+
// }
21+
// },
22+
// // mode-3
23+
// configureWebpack: config => {
24+
// config.resolve.alias = {
25+
// '@@@@': resolve('.'),
26+
// }
27+
// return config
28+
// },
29+
// mode-4
30+
// configureWebpack: () => {
31+
// return {
32+
// resolve: {
33+
// alias: {
34+
// '@@@@@': resolve('.'),
35+
// }
36+
// }
37+
// }
38+
// },
39+
// https://github.com/neutrinojs/webpack-chain/tree/v4#getting-started
40+
chainWebpack(config) {
41+
// We provide the app's title in Webpack's name field, so that
42+
// it can be accessed in index.html to inject the correct title.
43+
config.set('name', appConfig.title)
44+
45+
// Set up all the aliases we use in our app.
46+
config.resolve.alias.clear().merge(require('./aliases.config').webpack)
47+
48+
// Don't allow importing .vue files without the extension, as
49+
// it's necessary for some Vetur autocompletions.
50+
config.resolve.extensions.delete('.vue')
51+
52+
// Only enable performance hints for production builds,
53+
// outside of tests.
54+
config.performance.hints(
55+
process.env.NODE_ENV === 'production' &&
56+
!process.env.VUE_APP_TEST &&
57+
'warning'
58+
)
59+
},
60+
css: {
61+
// Enable CSS source maps.
62+
sourceMap: true,
63+
},
64+
// Configure Webpack's dev server.
65+
// https://cli.vuejs.org/guide/cli-service.html
66+
devServer: {
67+
...(process.env.API_BASE_URL
68+
? // Proxy API endpoints to the production base URL.
69+
{ proxy: { '/api': { target: process.env.API_BASE_URL } } }
70+
: // Proxy API endpoints a local mock API.
71+
{ before: require('./tests/mock-api') }),
72+
},
73+
pluginOptions: {
74+
vite: {
75+
// alias: {
76+
// '@': resolve('.'),
77+
// '@src': resolve('src'),
78+
// '@router': resolve('src/router'),
79+
// '@views': resolve('src/router/views'),
80+
// '@layouts': resolve('src/router/layouts'),
81+
// '@components': resolve('src/components'),
82+
// '@assets': resolve('src/assets'),
83+
// '@utils': resolve('src/utils'),
84+
// '@state': resolve('src/state'),
85+
// '@design': resolve('src/design/index.scss'),
86+
// },
87+
vitePluginVue2Options: {
88+
jsx: true,
89+
},
90+
},
91+
},
92+
}

0 commit comments

Comments
 (0)