chore: init project

This commit is contained in:
vben
2024-05-19 21:20:42 +08:00
commit 399334ac57
630 changed files with 45623 additions and 0 deletions

View File

@@ -0,0 +1,104 @@
import type { UserConfig } from 'vite';
import { resolve } from 'node:path';
import { defineConfig, mergeConfig } from 'vite';
import { getApplicationConditionPlugins } from '../plugins';
import { getCommonConfig } from './common';
import type { DefineAppcationOptions } from '../typing';
function defineApplicationConfig(options: DefineAppcationOptions = {}) {
return defineConfig(async ({ command, mode }) => {
const { appcation = {}, vite = {} } = options;
const root = process.cwd();
const isBuild = command === 'build';
// const env = loadEnv(mode, root);
const plugins = await getApplicationConditionPlugins({
compress: false,
compressTypes: ['brotli', 'gzip'],
devtools: true,
extraAppConfig: true,
html: true,
i18n: true,
injectAppLoading: true,
isBuild,
mock: true,
mode,
turboConsole: false,
...appcation,
});
const applicationConfig: UserConfig = {
// },
build: {
rollupOptions: {
output: {
assetFileNames: '[ext]/[name]-[hash].[ext]',
chunkFileNames: 'js/[name]-[hash].mjs',
entryFileNames: 'jse/index-[name]-[hash].mjs',
},
},
target: 'es2015',
},
// },
esbuild: {
drop: isBuild
? [
// 'console',
'debugger',
]
: [],
legalComments: 'none',
},
plugins,
// css: {
// preprocessorOptions: {
// scss: {
// additionalData: `@import "@vben-core/design/global";`,
// },
resolve: {
alias: [
{
find: /@\//,
replacement: `${resolve(root, '.', 'src')}/`,
},
/**
* 确保大仓内的子包,如果通过源码方式引用,可以直接使用@别名
*/
// {
// find: '@',
// replacement: '@',
// customResolver(source, importer) {
// if (source[0] === '@') {
// const realPath = source.replace(
// /^@/,
// resolve(findUpPackageDir(importer), 'src'),
// );
// return findFileByExtension(realPath);
// }
// return null;
// },
// },
],
},
server: {
host: true,
warmup: {
// 预热文件
clientFiles: ['./index.html', './src/{views}/*'],
},
},
};
const mergedConfig = mergeConfig(
await getCommonConfig(),
applicationConfig,
);
return mergeConfig(mergedConfig, vite);
});
}
export { defineApplicationConfig };

View File

@@ -0,0 +1,13 @@
import type { UserConfig } from 'vite';
async function getCommonConfig(): Promise<UserConfig> {
return {
build: {
chunkSizeWarningLimit: 1000,
reportCompressedSize: false,
sourcemap: false,
},
};
}
export { getCommonConfig };

View File

@@ -0,0 +1,31 @@
import { join } from 'node:path';
import { fs } from '@vben/node-utils';
import { defineApplicationConfig } from './application';
import { defineLibraryConfig } from './library';
import type { DefineConfig } from '../typing';
export * from './application';
export * from './library';
function defineConfig(options: DefineConfig = {}) {
const { type = 'auto', ...defineOptions } = options;
let projectType = type;
// 根据包是否存在 index.html,自动判断类型
if (type === 'auto') {
const htmlPath = join(process.cwd(), 'index.html');
projectType = fs.existsSync(htmlPath) ? 'appcation' : 'library';
}
if (projectType === 'appcation') {
return defineApplicationConfig(defineOptions);
} else if (projectType === 'library') {
return defineLibraryConfig(defineOptions);
}
}
export { defineConfig };

View File

@@ -0,0 +1,51 @@
import type { UserConfig } from 'vite';
import { readPackageJSON } from '@vben/node-utils';
import { defineConfig, mergeConfig } from 'vite';
import { getLibraryConditionPlugins } from '../plugins';
import { getCommonConfig } from './common';
import type { DefineLibraryOptions } from '../typing';
function defineLibraryConfig(options: DefineLibraryOptions = {}) {
return defineConfig(async ({ command, mode }) => {
const root = process.cwd();
const { library = {}, vite = {} } = options;
const isBuild = command === 'build';
const plugins = await getLibraryConditionPlugins({
dts: false,
injectLibCss: true,
isBuild,
mode,
...library,
});
const { dependencies = {}, peerDependencies = {} } =
await readPackageJSON(root);
const external = [
...Object.keys(dependencies),
...Object.keys(peerDependencies),
];
const packageConfig: UserConfig = {
build: {
lib: {
entry: 'src/index.ts',
fileName: () => 'index.mjs',
formats: ['es'],
},
rollupOptions: {
external,
},
},
plugins,
};
const commonConfig = await getCommonConfig();
const mergedConfig = mergeConfig(commonConfig, packageConfig);
return mergeConfig(mergedConfig, vite);
});
}
export { defineLibraryConfig };