@allkit/vite-config 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/core/build.d.ts +1 -0
- package/dist/cjs/core/build.js +45 -0
- package/dist/cjs/core/index.d.ts +6 -0
- package/dist/cjs/core/index.js +84 -0
- package/dist/cjs/core/lib-plugins.d.ts +3 -0
- package/dist/cjs/core/lib-plugins.js +23 -0
- package/dist/cjs/core/plugins.d.ts +3 -0
- package/dist/cjs/core/plugins.js +54 -0
- package/dist/cjs/core/server.d.ts +2 -0
- package/dist/cjs/core/server.js +35 -0
- package/dist/cjs/index.d.ts +3 -0
- package/dist/cjs/index.js +19 -0
- package/dist/cjs/logger.d.ts +20 -0
- package/dist/cjs/logger.js +39 -0
- package/dist/cjs/plugins/chii.d.ts +3 -0
- package/dist/cjs/plugins/chii.js +31 -0
- package/dist/cjs/plugins/env-runtime/index.d.ts +12 -0
- package/dist/cjs/plugins/env-runtime/index.js +90 -0
- package/dist/cjs/plugins/env-runtime/types.d.ts +15 -0
- package/dist/cjs/plugins/env-runtime/types.js +2 -0
- package/dist/cjs/plugins/env.d.ts +3 -0
- package/dist/cjs/plugins/env.js +50 -0
- package/dist/cjs/plugins/http2-proxy/index.d.ts +2 -0
- package/dist/cjs/plugins/http2-proxy/index.js +56 -0
- package/dist/cjs/plugins/index.d.ts +2 -0
- package/dist/cjs/plugins/index.js +18 -0
- package/dist/esm/core/build.d.ts +1 -0
- package/dist/esm/core/build.js +38 -0
- package/dist/esm/core/index.d.ts +6 -0
- package/dist/esm/core/index.js +76 -0
- package/dist/esm/core/lib-plugins.d.ts +3 -0
- package/dist/esm/core/lib-plugins.js +16 -0
- package/dist/esm/core/plugins.d.ts +3 -0
- package/dist/esm/core/plugins.js +47 -0
- package/dist/esm/core/server.d.ts +2 -0
- package/dist/esm/core/server.js +28 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +3 -0
- package/dist/esm/logger.d.ts +20 -0
- package/dist/esm/logger.js +33 -0
- package/dist/esm/plugins/chii.d.ts +3 -0
- package/dist/esm/plugins/chii.js +28 -0
- package/dist/esm/plugins/env-runtime/index.d.ts +12 -0
- package/dist/esm/plugins/env-runtime/index.js +85 -0
- package/dist/esm/plugins/env-runtime/types.d.ts +15 -0
- package/dist/esm/plugins/env-runtime/types.js +1 -0
- package/dist/esm/plugins/env.d.ts +3 -0
- package/dist/esm/plugins/env.js +47 -0
- package/dist/esm/plugins/http2-proxy/index.d.ts +2 -0
- package/dist/esm/plugins/http2-proxy/index.js +49 -0
- package/dist/esm/plugins/index.d.ts +2 -0
- package/dist/esm/plugins/index.js +2 -0
- package/package.json +48 -0
- package/tsconfig.cjs.json +24 -0
- package/tsconfig.esm.json +24 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import process from 'node:process';
|
|
2
|
+
import { resolve } from 'path';
|
|
3
|
+
export const useBuild = () => {
|
|
4
|
+
const option = {
|
|
5
|
+
cssTarget: 'chrome61',
|
|
6
|
+
chunkSizeWarningLimit: 2000,
|
|
7
|
+
emptyOutDir: true,
|
|
8
|
+
outDir: 'dist',
|
|
9
|
+
rollupOptions: {
|
|
10
|
+
input: {
|
|
11
|
+
main: resolve(process.cwd(), 'index.html'),
|
|
12
|
+
},
|
|
13
|
+
output: {
|
|
14
|
+
manualChunks: (filePath) => {
|
|
15
|
+
if (filePath.includes('node_modules/@vue') ||
|
|
16
|
+
filePath.includes('node_modules/vue-router') ||
|
|
17
|
+
filePath.includes('node_modules/pinia')) {
|
|
18
|
+
// vue全家桶放到一个包
|
|
19
|
+
return 'vue-family';
|
|
20
|
+
}
|
|
21
|
+
if (filePath.includes('node_modules/vant') || filePath.includes('node_modules/@vant')) {
|
|
22
|
+
return 'vant';
|
|
23
|
+
}
|
|
24
|
+
if (filePath.includes('node_modules/element-plus') ||
|
|
25
|
+
filePath.includes('node_modules/@element-plus')) {
|
|
26
|
+
return 'element-plus';
|
|
27
|
+
}
|
|
28
|
+
if (filePath.includes('node_modules/chart') ||
|
|
29
|
+
filePath.includes('node_modules/vue-chartjs') ||
|
|
30
|
+
filePath.includes('node_modules/echarts')) {
|
|
31
|
+
return 'chart-js';
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
};
|
|
37
|
+
return option;
|
|
38
|
+
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import path from 'path';
|
|
2
|
+
import process from 'process';
|
|
3
|
+
import { loadEnv, mergeConfig } from 'vite';
|
|
4
|
+
import { viteLog } from '../logger.js';
|
|
5
|
+
import { useBuild } from './build.js';
|
|
6
|
+
import { useLibPlugins } from './lib-plugins.js';
|
|
7
|
+
import { usePlugins } from './plugins.js';
|
|
8
|
+
import { useDevServer } from './server.js';
|
|
9
|
+
export const viteConfig = (config = {}) => {
|
|
10
|
+
const mode = config.mode || process.env.NODE_ENV;
|
|
11
|
+
const env = loadEnv(mode || '', config.envDir || './');
|
|
12
|
+
viteLog.success('**************************************');
|
|
13
|
+
viteLog.info('--mode =', mode);
|
|
14
|
+
viteLog.info('--envDir =', config.envDir);
|
|
15
|
+
viteLog.success('**************************************');
|
|
16
|
+
const _commonConfig = {
|
|
17
|
+
base: config.base,
|
|
18
|
+
resolve: {
|
|
19
|
+
extensions: ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'],
|
|
20
|
+
alias: {
|
|
21
|
+
'@': path.resolve(process.cwd(), './src'),
|
|
22
|
+
'~/': `${path.resolve(process.cwd(), './src')}/`,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
optimizeDeps: {
|
|
26
|
+
include: ['vue', 'vue-router'],
|
|
27
|
+
},
|
|
28
|
+
build: useBuild(),
|
|
29
|
+
plugins: usePlugins(env),
|
|
30
|
+
server: useDevServer(env),
|
|
31
|
+
};
|
|
32
|
+
const _config = mergeConfig(_commonConfig, config);
|
|
33
|
+
return _config;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* 库模式
|
|
37
|
+
*/
|
|
38
|
+
export const viteLibConfig = (config = {}) => {
|
|
39
|
+
const mode = config.mode || process.env.NODE_ENV;
|
|
40
|
+
viteLog.success('**************************************');
|
|
41
|
+
viteLog.info('--mode =', mode);
|
|
42
|
+
viteLog.info('--envDir =', config.envDir);
|
|
43
|
+
viteLog.success('**************************************');
|
|
44
|
+
const _commonConfig = {
|
|
45
|
+
base: config.base,
|
|
46
|
+
resolve: {
|
|
47
|
+
extensions: ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'],
|
|
48
|
+
alias: {
|
|
49
|
+
'@': path.resolve(process.cwd(), './src'),
|
|
50
|
+
'~/': `${path.resolve(process.cwd(), './src')}/`,
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
optimizeDeps: {
|
|
54
|
+
include: ['pinia', 'vue', 'vue-router'],
|
|
55
|
+
},
|
|
56
|
+
plugins: useLibPlugins(),
|
|
57
|
+
build: {
|
|
58
|
+
target: 'es2015',
|
|
59
|
+
cssTarget: 'chrome61',
|
|
60
|
+
outDir: 'dist',
|
|
61
|
+
rollupOptions: {
|
|
62
|
+
// // 确保外部化处理那些你不想打包进库的依赖
|
|
63
|
+
external: ['vue', 'vue-router'],
|
|
64
|
+
output: {
|
|
65
|
+
exports: 'named',
|
|
66
|
+
// 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
|
|
67
|
+
globals: {
|
|
68
|
+
vue: 'Vue',
|
|
69
|
+
},
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
const _config = mergeConfig(_commonConfig, config);
|
|
75
|
+
return _config;
|
|
76
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import vue from '@vitejs/plugin-vue';
|
|
2
|
+
import vueJsx from '@vitejs/plugin-vue-jsx';
|
|
3
|
+
import dts from 'vite-plugin-dts';
|
|
4
|
+
/** 库模式 */
|
|
5
|
+
export const useLibPlugins = () => {
|
|
6
|
+
const plugins = [
|
|
7
|
+
vue(),
|
|
8
|
+
vueJsx(),
|
|
9
|
+
dts({
|
|
10
|
+
exclude: ['node_modules', 'src/**/__test__', 'src/**/__tests__'],
|
|
11
|
+
outDir: 'dist',
|
|
12
|
+
insertTypesEntry: true,
|
|
13
|
+
}),
|
|
14
|
+
];
|
|
15
|
+
return plugins;
|
|
16
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import process from 'node:process';
|
|
2
|
+
import vue from '@vitejs/plugin-vue';
|
|
3
|
+
import vueJsx from '@vitejs/plugin-vue-jsx';
|
|
4
|
+
import legacy from '@vitejs/plugin-legacy';
|
|
5
|
+
import { visualizer } from 'rollup-plugin-visualizer';
|
|
6
|
+
import viteCompression from 'vite-plugin-compression';
|
|
7
|
+
import viteLog from '../logger.js';
|
|
8
|
+
/** 库模式 */
|
|
9
|
+
export const usePlugins = (env) => {
|
|
10
|
+
const isProd = process.env.NODE_ENV === 'production';
|
|
11
|
+
const plugins = [
|
|
12
|
+
vue(),
|
|
13
|
+
vueJsx(),
|
|
14
|
+
legacy({
|
|
15
|
+
targets: ['defaults', 'not IE 11', 'Chrome 63', 'iOS >= 12', 'android >= 6'],
|
|
16
|
+
polyfills: [
|
|
17
|
+
'es.symbol',
|
|
18
|
+
'es.array.flat',
|
|
19
|
+
'web.dom-collections.for-each',
|
|
20
|
+
'es.promise.finally',
|
|
21
|
+
'es.string.replace-all',
|
|
22
|
+
],
|
|
23
|
+
modernPolyfills: [
|
|
24
|
+
'es.promise.finally',
|
|
25
|
+
'es.global-this',
|
|
26
|
+
'es.string.match-all',
|
|
27
|
+
'es.string.replace-all',
|
|
28
|
+
],
|
|
29
|
+
}),
|
|
30
|
+
];
|
|
31
|
+
//包分析的时候,才使用这个包
|
|
32
|
+
if (env.VITE_PLUGIN_ANALYZER === 'true') {
|
|
33
|
+
viteLog.warn('打包分析', '-已开启');
|
|
34
|
+
plugins.push(visualizer({
|
|
35
|
+
emitFile: false,
|
|
36
|
+
filename: 'stats.html',
|
|
37
|
+
open: true,
|
|
38
|
+
}));
|
|
39
|
+
}
|
|
40
|
+
if (isProd && Boolean(env.VITE_PLUGIN_COMPRESSION === 'true')) {
|
|
41
|
+
viteLog.warn('代码压缩', '-已开启');
|
|
42
|
+
plugins.push(viteCompression({
|
|
43
|
+
threshold: 1024,
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
return plugins;
|
|
47
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import process from 'node:process';
|
|
2
|
+
export const useDevServer = (env) => {
|
|
3
|
+
const isProd = process.env.NODE_ENV === 'production';
|
|
4
|
+
if (isProd)
|
|
5
|
+
return undefined;
|
|
6
|
+
const option = {
|
|
7
|
+
host: '0.0.0.0',
|
|
8
|
+
port: env.VITE_PORT ? Number(env.VITE_PORT) : 3000,
|
|
9
|
+
proxy: {},
|
|
10
|
+
};
|
|
11
|
+
const proxyConf = env.VITE_PROXY ? JSON.parse(env.VITE_PROXY) : {};
|
|
12
|
+
Object.keys(proxyConf).forEach((key) => {
|
|
13
|
+
const reg = new RegExp('^\\' + key);
|
|
14
|
+
const proxy = {
|
|
15
|
+
target: proxyConf[key].target,
|
|
16
|
+
changeOrigin: true,
|
|
17
|
+
ws: true,
|
|
18
|
+
rewrite: (path) => {
|
|
19
|
+
const _path = path.replace(reg, proxyConf[key].rewrite);
|
|
20
|
+
return _path;
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
if (option && option.proxy) {
|
|
24
|
+
option.proxy[key] = proxy;
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
return option;
|
|
28
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
declare const viteLog: {
|
|
2
|
+
/**
|
|
3
|
+
* 基本日志
|
|
4
|
+
*/
|
|
5
|
+
info(...arg: any[]): void;
|
|
6
|
+
/**
|
|
7
|
+
* 成功日志
|
|
8
|
+
*/
|
|
9
|
+
success(...arg: any[]): void;
|
|
10
|
+
/**
|
|
11
|
+
* 警告日志
|
|
12
|
+
*/
|
|
13
|
+
warn(...arg: any[]): void;
|
|
14
|
+
/**
|
|
15
|
+
* 错误日志
|
|
16
|
+
*/
|
|
17
|
+
error(...arg: any[]): void;
|
|
18
|
+
};
|
|
19
|
+
export { viteLog };
|
|
20
|
+
export default viteLog;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import colors from 'picocolors';
|
|
2
|
+
const viteLog = {
|
|
3
|
+
/**
|
|
4
|
+
* 基本日志
|
|
5
|
+
*/
|
|
6
|
+
info(...arg) {
|
|
7
|
+
const [tag, ...rest] = arg || [];
|
|
8
|
+
console.log(colors.green('[vite-config]'), colors.blue(tag), ...rest);
|
|
9
|
+
},
|
|
10
|
+
/**
|
|
11
|
+
* 成功日志
|
|
12
|
+
*/
|
|
13
|
+
success(...arg) {
|
|
14
|
+
const [tag, ...rest] = arg || [];
|
|
15
|
+
console.log(colors.green('[vite-config]'), colors.green(tag), ...rest);
|
|
16
|
+
},
|
|
17
|
+
/**
|
|
18
|
+
* 警告日志
|
|
19
|
+
*/
|
|
20
|
+
warn(...arg) {
|
|
21
|
+
const [tag, ...rest] = arg || [];
|
|
22
|
+
console.log(colors.green('[vite-config]'), colors.yellow(tag), ...rest);
|
|
23
|
+
},
|
|
24
|
+
/**
|
|
25
|
+
* 错误日志
|
|
26
|
+
*/
|
|
27
|
+
error(...arg) {
|
|
28
|
+
const [tag, ...rest] = arg || [];
|
|
29
|
+
console.log(colors.green('[vite-config]'), colors.yellow(tag), ...rest);
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
export { viteLog };
|
|
33
|
+
export default viteLog;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
export function chiiPlugin() {
|
|
3
|
+
const chiiProcess = spawn('npx', ['chii', 'start', '-p', '8080']);
|
|
4
|
+
chiiProcess.stdout.on('data', (data) => {
|
|
5
|
+
console.log(`chii stdout: ${data}`);
|
|
6
|
+
});
|
|
7
|
+
chiiProcess.stderr.on('data', (data) => {
|
|
8
|
+
console.error(`chii stderr: ${data}`);
|
|
9
|
+
});
|
|
10
|
+
chiiProcess.on('close', (code) => {
|
|
11
|
+
console.log(`chii process exited with code ${code}`);
|
|
12
|
+
});
|
|
13
|
+
return {
|
|
14
|
+
name: 'vite:kye-chill',
|
|
15
|
+
enforce: 'post',
|
|
16
|
+
transformIndexHtml() {
|
|
17
|
+
return [
|
|
18
|
+
{
|
|
19
|
+
tag: 'script',
|
|
20
|
+
attrs: {
|
|
21
|
+
src: '//host-machine-ip:8080/target.js',
|
|
22
|
+
},
|
|
23
|
+
injectTo: 'body',
|
|
24
|
+
},
|
|
25
|
+
];
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type PluginOption } from 'vite';
|
|
2
|
+
import type { ViteEnvRuntimeOptions } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* 默认vite的环境规则解析
|
|
5
|
+
*/
|
|
6
|
+
export declare const viteRuleResolve: () => "production" | "development";
|
|
7
|
+
/**
|
|
8
|
+
* env生成环境运行时
|
|
9
|
+
* @param options - 配置
|
|
10
|
+
*/
|
|
11
|
+
export declare function envRuntimePlugin(options?: ViteEnvRuntimeOptions): PluginOption;
|
|
12
|
+
export type { PluginOption };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { loadEnv } from 'vite';
|
|
2
|
+
const defaultEnvList = [
|
|
3
|
+
'import.meta.env.MODE',
|
|
4
|
+
'import.meta.env.BASE_URL',
|
|
5
|
+
'import.meta.env.PROD',
|
|
6
|
+
'import.meta.env.DEV',
|
|
7
|
+
'import.meta.env.SSR',
|
|
8
|
+
];
|
|
9
|
+
/**
|
|
10
|
+
* 默认vite的环境规则解析
|
|
11
|
+
*/
|
|
12
|
+
export const viteRuleResolve = () => {
|
|
13
|
+
if (['feiyanyun.com'].includes(window.location.host)) {
|
|
14
|
+
return 'production';
|
|
15
|
+
}
|
|
16
|
+
return 'development';
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* env生成环境运行时
|
|
20
|
+
* @param options - 配置
|
|
21
|
+
*/
|
|
22
|
+
export function envRuntimePlugin(options) {
|
|
23
|
+
const virtualModuleId = '\0virtual:env-runtime-plugin';
|
|
24
|
+
let { excludes = [], ruleResolve = viteRuleResolve } = options || {};
|
|
25
|
+
excludes = [...defaultEnvList, ...excludes];
|
|
26
|
+
const envMap = {};
|
|
27
|
+
let configEnv = {};
|
|
28
|
+
return {
|
|
29
|
+
name: 'vite:env-runtime-plugin',
|
|
30
|
+
enforce: 'pre',
|
|
31
|
+
config(config, ConfigEnv) {
|
|
32
|
+
configEnv = ConfigEnv;
|
|
33
|
+
return config;
|
|
34
|
+
},
|
|
35
|
+
buildStart() {
|
|
36
|
+
const envDev = loadEnv('development', './env');
|
|
37
|
+
const envProd = loadEnv('production', './env');
|
|
38
|
+
envMap['development'] = envDev;
|
|
39
|
+
envMap['production'] = envProd;
|
|
40
|
+
},
|
|
41
|
+
resolveId(id) {
|
|
42
|
+
if (id === virtualModuleId) {
|
|
43
|
+
return id;
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
load(id) {
|
|
47
|
+
if (id === virtualModuleId) {
|
|
48
|
+
return `
|
|
49
|
+
let resolveRule = ${ruleResolve}\nlet runtimeMap = ${JSON.stringify(envMap)};\n
|
|
50
|
+
let mode = ${configEnv.command === 'serve' ? JSON.stringify(configEnv.mode) : 'resolveRule()'}
|
|
51
|
+
export default runtimeMap[mode]`;
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
transform(code) {
|
|
55
|
+
let isMatch = false;
|
|
56
|
+
code = code.replace(/import\.meta\.env\.(\w+)/g, (_, $2) => {
|
|
57
|
+
if (excludes.includes(`import.meta.env.${$2}`)) {
|
|
58
|
+
return `import.meta.env.${$2}`;
|
|
59
|
+
}
|
|
60
|
+
isMatch = true;
|
|
61
|
+
return `viteImportEnvMap['${$2}']`;
|
|
62
|
+
});
|
|
63
|
+
if (isMatch) {
|
|
64
|
+
const newImport = `import viteImportEnvMap from "${virtualModuleId}";`;
|
|
65
|
+
// 检查是否已有该import
|
|
66
|
+
if (code.includes(newImport)) {
|
|
67
|
+
return code;
|
|
68
|
+
}
|
|
69
|
+
// 正则表达式匹配所有import语句
|
|
70
|
+
const lastImportRegex = /^import\s.+from\s['"].*['"];?$/m;
|
|
71
|
+
const matches = code.match(lastImportRegex);
|
|
72
|
+
// 替换或追加新的import语句
|
|
73
|
+
if (matches && matches.length > 0) {
|
|
74
|
+
const lastImport = matches[matches.length - 1];
|
|
75
|
+
code = code.replace(lastImport, `${lastImport}\n${newImport}`);
|
|
76
|
+
}
|
|
77
|
+
// 如果文件末尾没有其他import语句,则追加到文件顶部
|
|
78
|
+
if (!code.includes(newImport)) {
|
|
79
|
+
code = `${newImport}\n` + code;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return code;
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface ViteEnvRuntimeOptions {
|
|
2
|
+
/**
|
|
3
|
+
* runtime唯一key,用来区分唯一环境
|
|
4
|
+
*/
|
|
5
|
+
key?: RegExp | ((file: string) => boolean);
|
|
6
|
+
/**
|
|
7
|
+
* 需要排除的env变量
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* excludes:[`import.meta.env.MODE`,`import.meta.env.BASE_URL`,`import.meta.env.PROD`,`import.meta.env.DEV`,`import.meta.env.SSR`]
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
excludes?: string[];
|
|
14
|
+
ruleResolve?: () => 'development' | 'production' | 'stg' | 'uat' | string;
|
|
15
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
//@ts-nocheck
|
|
2
|
+
import { log } from 'console';
|
|
3
|
+
import { loadEnv } from 'vite';
|
|
4
|
+
export function envRuntimePlugin() {
|
|
5
|
+
const virtualModuleId = 'virtual:env-module';
|
|
6
|
+
const resolvedVirtualModuleId = '\0' + virtualModuleId;
|
|
7
|
+
const option = {};
|
|
8
|
+
return {
|
|
9
|
+
name: 'vite:env-runtime-plugin',
|
|
10
|
+
enforce: 'pre',
|
|
11
|
+
buildStart() {
|
|
12
|
+
console.log('buildStart');
|
|
13
|
+
const envDev = loadEnv('development', './env');
|
|
14
|
+
const envProd = loadEnv('production', './env');
|
|
15
|
+
option[envDev['VITE_LOCATION_HOST']] = envDev;
|
|
16
|
+
option[envProd['VITE_LOCATION_HOST']] = envProd;
|
|
17
|
+
log('envPlugin', option);
|
|
18
|
+
},
|
|
19
|
+
resolveId(id) {
|
|
20
|
+
if (id === virtualModuleId) {
|
|
21
|
+
return resolvedVirtualModuleId;
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
load(id) {
|
|
25
|
+
if (id === resolvedVirtualModuleId) {
|
|
26
|
+
return `export default ${option}`;
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
transform(code, id, options) {
|
|
30
|
+
if (id.includes('main.ts')) {
|
|
31
|
+
console.log('transform', code, id, options);
|
|
32
|
+
code += `window.importEnv = ${JSON.stringify(option)}`;
|
|
33
|
+
console.log(code);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
code = code.replace(/import\.meta\.env\.(\w+)/g, ($1, $2, $3) => {
|
|
37
|
+
console.log($1, $2, $3);
|
|
38
|
+
return `window.importEnv[window.location.host][${$2}]`;
|
|
39
|
+
});
|
|
40
|
+
if (id.includes('home/index.vue')) {
|
|
41
|
+
console.log(id, code);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return code;
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import proxy from 'http2-proxy';
|
|
2
|
+
import { createLogger } from 'vite';
|
|
3
|
+
// export const https = {
|
|
4
|
+
// key: fs.readFileSync(path.resolve(__dirname, './cert/local.key')),
|
|
5
|
+
// cert: fs.readFileSync(path.resolve(__dirname, './cert/local.pem')),
|
|
6
|
+
// maxSessionMemory: 1000, // 默认10M
|
|
7
|
+
// peerMaxConcurrentStreams: 300, // 设置远程对等点的最大并发流数
|
|
8
|
+
// }
|
|
9
|
+
export const http2ProxyPlugin = () => {
|
|
10
|
+
let routes;
|
|
11
|
+
return {
|
|
12
|
+
name: 'vite:http2-proxy-plugin',
|
|
13
|
+
config: (config) => {
|
|
14
|
+
const { server } = config;
|
|
15
|
+
routes = server?.proxy ?? {};
|
|
16
|
+
if (server) {
|
|
17
|
+
server.proxy = undefined;
|
|
18
|
+
}
|
|
19
|
+
return config;
|
|
20
|
+
},
|
|
21
|
+
configureServer: ({ config: { logger }, middlewares }) => {
|
|
22
|
+
Object.entries(routes).forEach(([route, value]) => {
|
|
23
|
+
if (!value) {
|
|
24
|
+
createLogger().error("proxy don't have target api");
|
|
25
|
+
}
|
|
26
|
+
let url = value;
|
|
27
|
+
if (value instanceof Object && !Array.isArray(value)) {
|
|
28
|
+
url = value.target;
|
|
29
|
+
}
|
|
30
|
+
const { protocol, hostname, port } = new URL(url);
|
|
31
|
+
const options = {
|
|
32
|
+
protocol: protocol,
|
|
33
|
+
hostname,
|
|
34
|
+
port: Number(port),
|
|
35
|
+
};
|
|
36
|
+
middlewares.use(route, (req, res) => {
|
|
37
|
+
proxy.web(req, res, { ...options, path: req.originalUrl }, (err) => {
|
|
38
|
+
if (err) {
|
|
39
|
+
logger.error(`[http2-proxy] Error when proxying request on '${req.originalUrl}'`, {
|
|
40
|
+
timestamp: true,
|
|
41
|
+
error: err,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@allkit/vite-config",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "this is a vite config",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/cjs/index.js",
|
|
7
|
+
"module": "./dist/esm/index.js",
|
|
8
|
+
"types": "./dist/esm/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/esm/index.js",
|
|
12
|
+
"require": "./dist/cjs/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"package.json",
|
|
21
|
+
"tsconfig.*.json"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build:esm": "tsc -p ./tsconfig.esm.json",
|
|
25
|
+
"build": "tsc -p ./tsconfig.esm.json && node ./scripts/build.mjs"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [],
|
|
28
|
+
"author": "SPig",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"vite": "^7.3.1"
|
|
32
|
+
},
|
|
33
|
+
"dependencies": {
|
|
34
|
+
"@rollup/plugin-babel": "^6.0.4",
|
|
35
|
+
"@types/node": "22",
|
|
36
|
+
"@vitejs/plugin-legacy": "^7.2.1",
|
|
37
|
+
"@vitejs/plugin-vue": "^6.0.3",
|
|
38
|
+
"@vitejs/plugin-vue-jsx": "^5.1.3",
|
|
39
|
+
"fs-extra": "^11.3.0",
|
|
40
|
+
"http2-proxy": "^5.0.53",
|
|
41
|
+
"picocolors": "^1.1.1",
|
|
42
|
+
"rollup-plugin-visualizer": "^6.0.5",
|
|
43
|
+
"terser": "^5.22.0",
|
|
44
|
+
"vite-plugin-compression": "^0.5.1",
|
|
45
|
+
"vite-plugin-dts": "^4.5.4",
|
|
46
|
+
"vite-plugin-style-import": "^2.0.0"
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"lib": ["ES2020", "DOM"],
|
|
6
|
+
"baseUrl": ".",
|
|
7
|
+
"sourceMap": false,
|
|
8
|
+
"moduleResolution": "bundler",
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
"checkJs": false,
|
|
11
|
+
"strict": false,
|
|
12
|
+
"noImplicitAny": false,
|
|
13
|
+
"resolveJsonModule": true,
|
|
14
|
+
"allowSyntheticDefaultImports": true,
|
|
15
|
+
"esModuleInterop": true,
|
|
16
|
+
"outDir": "dist/esm",
|
|
17
|
+
"declaration": true,
|
|
18
|
+
"declarationDir": "./dist/esm",
|
|
19
|
+
"skipLibCheck": true,
|
|
20
|
+
"types": ["node"]
|
|
21
|
+
},
|
|
22
|
+
"include": ["src", "src/**/*.ts", "src/**/*.d.ts"],
|
|
23
|
+
"exclude": ["node_modules", "dist"]
|
|
24
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"lib": ["ES2020", "DOM"],
|
|
6
|
+
"baseUrl": ".",
|
|
7
|
+
"sourceMap": false,
|
|
8
|
+
"moduleResolution": "bundler",
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
"checkJs": false,
|
|
11
|
+
"strict": false,
|
|
12
|
+
"noImplicitAny": false,
|
|
13
|
+
"resolveJsonModule": true,
|
|
14
|
+
"allowSyntheticDefaultImports": true,
|
|
15
|
+
"esModuleInterop": true,
|
|
16
|
+
"outDir": "dist/esm",
|
|
17
|
+
"declaration": true,
|
|
18
|
+
"declarationDir": "./dist/esm",
|
|
19
|
+
"skipLibCheck": true,
|
|
20
|
+
"types": ["node"]
|
|
21
|
+
},
|
|
22
|
+
"include": ["src", "src/**/*.ts", "src/**/*.d.ts"],
|
|
23
|
+
"exclude": ["node_modules", "dist"]
|
|
24
|
+
}
|