@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 @@
|
|
|
1
|
+
export declare const useBuild: () => import("vite").BuildEnvironmentOptions;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.useBuild = void 0;
|
|
7
|
+
const node_process_1 = __importDefault(require("node:process"));
|
|
8
|
+
const path_1 = require("path");
|
|
9
|
+
const useBuild = () => {
|
|
10
|
+
const option = {
|
|
11
|
+
cssTarget: 'chrome61',
|
|
12
|
+
chunkSizeWarningLimit: 2000,
|
|
13
|
+
emptyOutDir: true,
|
|
14
|
+
outDir: 'dist',
|
|
15
|
+
rollupOptions: {
|
|
16
|
+
input: {
|
|
17
|
+
main: (0, path_1.resolve)(node_process_1.default.cwd(), 'index.html'),
|
|
18
|
+
},
|
|
19
|
+
output: {
|
|
20
|
+
manualChunks: (filePath) => {
|
|
21
|
+
if (filePath.includes('node_modules/@vue') ||
|
|
22
|
+
filePath.includes('node_modules/vue-router') ||
|
|
23
|
+
filePath.includes('node_modules/pinia')) {
|
|
24
|
+
// vue全家桶放到一个包
|
|
25
|
+
return 'vue-family';
|
|
26
|
+
}
|
|
27
|
+
if (filePath.includes('node_modules/vant') || filePath.includes('node_modules/@vant')) {
|
|
28
|
+
return 'vant';
|
|
29
|
+
}
|
|
30
|
+
if (filePath.includes('node_modules/element-plus') ||
|
|
31
|
+
filePath.includes('node_modules/@element-plus')) {
|
|
32
|
+
return 'element-plus';
|
|
33
|
+
}
|
|
34
|
+
if (filePath.includes('node_modules/chart') ||
|
|
35
|
+
filePath.includes('node_modules/vue-chartjs') ||
|
|
36
|
+
filePath.includes('node_modules/echarts')) {
|
|
37
|
+
return 'chart-js';
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
return option;
|
|
44
|
+
};
|
|
45
|
+
exports.useBuild = useBuild;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.viteLibConfig = exports.viteConfig = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const process_1 = __importDefault(require("process"));
|
|
9
|
+
const vite_1 = require("vite");
|
|
10
|
+
const logger_js_1 = require("../logger.js");
|
|
11
|
+
const build_js_1 = require("./build.js");
|
|
12
|
+
const lib_plugins_js_1 = require("./lib-plugins.js");
|
|
13
|
+
const plugins_js_1 = require("./plugins.js");
|
|
14
|
+
const server_js_1 = require("./server.js");
|
|
15
|
+
const viteConfig = (config = {}) => {
|
|
16
|
+
const mode = config.mode || process_1.default.env.NODE_ENV;
|
|
17
|
+
const env = (0, vite_1.loadEnv)(mode || '', config.envDir || './');
|
|
18
|
+
logger_js_1.viteLog.success('**************************************');
|
|
19
|
+
logger_js_1.viteLog.info('--mode =', mode);
|
|
20
|
+
logger_js_1.viteLog.info('--envDir =', config.envDir);
|
|
21
|
+
logger_js_1.viteLog.success('**************************************');
|
|
22
|
+
const _commonConfig = {
|
|
23
|
+
base: config.base,
|
|
24
|
+
resolve: {
|
|
25
|
+
extensions: ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'],
|
|
26
|
+
alias: {
|
|
27
|
+
'@': path_1.default.resolve(process_1.default.cwd(), './src'),
|
|
28
|
+
'~/': `${path_1.default.resolve(process_1.default.cwd(), './src')}/`,
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
optimizeDeps: {
|
|
32
|
+
include: ['vue', 'vue-router'],
|
|
33
|
+
},
|
|
34
|
+
build: (0, build_js_1.useBuild)(),
|
|
35
|
+
plugins: (0, plugins_js_1.usePlugins)(env),
|
|
36
|
+
server: (0, server_js_1.useDevServer)(env),
|
|
37
|
+
};
|
|
38
|
+
const _config = (0, vite_1.mergeConfig)(_commonConfig, config);
|
|
39
|
+
return _config;
|
|
40
|
+
};
|
|
41
|
+
exports.viteConfig = viteConfig;
|
|
42
|
+
/**
|
|
43
|
+
* 库模式
|
|
44
|
+
*/
|
|
45
|
+
const viteLibConfig = (config = {}) => {
|
|
46
|
+
const mode = config.mode || process_1.default.env.NODE_ENV;
|
|
47
|
+
logger_js_1.viteLog.success('**************************************');
|
|
48
|
+
logger_js_1.viteLog.info('--mode =', mode);
|
|
49
|
+
logger_js_1.viteLog.info('--envDir =', config.envDir);
|
|
50
|
+
logger_js_1.viteLog.success('**************************************');
|
|
51
|
+
const _commonConfig = {
|
|
52
|
+
base: config.base,
|
|
53
|
+
resolve: {
|
|
54
|
+
extensions: ['.mjs', '.js', '.mts', '.ts', '.jsx', '.tsx', '.json'],
|
|
55
|
+
alias: {
|
|
56
|
+
'@': path_1.default.resolve(process_1.default.cwd(), './src'),
|
|
57
|
+
'~/': `${path_1.default.resolve(process_1.default.cwd(), './src')}/`,
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
optimizeDeps: {
|
|
61
|
+
include: ['pinia', 'vue', 'vue-router'],
|
|
62
|
+
},
|
|
63
|
+
plugins: (0, lib_plugins_js_1.useLibPlugins)(),
|
|
64
|
+
build: {
|
|
65
|
+
target: 'es2015',
|
|
66
|
+
cssTarget: 'chrome61',
|
|
67
|
+
outDir: 'dist',
|
|
68
|
+
rollupOptions: {
|
|
69
|
+
// // 确保外部化处理那些你不想打包进库的依赖
|
|
70
|
+
external: ['vue', 'vue-router'],
|
|
71
|
+
output: {
|
|
72
|
+
exports: 'named',
|
|
73
|
+
// 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
|
|
74
|
+
globals: {
|
|
75
|
+
vue: 'Vue',
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
const _config = (0, vite_1.mergeConfig)(_commonConfig, config);
|
|
82
|
+
return _config;
|
|
83
|
+
};
|
|
84
|
+
exports.viteLibConfig = viteLibConfig;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.useLibPlugins = void 0;
|
|
7
|
+
const plugin_vue_1 = __importDefault(require("@vitejs/plugin-vue"));
|
|
8
|
+
const plugin_vue_jsx_1 = __importDefault(require("@vitejs/plugin-vue-jsx"));
|
|
9
|
+
const vite_plugin_dts_1 = __importDefault(require("vite-plugin-dts"));
|
|
10
|
+
/** 库模式 */
|
|
11
|
+
const useLibPlugins = () => {
|
|
12
|
+
const plugins = [
|
|
13
|
+
(0, plugin_vue_1.default)(),
|
|
14
|
+
(0, plugin_vue_jsx_1.default)(),
|
|
15
|
+
(0, vite_plugin_dts_1.default)({
|
|
16
|
+
exclude: ['node_modules', 'src/**/__test__', 'src/**/__tests__'],
|
|
17
|
+
outDir: 'dist',
|
|
18
|
+
insertTypesEntry: true,
|
|
19
|
+
}),
|
|
20
|
+
];
|
|
21
|
+
return plugins;
|
|
22
|
+
};
|
|
23
|
+
exports.useLibPlugins = useLibPlugins;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.usePlugins = void 0;
|
|
7
|
+
const node_process_1 = __importDefault(require("node:process"));
|
|
8
|
+
const plugin_vue_1 = __importDefault(require("@vitejs/plugin-vue"));
|
|
9
|
+
const plugin_vue_jsx_1 = __importDefault(require("@vitejs/plugin-vue-jsx"));
|
|
10
|
+
const plugin_legacy_1 = __importDefault(require("@vitejs/plugin-legacy"));
|
|
11
|
+
const rollup_plugin_visualizer_1 = require("rollup-plugin-visualizer");
|
|
12
|
+
const vite_plugin_compression_1 = __importDefault(require("vite-plugin-compression"));
|
|
13
|
+
const logger_js_1 = __importDefault(require("../logger.js"));
|
|
14
|
+
/** 库模式 */
|
|
15
|
+
const usePlugins = (env) => {
|
|
16
|
+
const isProd = node_process_1.default.env.NODE_ENV === 'production';
|
|
17
|
+
const plugins = [
|
|
18
|
+
(0, plugin_vue_1.default)(),
|
|
19
|
+
(0, plugin_vue_jsx_1.default)(),
|
|
20
|
+
(0, plugin_legacy_1.default)({
|
|
21
|
+
targets: ['defaults', 'not IE 11', 'Chrome 63', 'iOS >= 12', 'android >= 6'],
|
|
22
|
+
polyfills: [
|
|
23
|
+
'es.symbol',
|
|
24
|
+
'es.array.flat',
|
|
25
|
+
'web.dom-collections.for-each',
|
|
26
|
+
'es.promise.finally',
|
|
27
|
+
'es.string.replace-all',
|
|
28
|
+
],
|
|
29
|
+
modernPolyfills: [
|
|
30
|
+
'es.promise.finally',
|
|
31
|
+
'es.global-this',
|
|
32
|
+
'es.string.match-all',
|
|
33
|
+
'es.string.replace-all',
|
|
34
|
+
],
|
|
35
|
+
}),
|
|
36
|
+
];
|
|
37
|
+
//包分析的时候,才使用这个包
|
|
38
|
+
if (env.VITE_PLUGIN_ANALYZER === 'true') {
|
|
39
|
+
logger_js_1.default.warn('打包分析', '-已开启');
|
|
40
|
+
plugins.push((0, rollup_plugin_visualizer_1.visualizer)({
|
|
41
|
+
emitFile: false,
|
|
42
|
+
filename: 'stats.html',
|
|
43
|
+
open: true,
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
if (isProd && Boolean(env.VITE_PLUGIN_COMPRESSION === 'true')) {
|
|
47
|
+
logger_js_1.default.warn('代码压缩', '-已开启');
|
|
48
|
+
plugins.push((0, vite_plugin_compression_1.default)({
|
|
49
|
+
threshold: 1024,
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
return plugins;
|
|
53
|
+
};
|
|
54
|
+
exports.usePlugins = usePlugins;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.useDevServer = void 0;
|
|
7
|
+
const node_process_1 = __importDefault(require("node:process"));
|
|
8
|
+
const useDevServer = (env) => {
|
|
9
|
+
const isProd = node_process_1.default.env.NODE_ENV === 'production';
|
|
10
|
+
if (isProd)
|
|
11
|
+
return undefined;
|
|
12
|
+
const option = {
|
|
13
|
+
host: '0.0.0.0',
|
|
14
|
+
port: env.VITE_PORT ? Number(env.VITE_PORT) : 3000,
|
|
15
|
+
proxy: {},
|
|
16
|
+
};
|
|
17
|
+
const proxyConf = env.VITE_PROXY ? JSON.parse(env.VITE_PROXY) : {};
|
|
18
|
+
Object.keys(proxyConf).forEach((key) => {
|
|
19
|
+
const reg = new RegExp('^\\' + key);
|
|
20
|
+
const proxy = {
|
|
21
|
+
target: proxyConf[key].target,
|
|
22
|
+
changeOrigin: true,
|
|
23
|
+
ws: true,
|
|
24
|
+
rewrite: (path) => {
|
|
25
|
+
const _path = path.replace(reg, proxyConf[key].rewrite);
|
|
26
|
+
return _path;
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
if (option && option.proxy) {
|
|
30
|
+
option.proxy[key] = proxy;
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
return option;
|
|
34
|
+
};
|
|
35
|
+
exports.useDevServer = useDevServer;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./core"), exports);
|
|
18
|
+
__exportStar(require("./logger"), exports);
|
|
19
|
+
__exportStar(require("./plugins"), exports);
|
|
@@ -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,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.viteLog = void 0;
|
|
7
|
+
const picocolors_1 = __importDefault(require("picocolors"));
|
|
8
|
+
const viteLog = {
|
|
9
|
+
/**
|
|
10
|
+
* 基本日志
|
|
11
|
+
*/
|
|
12
|
+
info(...arg) {
|
|
13
|
+
const [tag, ...rest] = arg || [];
|
|
14
|
+
console.log(picocolors_1.default.green('[vite-config]'), picocolors_1.default.blue(tag), ...rest);
|
|
15
|
+
},
|
|
16
|
+
/**
|
|
17
|
+
* 成功日志
|
|
18
|
+
*/
|
|
19
|
+
success(...arg) {
|
|
20
|
+
const [tag, ...rest] = arg || [];
|
|
21
|
+
console.log(picocolors_1.default.green('[vite-config]'), picocolors_1.default.green(tag), ...rest);
|
|
22
|
+
},
|
|
23
|
+
/**
|
|
24
|
+
* 警告日志
|
|
25
|
+
*/
|
|
26
|
+
warn(...arg) {
|
|
27
|
+
const [tag, ...rest] = arg || [];
|
|
28
|
+
console.log(picocolors_1.default.green('[vite-config]'), picocolors_1.default.yellow(tag), ...rest);
|
|
29
|
+
},
|
|
30
|
+
/**
|
|
31
|
+
* 错误日志
|
|
32
|
+
*/
|
|
33
|
+
error(...arg) {
|
|
34
|
+
const [tag, ...rest] = arg || [];
|
|
35
|
+
console.log(picocolors_1.default.green('[vite-config]'), picocolors_1.default.yellow(tag), ...rest);
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
exports.viteLog = viteLog;
|
|
39
|
+
exports.default = viteLog;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.chiiPlugin = chiiPlugin;
|
|
4
|
+
const node_child_process_1 = require("node:child_process");
|
|
5
|
+
function chiiPlugin() {
|
|
6
|
+
const chiiProcess = (0, node_child_process_1.spawn)('npx', ['chii', 'start', '-p', '8080']);
|
|
7
|
+
chiiProcess.stdout.on('data', (data) => {
|
|
8
|
+
console.log(`chii stdout: ${data}`);
|
|
9
|
+
});
|
|
10
|
+
chiiProcess.stderr.on('data', (data) => {
|
|
11
|
+
console.error(`chii stderr: ${data}`);
|
|
12
|
+
});
|
|
13
|
+
chiiProcess.on('close', (code) => {
|
|
14
|
+
console.log(`chii process exited with code ${code}`);
|
|
15
|
+
});
|
|
16
|
+
return {
|
|
17
|
+
name: 'vite:kye-chill',
|
|
18
|
+
enforce: 'post',
|
|
19
|
+
transformIndexHtml() {
|
|
20
|
+
return [
|
|
21
|
+
{
|
|
22
|
+
tag: 'script',
|
|
23
|
+
attrs: {
|
|
24
|
+
src: '//host-machine-ip:8080/target.js',
|
|
25
|
+
},
|
|
26
|
+
injectTo: 'body',
|
|
27
|
+
},
|
|
28
|
+
];
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -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,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.viteRuleResolve = void 0;
|
|
4
|
+
exports.envRuntimePlugin = envRuntimePlugin;
|
|
5
|
+
const vite_1 = require("vite");
|
|
6
|
+
const defaultEnvList = [
|
|
7
|
+
'import.meta.env.MODE',
|
|
8
|
+
'import.meta.env.BASE_URL',
|
|
9
|
+
'import.meta.env.PROD',
|
|
10
|
+
'import.meta.env.DEV',
|
|
11
|
+
'import.meta.env.SSR',
|
|
12
|
+
];
|
|
13
|
+
/**
|
|
14
|
+
* 默认vite的环境规则解析
|
|
15
|
+
*/
|
|
16
|
+
const viteRuleResolve = () => {
|
|
17
|
+
if (['feiyanyun.com'].includes(window.location.host)) {
|
|
18
|
+
return 'production';
|
|
19
|
+
}
|
|
20
|
+
return 'development';
|
|
21
|
+
};
|
|
22
|
+
exports.viteRuleResolve = viteRuleResolve;
|
|
23
|
+
/**
|
|
24
|
+
* env生成环境运行时
|
|
25
|
+
* @param options - 配置
|
|
26
|
+
*/
|
|
27
|
+
function envRuntimePlugin(options) {
|
|
28
|
+
const virtualModuleId = '\0virtual:env-runtime-plugin';
|
|
29
|
+
let { excludes = [], ruleResolve = exports.viteRuleResolve } = options || {};
|
|
30
|
+
excludes = [...defaultEnvList, ...excludes];
|
|
31
|
+
const envMap = {};
|
|
32
|
+
let configEnv = {};
|
|
33
|
+
return {
|
|
34
|
+
name: 'vite:env-runtime-plugin',
|
|
35
|
+
enforce: 'pre',
|
|
36
|
+
config(config, ConfigEnv) {
|
|
37
|
+
configEnv = ConfigEnv;
|
|
38
|
+
return config;
|
|
39
|
+
},
|
|
40
|
+
buildStart() {
|
|
41
|
+
const envDev = (0, vite_1.loadEnv)('development', './env');
|
|
42
|
+
const envProd = (0, vite_1.loadEnv)('production', './env');
|
|
43
|
+
envMap['development'] = envDev;
|
|
44
|
+
envMap['production'] = envProd;
|
|
45
|
+
},
|
|
46
|
+
resolveId(id) {
|
|
47
|
+
if (id === virtualModuleId) {
|
|
48
|
+
return id;
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
load(id) {
|
|
52
|
+
if (id === virtualModuleId) {
|
|
53
|
+
return `
|
|
54
|
+
let resolveRule = ${ruleResolve}\nlet runtimeMap = ${JSON.stringify(envMap)};\n
|
|
55
|
+
let mode = ${configEnv.command === 'serve' ? JSON.stringify(configEnv.mode) : 'resolveRule()'}
|
|
56
|
+
export default runtimeMap[mode]`;
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
transform(code) {
|
|
60
|
+
let isMatch = false;
|
|
61
|
+
code = code.replace(/import\.meta\.env\.(\w+)/g, (_, $2) => {
|
|
62
|
+
if (excludes.includes(`import.meta.env.${$2}`)) {
|
|
63
|
+
return `import.meta.env.${$2}`;
|
|
64
|
+
}
|
|
65
|
+
isMatch = true;
|
|
66
|
+
return `viteImportEnvMap['${$2}']`;
|
|
67
|
+
});
|
|
68
|
+
if (isMatch) {
|
|
69
|
+
const newImport = `import viteImportEnvMap from "${virtualModuleId}";`;
|
|
70
|
+
// 检查是否已有该import
|
|
71
|
+
if (code.includes(newImport)) {
|
|
72
|
+
return code;
|
|
73
|
+
}
|
|
74
|
+
// 正则表达式匹配所有import语句
|
|
75
|
+
const lastImportRegex = /^import\s.+from\s['"].*['"];?$/m;
|
|
76
|
+
const matches = code.match(lastImportRegex);
|
|
77
|
+
// 替换或追加新的import语句
|
|
78
|
+
if (matches && matches.length > 0) {
|
|
79
|
+
const lastImport = matches[matches.length - 1];
|
|
80
|
+
code = code.replace(lastImport, `${lastImport}\n${newImport}`);
|
|
81
|
+
}
|
|
82
|
+
// 如果文件末尾没有其他import语句,则追加到文件顶部
|
|
83
|
+
if (!code.includes(newImport)) {
|
|
84
|
+
code = `${newImport}\n` + code;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return code;
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
@@ -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,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//@ts-nocheck
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.envRuntimePlugin = envRuntimePlugin;
|
|
5
|
+
const console_1 = require("console");
|
|
6
|
+
const vite_1 = require("vite");
|
|
7
|
+
function envRuntimePlugin() {
|
|
8
|
+
const virtualModuleId = 'virtual:env-module';
|
|
9
|
+
const resolvedVirtualModuleId = '\0' + virtualModuleId;
|
|
10
|
+
const option = {};
|
|
11
|
+
return {
|
|
12
|
+
name: 'vite:env-runtime-plugin',
|
|
13
|
+
enforce: 'pre',
|
|
14
|
+
buildStart() {
|
|
15
|
+
console.log('buildStart');
|
|
16
|
+
const envDev = (0, vite_1.loadEnv)('development', './env');
|
|
17
|
+
const envProd = (0, vite_1.loadEnv)('production', './env');
|
|
18
|
+
option[envDev['VITE_LOCATION_HOST']] = envDev;
|
|
19
|
+
option[envProd['VITE_LOCATION_HOST']] = envProd;
|
|
20
|
+
(0, console_1.log)('envPlugin', option);
|
|
21
|
+
},
|
|
22
|
+
resolveId(id) {
|
|
23
|
+
if (id === virtualModuleId) {
|
|
24
|
+
return resolvedVirtualModuleId;
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
load(id) {
|
|
28
|
+
if (id === resolvedVirtualModuleId) {
|
|
29
|
+
return `export default ${option}`;
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
transform(code, id, options) {
|
|
33
|
+
if (id.includes('main.ts')) {
|
|
34
|
+
console.log('transform', code, id, options);
|
|
35
|
+
code += `window.importEnv = ${JSON.stringify(option)}`;
|
|
36
|
+
console.log(code);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
code = code.replace(/import\.meta\.env\.(\w+)/g, ($1, $2, $3) => {
|
|
40
|
+
console.log($1, $2, $3);
|
|
41
|
+
return `window.importEnv[window.location.host][${$2}]`;
|
|
42
|
+
});
|
|
43
|
+
if (id.includes('home/index.vue')) {
|
|
44
|
+
console.log(id, code);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return code;
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.http2ProxyPlugin = void 0;
|
|
7
|
+
const http2_proxy_1 = __importDefault(require("http2-proxy"));
|
|
8
|
+
const vite_1 = require("vite");
|
|
9
|
+
// export const https = {
|
|
10
|
+
// key: fs.readFileSync(path.resolve(__dirname, './cert/local.key')),
|
|
11
|
+
// cert: fs.readFileSync(path.resolve(__dirname, './cert/local.pem')),
|
|
12
|
+
// maxSessionMemory: 1000, // 默认10M
|
|
13
|
+
// peerMaxConcurrentStreams: 300, // 设置远程对等点的最大并发流数
|
|
14
|
+
// }
|
|
15
|
+
const http2ProxyPlugin = () => {
|
|
16
|
+
let routes;
|
|
17
|
+
return {
|
|
18
|
+
name: 'vite:http2-proxy-plugin',
|
|
19
|
+
config: (config) => {
|
|
20
|
+
const { server } = config;
|
|
21
|
+
routes = server?.proxy ?? {};
|
|
22
|
+
if (server) {
|
|
23
|
+
server.proxy = undefined;
|
|
24
|
+
}
|
|
25
|
+
return config;
|
|
26
|
+
},
|
|
27
|
+
configureServer: ({ config: { logger }, middlewares }) => {
|
|
28
|
+
Object.entries(routes).forEach(([route, value]) => {
|
|
29
|
+
if (!value) {
|
|
30
|
+
(0, vite_1.createLogger)().error("proxy don't have target api");
|
|
31
|
+
}
|
|
32
|
+
let url = value;
|
|
33
|
+
if (value instanceof Object && !Array.isArray(value)) {
|
|
34
|
+
url = value.target;
|
|
35
|
+
}
|
|
36
|
+
const { protocol, hostname, port } = new URL(url);
|
|
37
|
+
const options = {
|
|
38
|
+
protocol: protocol,
|
|
39
|
+
hostname,
|
|
40
|
+
port: Number(port),
|
|
41
|
+
};
|
|
42
|
+
middlewares.use(route, (req, res) => {
|
|
43
|
+
http2_proxy_1.default.web(req, res, { ...options, path: req.originalUrl }, (err) => {
|
|
44
|
+
if (err) {
|
|
45
|
+
logger.error(`[http2-proxy] Error when proxying request on '${req.originalUrl}'`, {
|
|
46
|
+
timestamp: true,
|
|
47
|
+
error: err,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
exports.http2ProxyPlugin = http2ProxyPlugin;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./http2-proxy/index.js"), exports);
|
|
18
|
+
__exportStar(require("./env-runtime/index.js"), exports);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const useBuild: () => import("vite").BuildEnvironmentOptions;
|