feat(vite): 优化开发插件加载逻辑与构建配置
This commit is contained in:
@@ -5,7 +5,7 @@ import { vitePluginFakeServer } from 'vite-plugin-fake-server';
|
|||||||
|
|
||||||
export function loadPlugin(_configEnv: ConfigEnv): PluginOption {
|
export function loadPlugin(_configEnv: ConfigEnv): PluginOption {
|
||||||
if (_configEnv.mode !== 'development') {
|
if (_configEnv.mode !== 'development') {
|
||||||
consola.info('fake server plugin disabled in test mode');
|
consola.info('fake server plugin is disabled in non-development mode.');
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
return vitePluginFakeServer({
|
return vitePluginFakeServer({
|
||||||
|
|||||||
@@ -1,6 +1,14 @@
|
|||||||
import type { PluginOption } from 'vite';
|
import consola from 'consola';
|
||||||
|
import { loadEnv, type ConfigEnv, type PluginOption } from 'vite';
|
||||||
import vueDevTools from 'vite-plugin-vue-devtools';
|
import vueDevTools from 'vite-plugin-vue-devtools';
|
||||||
|
|
||||||
// env.VITE_ENABLE_VUE_DEVTOOLS === 'true'
|
export function loadPlugin(configEnv: ConfigEnv): PluginOption {
|
||||||
|
const env = loadEnv(configEnv.mode, process.cwd());
|
||||||
|
|
||||||
export default [vueDevTools()] satisfies PluginOption;
|
if (env.VITE_ENABLE_VUE_DEVTOOLS === 'true') {
|
||||||
|
return [vueDevTools()];
|
||||||
|
} else {
|
||||||
|
consola.info('VITE_ENABLE_VUE_DEVTOOLS is not enabled.');
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import { createViteProxy } from 'utils4u/vite';
|
|||||||
import { defineConfig, loadEnv, type ConfigEnv, type PluginOption } from 'vite';
|
import { defineConfig, loadEnv, type ConfigEnv, type PluginOption } from 'vite';
|
||||||
import { optimizeDeps } from './vite.config.optimizeDeps';
|
import { optimizeDeps } from './vite.config.optimizeDeps';
|
||||||
|
|
||||||
|
type LoadPluginFunction = (configEnv: ConfigEnv) => PluginOption;
|
||||||
async function loadPlugins(configEnv: ConfigEnv): Promise<PluginOption[]> {
|
async function loadPlugins(configEnv: ConfigEnv): Promise<PluginOption[]> {
|
||||||
const plugins: PluginOption[] = [];
|
const plugins: PluginOption[] = [];
|
||||||
|
|
||||||
@@ -33,14 +34,13 @@ async function loadPlugins(configEnv: ConfigEnv): Promise<PluginOption[]> {
|
|||||||
const paddedName = pluginName.padEnd(maxNameLength, ' ');
|
const paddedName = pluginName.padEnd(maxNameLength, ' ');
|
||||||
const imported = await import(pathToFileURL(entry).href);
|
const imported = await import(pathToFileURL(entry).href);
|
||||||
|
|
||||||
const loadPlugin = imported.loadPlugin as (configEnv: ConfigEnv) => PluginOption;
|
const loadPlugin = imported.loadPlugin as LoadPluginFunction | undefined;
|
||||||
let plugin: PluginOption | undefined;
|
let plugin: PluginOption | undefined;
|
||||||
let loadMethod = '';
|
let loadMethod = '';
|
||||||
|
|
||||||
// 优先使用 loadPlugin 函数(接收 configEnv 参数)
|
// 优先使用 loadPlugin 函数(接收 configEnv 参数)
|
||||||
if (loadPlugin && typeof loadPlugin === 'function') {
|
if (loadPlugin && typeof loadPlugin === 'function') {
|
||||||
const result = loadPlugin(configEnv);
|
plugin = loadPlugin(configEnv);
|
||||||
plugin = result;
|
|
||||||
loadMethod = 'loadPlugin';
|
loadMethod = 'loadPlugin';
|
||||||
} else if (imported.default) {
|
} else if (imported.default) {
|
||||||
plugin = imported.default;
|
plugin = imported.default;
|
||||||
@@ -59,7 +59,7 @@ async function loadPlugins(configEnv: ConfigEnv): Promise<PluginOption[]> {
|
|||||||
plugins.push(...validPlugins);
|
plugins.push(...validPlugins);
|
||||||
consola.success(`${paddedName} → ${pluginCount} 个实例 (${loadMethod})`);
|
consola.success(`${paddedName} → ${pluginCount} 个实例 (${loadMethod})`);
|
||||||
} else {
|
} else {
|
||||||
consola.warn(`${paddedName} 返回了空数组或无效值`);
|
consola.info(`${paddedName} 返回了空数组或无效值`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -93,8 +93,9 @@ export default defineConfig(async (configEnv) => {
|
|||||||
|
|
||||||
output: {
|
output: {
|
||||||
// Keep hashed file names predictable across entry, chunk, and asset outputs.
|
// Keep hashed file names predictable across entry, chunk, and asset outputs.
|
||||||
// entryFileNames: '_entry/[name].[hash].js',
|
entryFileNames: 'entry/[name].[hash].js', // 默认: "[name].js"
|
||||||
// chunkFileNames: '_chunk/[name].[hash].js',
|
chunkFileNames: 'chunk/[name].[hash].js', // 默认: "[name]-[hash].js"
|
||||||
|
// assetFileNames:'', // 默认: "assets/[name]-[hash][extname]"
|
||||||
// https://cn.rollupjs.org/configuration-options/#output-assetfilenames
|
// https://cn.rollupjs.org/configuration-options/#output-assetfilenames
|
||||||
assetFileNames(chunkInfo: PreRenderedAsset) {
|
assetFileNames(chunkInfo: PreRenderedAsset) {
|
||||||
const names = chunkInfo.names;
|
const names = chunkInfo.names;
|
||||||
@@ -177,8 +178,8 @@ export default defineConfig(async (configEnv) => {
|
|||||||
},
|
},
|
||||||
define: {
|
define: {
|
||||||
__DEV__: JSON.stringify(!isBuild),
|
__DEV__: JSON.stringify(!isBuild),
|
||||||
// https://github.com/fi3ework/vite-plugin-checker/issues/569#issuecomment-3254311799
|
// // https://github.com/fi3ework/vite-plugin-checker/issues/569#issuecomment-3254311799
|
||||||
'process.env.NODE_ENV': JSON.stringify('production'),
|
// 'process.env.NODE_ENV': JSON.stringify('production'),
|
||||||
},
|
},
|
||||||
server: {
|
server: {
|
||||||
allowedHosts: ['.NWCT.DEV'],
|
allowedHosts: ['.NWCT.DEV'],
|
||||||
|
|||||||
Reference in New Issue
Block a user