chore: simplify Vite configuration
This commit is contained in:
@@ -37,9 +37,9 @@
|
|||||||
},
|
},
|
||||||
"homepage": "https://alibaba.github.io/page-agent/",
|
"homepage": "https://alibaba.github.io/page-agent/",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "vite build && MODE=umd vite build",
|
"build": "vite build && vite build --config vite.umd.config.js",
|
||||||
"serve": "npx serve dist/umd -p 5173",
|
"serve": "npx serve dist/umd -p 5173",
|
||||||
"dev:umd": "concurrently \"MODE=umd vite build --watch\" \"npm run serve\"",
|
"dev:umd": "concurrently \"vite build --config vite.umd.config.js --watch\" \"npm run serve\"",
|
||||||
"prepublishOnly": "node -e \"const fs=require('fs');['README.md','LICENSE'].forEach(f=>fs.copyFileSync('../../'+f,f))\"",
|
"prepublishOnly": "node -e \"const fs=require('fs');['README.md','LICENSE'].forEach(f=>fs.copyFileSync('../../'+f,f))\"",
|
||||||
"postpublish": "node -e \"['README.md','LICENSE'].forEach(f=>{try{require('fs').unlinkSync(f)}catch{}})\""
|
"postpublish": "node -e \"['README.md','LICENSE'].forEach(f=>{try{require('fs').unlinkSync(f)}catch{}})\""
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,7 +1,4 @@
|
|||||||
// @ts-check
|
// @ts-check
|
||||||
import chalk from 'chalk'
|
|
||||||
import 'dotenv/config'
|
|
||||||
import process from 'node:process'
|
|
||||||
import { dirname, resolve } from 'path'
|
import { dirname, resolve } from 'path'
|
||||||
import dts from 'unplugin-dts/vite'
|
import dts from 'unplugin-dts/vite'
|
||||||
import { fileURLToPath } from 'url'
|
import { fileURLToPath } from 'url'
|
||||||
@@ -10,11 +7,8 @@ import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js'
|
|||||||
|
|
||||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||||
|
|
||||||
// ============================================================================
|
|
||||||
// ES Module for NPM Package
|
// ES Module for NPM Package
|
||||||
// ============================================================================
|
export default defineConfig({
|
||||||
/** @type {import('vite').UserConfig} */
|
|
||||||
const esmConfig = {
|
|
||||||
clearScreen: false,
|
clearScreen: false,
|
||||||
plugins: [
|
plugins: [
|
||||||
dts({ tsconfigPath: './tsconfig.dts.json', bundleTypes: true }),
|
dts({ tsconfigPath: './tsconfig.dts.json', bundleTypes: true }),
|
||||||
@@ -47,58 +41,4 @@ const esmConfig = {
|
|||||||
define: {
|
define: {
|
||||||
'process.env.NODE_ENV': '"production"',
|
'process.env.NODE_ENV': '"production"',
|
||||||
},
|
},
|
||||||
}
|
})
|
||||||
|
|
||||||
// ============================================================================
|
|
||||||
// UMD Bundle for CDN
|
|
||||||
// - alias all local packages so that they can be build in
|
|
||||||
// - no external
|
|
||||||
// - no d.ts. dts does not work with monorepo aliasing
|
|
||||||
// ============================================================================
|
|
||||||
/** @type {import('vite').UserConfig} */
|
|
||||||
const umdConfig = {
|
|
||||||
plugins: [cssInjectedByJsPlugin({ relativeCSSInjection: true })],
|
|
||||||
publicDir: false,
|
|
||||||
esbuild: {
|
|
||||||
keepNames: true,
|
|
||||||
},
|
|
||||||
resolve: {
|
|
||||||
alias: {
|
|
||||||
'@page-agent/page-controller': resolve(__dirname, '../page-controller/src/PageController.ts'),
|
|
||||||
'@page-agent/ui': resolve(__dirname, '../ui/src/index.ts'),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
build: {
|
|
||||||
lib: {
|
|
||||||
entry: resolve(__dirname, 'src/umd.ts'),
|
|
||||||
name: 'PageAgent',
|
|
||||||
fileName: 'page-agent',
|
|
||||||
formats: ['umd'],
|
|
||||||
},
|
|
||||||
outDir: resolve(__dirname, 'dist', 'umd'),
|
|
||||||
rollupOptions: {
|
|
||||||
output: {
|
|
||||||
entryFileNames: 'page-agent.js', // 强制指定完整文件名
|
|
||||||
},
|
|
||||||
},
|
|
||||||
cssCodeSplit: true,
|
|
||||||
},
|
|
||||||
define: {
|
|
||||||
'process.env.NODE_ENV': '"production"',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============================================================================
|
|
||||||
|
|
||||||
const MODE = process.env.MODE
|
|
||||||
|
|
||||||
console.log(chalk.cyan(`📦 Build mode: ${chalk.bold(MODE || 'esm')}`))
|
|
||||||
|
|
||||||
let config
|
|
||||||
if (MODE === 'umd') {
|
|
||||||
config = umdConfig
|
|
||||||
} else {
|
|
||||||
config = esmConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
export default defineConfig(config)
|
|
||||||
|
|||||||
44
packages/page-agent/vite.umd.config.js
Normal file
44
packages/page-agent/vite.umd.config.js
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
// @ts-check
|
||||||
|
import { dirname, resolve } from 'path'
|
||||||
|
import { fileURLToPath } from 'url'
|
||||||
|
import { defineConfig } from 'vite'
|
||||||
|
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js'
|
||||||
|
|
||||||
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||||
|
|
||||||
|
// UMD Bundle for CDN
|
||||||
|
// - alias all local packages so that they can be build in
|
||||||
|
// - no external
|
||||||
|
// - no d.ts. dts does not work with monorepo aliasing
|
||||||
|
export default defineConfig({
|
||||||
|
plugins: [cssInjectedByJsPlugin({ relativeCSSInjection: true })],
|
||||||
|
publicDir: false,
|
||||||
|
esbuild: {
|
||||||
|
keepNames: true,
|
||||||
|
},
|
||||||
|
resolve: {
|
||||||
|
alias: {
|
||||||
|
'@page-agent/page-controller': resolve(__dirname, '../page-controller/src/PageController.ts'),
|
||||||
|
'@page-agent/ui': resolve(__dirname, '../ui/src/index.ts'),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
build: {
|
||||||
|
lib: {
|
||||||
|
entry: resolve(__dirname, 'src/umd.ts'),
|
||||||
|
name: 'PageAgent',
|
||||||
|
fileName: 'page-agent',
|
||||||
|
formats: ['umd'],
|
||||||
|
},
|
||||||
|
outDir: resolve(__dirname, 'dist', 'umd'),
|
||||||
|
rollupOptions: {
|
||||||
|
output: {
|
||||||
|
// force use .js as extension
|
||||||
|
entryFileNames: 'page-agent.js',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
cssCodeSplit: true,
|
||||||
|
},
|
||||||
|
define: {
|
||||||
|
'process.env.NODE_ENV': '"production"',
|
||||||
|
},
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user