feat(@vant/cli): using vite to build site

This commit is contained in:
chenjiahan 2021-09-07 11:59:12 +08:00
parent 6e5f215140
commit 2dd7e853d6
10 changed files with 45 additions and 17 deletions

2
.gitignore vendored
View File

@ -16,5 +16,5 @@ es
lib lib
dist dist
vetur vetur
./site **/site
changelog.generated.md changelog.generated.md

View File

@ -15,7 +15,7 @@
"build": "vant-cli build", "build": "vant-cli build",
"release": "vant-cli release", "release": "vant-cli release",
"test:coverage": "open test/coverage/index.html", "test:coverage": "open test/coverage/index.html",
"build-site": "vant-cli build-site && gh-pages -d site" "build-site": "vant-cli build-site && gh-pages -d site-dist"
}, },
"author": "", "author": "",
"license": "MIT", "license": "MIT",

View File

@ -16,7 +16,7 @@
"prepare": "husky install", "prepare": "husky install",
"release": "vant-cli release", "release": "vant-cli release",
"test:coverage": "open test/coverage/index.html", "test:coverage": "open test/coverage/index.html",
"build-site": "vant-cli build-site && gh-pages -d site" "build-site": "vant-cli build-site && gh-pages -d site-dist"
}, },
"author": "", "author": "",
"license": "MIT", "license": "MIT",

View File

@ -5,6 +5,7 @@
### 不兼容更新 ### 不兼容更新
- 使用 vite 代替 webpack 进行构建 - 使用 vite 代替 webpack 进行构建
- 站点构建产物的目录由 `site` 调整为 `site-dist`
- 不再支持 webpack.config.js 配置文件 - 不再支持 webpack.config.js 配置文件
- 不再内置 babel-plugin-import 插件 - 不再内置 babel-plugin-import 插件

View File

@ -108,9 +108,9 @@ export default {
[] []
); );
const current = navItems.find((item) => { const current = navItems.find(
return item.path === this.$route.meta.name; (item) => item.path === this.$route.meta.name
}); );
if (current && current.title) { if (current && current.title) {
title = current.title + ' - ' + title; title = current.title + ' - ' + title;

View File

@ -25,7 +25,7 @@ export const ES_DIR = join(ROOT, 'es');
export const LIB_DIR = join(ROOT, 'lib'); export const LIB_DIR = join(ROOT, 'lib');
export const DOCS_DIR = join(ROOT, 'docs'); export const DOCS_DIR = join(ROOT, 'docs');
export const VETUR_DIR = join(ROOT, 'vetur'); export const VETUR_DIR = join(ROOT, 'vetur');
export const SITE_DIST_DIR = join(ROOT, 'site'); export const SITE_DIST_DIR = join(ROOT, 'site-dist');
export const VANT_CONFIG_FILE = join(ROOT, 'vant.config.js'); export const VANT_CONFIG_FILE = join(ROOT, 'vant.config.js');
export const PACKAGE_JSON_FILE = join(ROOT, 'package.json'); export const PACKAGE_JSON_FILE = join(ROOT, 'package.json');
export const ROOT_POSTCSS_CONFIG_FILE = join(ROOT, 'postcss.config.js'); export const ROOT_POSTCSS_CONFIG_FILE = join(ROOT, 'postcss.config.js');
@ -34,6 +34,7 @@ export const CACHE_DIR = join(ROOT, 'node_modules/.cache');
// Relative paths // Relative paths
export const DIST_DIR = join(__dirname, '../../dist'); export const DIST_DIR = join(__dirname, '../../dist');
export const CONFIG_DIR = join(__dirname, '../config'); export const CONFIG_DIR = join(__dirname, '../config');
export const SITE_SRC_DIR = join(__dirname, '../../site');
// Dist files // Dist files
export const PACKAGE_ENTRY_FILE = join(DIST_DIR, 'package-entry.js'); export const PACKAGE_ENTRY_FILE = join(DIST_DIR, 'package-entry.js');

View File

@ -1,13 +1,14 @@
import { createServer } from 'vite'; import { createServer, build } from 'vite';
import { getViteConfigForSiteDev } from '../config/vite.site'; import {
getViteConfigForSiteDev,
getViteConfigForSiteProd,
} from '../config/vite.site';
import { genSiteEntry } from './vant-cli-site-plugin'; import { genSiteEntry } from './vant-cli-site-plugin';
async function build() {}
export async function compileSite(production = false) { export async function compileSite(production = false) {
await genSiteEntry(); await genSiteEntry();
if (production) { if (production) {
await build(); await build(getViteConfigForSiteProd());
} else { } else {
const server = await createServer(getViteConfigForSiteDev()); const server = await createServer(getViteConfigForSiteDev());
await server.listen(); await server.listen();

View File

@ -6,8 +6,10 @@ import vitePluginVue from '@vitejs/plugin-vue';
import vitePluginJsx from '@vitejs/plugin-vue-jsx'; import vitePluginJsx from '@vitejs/plugin-vue-jsx';
import { setBuildTarget, getVantConfig } from '../common'; import { setBuildTarget, getVantConfig } from '../common';
import { import {
SITE_DESKTOP_SHARED_FILE, SITE_DIST_DIR,
SITE_MOBILE_SHARED_FILE, SITE_MOBILE_SHARED_FILE,
SITE_DESKTOP_SHARED_FILE,
SITE_SRC_DIR,
} from '../common/constant'; } from '../common/constant';
import { injectHtml } from 'vite-plugin-html'; import { injectHtml } from 'vite-plugin-html';
import type { InlineConfig } from 'vite'; import type { InlineConfig } from 'vite';
@ -67,7 +69,7 @@ export function getViteConfigForSiteDev(): InlineConfig {
const baiduAnalytics = get(vantConfig, 'site.baiduAnalytics'); const baiduAnalytics = get(vantConfig, 'site.baiduAnalytics');
return { return {
root: join(__dirname, '../../site'), root: SITE_SRC_DIR,
plugins: [ plugins: [
vitePluginVue({ vitePluginVue({
@ -112,3 +114,26 @@ export function getViteConfigForSiteDev(): InlineConfig {
}, },
}; };
} }
export function getViteConfigForSiteProd(): InlineConfig {
const devConfig = getViteConfigForSiteDev();
const vantConfig = getVantConfig();
const outDir = get(vantConfig, 'build.site.outputDir', SITE_DIST_DIR);
const publicPath = get(vantConfig, 'build.site.publicPath', '/');
return {
...devConfig,
base: publicPath,
build: {
outDir,
brotliSize: false,
emptyOutDir: true,
rollupOptions: {
input: {
main: join(SITE_SRC_DIR, 'index.html'),
nested: join(SITE_SRC_DIR, 'mobile.html'),
},
},
},
};
}

View File

@ -21,7 +21,7 @@
"build": "vant-cli build", "build": "vant-cli build",
"build:site": "vant-cli build-site", "build:site": "vant-cli build-site",
"release": "vant-cli release --tag next", "release": "vant-cli release --tag next",
"release:site": "yarn build:site && gh-pages -d site --add --dest v3", "release:site": "yarn build:site && gh-pages -d site-dist --add --dest v3",
"test:watch": "vant-cli test --watch", "test:watch": "vant-cli test --watch",
"test:coverage": "open test/coverage/index.html" "test:coverage": "open test/coverage/index.html"
}, },

View File

@ -73,7 +73,7 @@ const fileList5 = ref([
}, },
]); ]);
const statusFileList = [ const statusFileList = ref([
{ {
url: 'https://img.yzcdn.cn/vant/leaf.jpg', url: 'https://img.yzcdn.cn/vant/leaf.jpg',
status: 'uploading', status: 'uploading',
@ -84,7 +84,7 @@ const statusFileList = [
status: 'failed', status: 'failed',
message: t('failed'), message: t('failed'),
}, },
]; ]);
const previewCoverFiles = ref([ const previewCoverFiles = ref([
{ {