mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-05 19:41:42 +08:00
feat(@vant/cli): support custom vite config (#9738)
This commit is contained in:
parent
4778b07d40
commit
8585842712
@ -7,6 +7,7 @@
|
||||
- [build.site.publicPath](#buildsitepublicpath)
|
||||
- [build.srcDir](#buildsrcdir)
|
||||
- [build.namedExport](#buildnamedexport)
|
||||
- [build.configureVite](#buildconfigurevite)
|
||||
- [site.title](#sitetitle)
|
||||
- [site.logo](#sitelogo)
|
||||
- [site.description](#sitedescription)
|
||||
@ -138,6 +139,47 @@ module.exports = {
|
||||
|
||||
开启此选项后,会通过 `export * from 'xxx'` 导出组件内部的所有模块、类型定义。
|
||||
|
||||
### build.configureVite
|
||||
|
||||
- Type: `(config: InlineConfig): InlineConfig`
|
||||
- Default: `undefined`
|
||||
|
||||
vant-cli 使用 vite 来构建组件库和文档站点,通过 `configureVite` 选项可以自定义 vite 配置(从 4.0.0 版本开始支持)。
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
build: {
|
||||
configureVite(config) {
|
||||
// 添加一个自定义插件
|
||||
config.plugins.push(vitePluginXXX);
|
||||
return config;
|
||||
},
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
在自定义配置时,可以通过 `process.env.BUILD_TARGET` 对构建目标进行区分:
|
||||
|
||||
```js
|
||||
module.exports = {
|
||||
build: {
|
||||
configureVite(config) {
|
||||
const { BUILD_TARGET } = process.env;
|
||||
|
||||
if (BUILD_TARGET === 'package') {
|
||||
// 修改组件库构建配置
|
||||
}
|
||||
|
||||
if (BUILD_TARGET === 'site') {
|
||||
// 修改文档站点构建配置
|
||||
}
|
||||
|
||||
return config;
|
||||
},
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
### site.title
|
||||
|
||||
- Type: `string`
|
||||
|
@ -1,3 +1,4 @@
|
||||
import { get } from 'lodash';
|
||||
import { sep, join } from 'path';
|
||||
import {
|
||||
lstatSync,
|
||||
@ -7,6 +8,7 @@ import {
|
||||
outputFileSync,
|
||||
} from 'fs-extra';
|
||||
import { SRC_DIR, getVantConfig } from './constant';
|
||||
import type { InlineConfig } from 'vite';
|
||||
|
||||
export const EXT_REGEXP = /\.\w+$/;
|
||||
export const SFC_REGEXP = /\.(vue)$/;
|
||||
@ -134,4 +136,14 @@ export function smartOutputFile(filePath: string, content: string) {
|
||||
outputFileSync(filePath, content);
|
||||
}
|
||||
|
||||
export function mergeCustomViteConfig(config: InlineConfig) {
|
||||
const vantConfig = getVantConfig();
|
||||
const configureVite = get(vantConfig, 'build.configureVite');
|
||||
|
||||
if (configureVite) {
|
||||
return configureVite(config);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
export { getVantConfig };
|
||||
|
@ -1,6 +1,8 @@
|
||||
import { build } from 'vite';
|
||||
import { mergeCustomViteConfig } from '../common';
|
||||
import { getViteConfigForPackage } from '../config/vite.package';
|
||||
|
||||
export async function compilePackage(minify: boolean) {
|
||||
return build(getViteConfigForPackage(minify));
|
||||
const config = mergeCustomViteConfig(getViteConfigForPackage(minify));
|
||||
return build(config);
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import {
|
||||
getViteConfigForSiteDev,
|
||||
getViteConfigForSiteProd,
|
||||
} from '../config/vite.site';
|
||||
import { replaceExt } from '../common';
|
||||
import { mergeCustomViteConfig, replaceExt } from '../common';
|
||||
import { CSS_LANG } from '../common/css';
|
||||
import { genPackageEntry } from './gen-package-entry';
|
||||
import { genPackageStyle } from './gen-package-style';
|
||||
@ -37,9 +37,11 @@ export async function genSiteEntry(): Promise<void> {
|
||||
export async function compileSite(production = false) {
|
||||
await genSiteEntry();
|
||||
if (production) {
|
||||
await build(getViteConfigForSiteProd());
|
||||
const config = mergeCustomViteConfig(getViteConfigForSiteProd());
|
||||
await build(config);
|
||||
} else {
|
||||
const server = await createServer(getViteConfigForSiteDev());
|
||||
const config = mergeCustomViteConfig(getViteConfigForSiteDev());
|
||||
const server = await createServer(config);
|
||||
await server.listen();
|
||||
|
||||
const { version } = require('vite/package.json');
|
||||
|
@ -92,7 +92,7 @@ function getHTMLMeta(vantConfig: any) {
|
||||
}
|
||||
|
||||
export function getViteConfigForSiteDev(): InlineConfig {
|
||||
setBuildTarget('package');
|
||||
setBuildTarget('site');
|
||||
|
||||
const vantConfig = getVantConfig();
|
||||
const siteConfig = getSiteConfig(vantConfig);
|
||||
|
Loading…
x
Reference in New Issue
Block a user