From b7adc8c19911809f68d2554e0c96bb08707179b2 Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Sat, 4 Sep 2021 17:11:47 +0800 Subject: [PATCH] feat(@vant/cli): using vite to build packages --- .../vant-cli/src/compiler/compile-package.ts | 18 ++------ .../src/config/vite.config.package.ts | 38 ++++++++++++++++ .../vant-cli/src/config/webpack.package.ts | 43 ------------------- 3 files changed, 42 insertions(+), 57 deletions(-) create mode 100644 packages/vant-cli/src/config/vite.config.package.ts delete mode 100644 packages/vant-cli/src/config/webpack.package.ts diff --git a/packages/vant-cli/src/compiler/compile-package.ts b/packages/vant-cli/src/compiler/compile-package.ts index 132e8784c..74a69436d 100644 --- a/packages/vant-cli/src/compiler/compile-package.ts +++ b/packages/vant-cli/src/compiler/compile-package.ts @@ -1,16 +1,6 @@ -import webpack from 'webpack'; -import { getPackageConfig } from '../config/webpack.package'; +import { build } from 'vite'; +import { getViteConfigForPackage } from '../config/vite.config.package'; -export async function compilePackage(isMinify: boolean) { - return new Promise((resolve, reject) => { - const config = getPackageConfig(isMinify); - - webpack(config, (err, stats) => { - if (err || (stats?.hasErrors())) { - reject(err || stats?.toString()); - } else { - resolve(); - } - }); - }); +export async function compilePackage(minify: boolean) { + return build(getViteConfigForPackage(minify)); } diff --git a/packages/vant-cli/src/config/vite.config.package.ts b/packages/vant-cli/src/config/vite.config.package.ts new file mode 100644 index 000000000..45271036d --- /dev/null +++ b/packages/vant-cli/src/config/vite.config.package.ts @@ -0,0 +1,38 @@ +import { join } from 'path'; +import type { InlineConfig } from 'vite'; +import { setBuildTarget } from '../common'; +import { CWD, ES_DIR, getVantConfig, LIB_DIR } from '../common/constant'; + +export function getViteConfigForPackage(minify: boolean): InlineConfig { + setBuildTarget('package'); + + const { name } = getVantConfig(); + + return { + root: CWD, + + logLevel: 'silent', + + build: { + lib: { + name, + entry: join(ES_DIR, 'index.js'), + fileName: (format: string) => { + const suffix = format === 'umd' ? '' : `.${format}`; + return minify ? `${name}${suffix}.min.js` : `${name}${suffix}.js`; + }, + }, + minify, + rollupOptions: { + external: ['vue'], + output: { + dir: LIB_DIR, + exports: 'named', + globals: { + vue: 'Vue', + }, + }, + }, + }, + }; +} diff --git a/packages/vant-cli/src/config/webpack.package.ts b/packages/vant-cli/src/config/webpack.package.ts deleted file mode 100644 index 93229fe77..000000000 --- a/packages/vant-cli/src/config/webpack.package.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { merge } from 'webpack-merge'; -import { join } from 'path'; -import { baseConfig } from './webpack.base'; -import { WebpackConfig } from '../common/types'; -import { getVantConfig, getWebpackConfig, setBuildTarget } from '../common'; -import { LIB_DIR, ES_DIR } from '../common/constant'; - -export function getPackageConfig(isMinify: boolean): WebpackConfig { - const { name } = getVantConfig(); - - setBuildTarget('package'); - - return getWebpackConfig( - merge(baseConfig as any, { - mode: 'production', - entry: { - [name]: join(ES_DIR, 'index.js'), - }, - stats: 'none', - output: { - path: LIB_DIR, - library: name, - libraryTarget: 'umd', - filename: isMinify ? '[name].min.js' : '[name].js', - umdNamedDefine: true, - // https://github.com/webpack/webpack/issues/6522 - globalObject: "typeof self !== 'undefined' ? self : this", - }, - externals: { - vue: { - root: 'Vue', - commonjs: 'vue', - commonjs2: 'vue', - amd: 'vue', - }, - }, - performance: false, - optimization: { - minimize: isMinify, - }, - }) - ); -}