mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
feat(@vant/cli): add external config for bundles (#9947)
This commit is contained in:
parent
cfcc84ff25
commit
9e67b81288
@ -8,7 +8,7 @@ import { installDependencies } from '../common/manager.js';
|
|||||||
import { compileSfc } from '../compiler/compile-sfc.js';
|
import { compileSfc } from '../compiler/compile-sfc.js';
|
||||||
import { compileStyle } from '../compiler/compile-style.js';
|
import { compileStyle } from '../compiler/compile-style.js';
|
||||||
import { compileScript } from '../compiler/compile-script.js';
|
import { compileScript } from '../compiler/compile-script.js';
|
||||||
import { compilePackage } from '../compiler/compile-package.js';
|
import { compileBundles } from '../compiler/compile-bundles.js';
|
||||||
import { genPackageEntry } from '../compiler/gen-package-entry.js';
|
import { genPackageEntry } from '../compiler/gen-package-entry.js';
|
||||||
import { genStyleDepsMap } from '../compiler/gen-style-deps-map.js';
|
import { genStyleDepsMap } from '../compiler/gen-style-deps-map.js';
|
||||||
import { genComponentStyle } from '../compiler/gen-component-style.js';
|
import { genComponentStyle } from '../compiler/gen-component-style.js';
|
||||||
@ -133,8 +133,7 @@ async function buildPackageStyleEntry() {
|
|||||||
|
|
||||||
async function buildBundledOutputs() {
|
async function buildBundledOutputs() {
|
||||||
setModuleEnv('esmodule');
|
setModuleEnv('esmodule');
|
||||||
await compilePackage(false);
|
await compileBundles();
|
||||||
await compilePackage(true);
|
|
||||||
genVeturConfig();
|
genVeturConfig();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
42
packages/vant-cli/src/compiler/compile-bundles.ts
Normal file
42
packages/vant-cli/src/compiler/compile-bundles.ts
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import { build } from 'vite';
|
||||||
|
import { getPackageJson } from '../common/constant.js';
|
||||||
|
import { mergeCustomViteConfig } from '../common/index.js';
|
||||||
|
import { getViteConfigForPackage } from '../config/vite.package.js';
|
||||||
|
|
||||||
|
export async function compileBundles() {
|
||||||
|
const dependencies = getPackageJson().dependencies || {};
|
||||||
|
const externals = Object.keys(dependencies);
|
||||||
|
|
||||||
|
const configs = [
|
||||||
|
// umd bundle
|
||||||
|
getViteConfigForPackage({
|
||||||
|
minify: false,
|
||||||
|
formats: ['umd'],
|
||||||
|
external: ['vue'],
|
||||||
|
}),
|
||||||
|
// umd bundle (minified)
|
||||||
|
getViteConfigForPackage({
|
||||||
|
minify: true,
|
||||||
|
formats: ['umd'],
|
||||||
|
external: ['vue'],
|
||||||
|
}),
|
||||||
|
// esm/cjs bundle
|
||||||
|
getViteConfigForPackage({
|
||||||
|
minify: false,
|
||||||
|
formats: ['es', 'cjs'],
|
||||||
|
external: ['vue', ...externals],
|
||||||
|
}),
|
||||||
|
// esm/cjs bundle (minified)
|
||||||
|
// vite will not minify es bundle
|
||||||
|
// see: https://github.com/vuejs/vue-next/issues/2860
|
||||||
|
getViteConfigForPackage({
|
||||||
|
minify: true,
|
||||||
|
formats: ['es', 'cjs'],
|
||||||
|
external: ['vue', ...externals],
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
|
||||||
|
await Promise.all(
|
||||||
|
configs.map((config) => build(mergeCustomViteConfig(config)))
|
||||||
|
);
|
||||||
|
}
|
@ -1,10 +0,0 @@
|
|||||||
import { build } from 'vite';
|
|
||||||
import { mergeCustomViteConfig } from '../common/index.js';
|
|
||||||
import { getViteConfigForPackage } from '../config/vite.package.js';
|
|
||||||
|
|
||||||
export async function compilePackage(
|
|
||||||
minify: boolean
|
|
||||||
): ReturnType<typeof build> {
|
|
||||||
const config = mergeCustomViteConfig(getViteConfigForPackage(minify));
|
|
||||||
return build(config);
|
|
||||||
}
|
|
@ -1,9 +1,17 @@
|
|||||||
import { join } from 'path';
|
import { join } from 'path';
|
||||||
import type { InlineConfig } from 'vite';
|
|
||||||
import { setBuildTarget } from '../common/index.js';
|
import { setBuildTarget } from '../common/index.js';
|
||||||
import { CWD, ES_DIR, getVantConfig, LIB_DIR } from '../common/constant.js';
|
import { CWD, ES_DIR, getVantConfig, LIB_DIR } from '../common/constant.js';
|
||||||
|
import type { InlineConfig, LibraryFormats } from 'vite';
|
||||||
|
|
||||||
export function getViteConfigForPackage(minify: boolean): InlineConfig {
|
export function getViteConfigForPackage({
|
||||||
|
minify,
|
||||||
|
formats,
|
||||||
|
external,
|
||||||
|
}: {
|
||||||
|
minify: boolean;
|
||||||
|
formats: LibraryFormats[];
|
||||||
|
external: string[];
|
||||||
|
}): InlineConfig {
|
||||||
setBuildTarget('package');
|
setBuildTarget('package');
|
||||||
|
|
||||||
const { name } = getVantConfig();
|
const { name } = getVantConfig();
|
||||||
@ -17,7 +25,7 @@ export function getViteConfigForPackage(minify: boolean): InlineConfig {
|
|||||||
lib: {
|
lib: {
|
||||||
name,
|
name,
|
||||||
entry: join(ES_DIR, 'index.js'),
|
entry: join(ES_DIR, 'index.js'),
|
||||||
formats: ['es', 'cjs', 'umd'],
|
formats,
|
||||||
fileName: (format: string) => {
|
fileName: (format: string) => {
|
||||||
const suffix = format === 'umd' ? '' : `.${format}`;
|
const suffix = format === 'umd' ? '' : `.${format}`;
|
||||||
return minify ? `${name}${suffix}.min.js` : `${name}${suffix}.js`;
|
return minify ? `${name}${suffix}.min.js` : `${name}${suffix}.js`;
|
||||||
@ -26,7 +34,7 @@ export function getViteConfigForPackage(minify: boolean): InlineConfig {
|
|||||||
// terser has better compression than esbuild
|
// terser has better compression than esbuild
|
||||||
minify: minify ? 'terser' : false,
|
minify: minify ? 'terser' : false,
|
||||||
rollupOptions: {
|
rollupOptions: {
|
||||||
external: ['vue'],
|
external,
|
||||||
output: {
|
output: {
|
||||||
dir: LIB_DIR,
|
dir: LIB_DIR,
|
||||||
exports: 'named',
|
exports: 'named',
|
||||||
|
Loading…
x
Reference in New Issue
Block a user