application.ts
3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import type { CSSOptions, UserConfig } from "vite";
import type { DefineApplicationOptions } from "../typing";
import path, { relative } from "node:path";
import { findMonorepoRoot } from "@venus/node-utils";
import { NodePackageImporter } from "sass";
import { defineConfig, loadEnv, mergeConfig } from "vite";
import { defaultImportmapOptions, getDefaultPwaOptions } from "../options";
import { loadApplicationPlugins } from "../plugins";
import { loadAndConvertEnv } from "../utils/env";
import { getCommonConfig } from "./common";
function defineApplicationConfig(userConfigPromise?: DefineApplicationOptions) {
return defineConfig(async (config) => {
const options = await userConfigPromise?.(config);
const { appTitle, base, port, ...envConfig } = await loadAndConvertEnv();
const { command, mode } = config;
const { application = {}, vite = {} } = options || {};
const root = process.cwd();
const isBuild = command === "build";
const env = loadEnv(mode, root);
const plugins = await loadApplicationPlugins({
archiver: true,
archiverPluginOptions: {},
compress: false,
compressTypes: [ "brotli", "gzip" ],
devtools: true,
env,
extraAppConfig: true,
html: true,
i18n: true,
importmapOptions: defaultImportmapOptions,
injectAppLoading: true,
injectMetadata: true,
isBuild,
license: true,
mode,
nitroMock: !isBuild,
nitroMockOptions: {},
print: !isBuild,
printInfoMap: {
"Vben Admin Docs": "https://doc.vben.pro",
},
pwa: true,
pwaOptions: getDefaultPwaOptions(appTitle),
vxeTableLazyImport: true,
...envConfig,
...application,
});
const { injectGlobalScss = true } = application;
const applicationConfig: UserConfig = {
base,
build: {
rollupOptions: {
output: {
assetFileNames: "[ext]/[name]-[hash].[ext]",
chunkFileNames: "js/[name]-[hash].js",
entryFileNames: "jse/index-[name]-[hash].js",
},
},
target: "es2015",
},
css: createCssOptions(injectGlobalScss),
esbuild: {
drop: isBuild
? [
// 'console',
"debugger",
]
: [],
legalComments: "none",
},
plugins,
server: {
host: true,
port,
warmup: {
// 预热文件
clientFiles: [
"./index.html",
"./src/bootstrap.ts",
"./src/{views,layouts,router,store,api,adapter}/*",
],
},
},
};
const mergedCommonConfig = mergeConfig(await getCommonConfig(), applicationConfig);
return mergeConfig(mergedCommonConfig, vite);
});
}
function createCssOptions(injectGlobalScss = true): CSSOptions {
const root = findMonorepoRoot();
return {
preprocessorOptions: injectGlobalScss
? {
scss: {
additionalData: (content: string, filepath: string) => {
const relativePath = relative(root, filepath);
// apps下的包注入全局样式
if (relativePath.startsWith(`apps${path.sep}`)) {
return `@use "@venus/styles/global" as *;\n${content}`;
}
return content;
},
// api: 'modern',
importers: [ new NodePackageImporter() ],
},
}
: {},
};
}
export { defineApplicationConfig };