Skip to content

Commit

Permalink
feat: vue2多打包vite/rspack/webpack (#171)
Browse files Browse the repository at this point in the history
* feat: vue2多打包vite/rspack/webpack

* fix: 检视意见修改
  • Loading branch information
Muyu-art authored Aug 21, 2024
1 parent da79036 commit bcdc535
Show file tree
Hide file tree
Showing 14 changed files with 429 additions and 42 deletions.
2 changes: 1 addition & 1 deletion packages/toolkits/pro/template/tinyvue2/.env
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ VITE_MOCK_HOST= http://127.0.0.1:8848
VITE_USE_MOCK= false
VITE_MOCK_IGNORE= /api/user/userInfo,/api/user/login,/api/user/register,/api/employee/getEmployee

VITE_MOCK_SERVER_HOST=/mock
VITE_MOCK_SERVER_HOST=/mock
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
import { fileURLToPath, URL } from 'node:url';

import { defineConfig } from 'vite';
import legacy from '@vitejs/plugin-legacy';
import { fileURLToPath, URL } from 'node:url';
import vue2 from '@vitejs/plugin-vue2';
import { resolve } from 'node:path';
import { viteMockServe } from 'vite-plugin-mock';

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
vue2(),
legacy({
targets: ['ie >= 11'],
additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
}),
viteMockServe({
mockPath: './src/mock',
enable: true,
}) as any,
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url)),
'@': fileURLToPath(new URL('../src', import.meta.url)),
},
},
define: {
Expand All @@ -40,18 +33,4 @@ export default defineConfig({
},
},
},
server: {
proxy: {
'/api': {
target: 'http://127.0.0.1:3000',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ''),
},
'/mock': {
target: 'http://127.0.0.1:8848',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/mock/, ''),
},
},
},
});
23 changes: 23 additions & 0 deletions packages/toolkits/pro/template/tinyvue2/config/vite.config.dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { mergeConfig } from 'vite';
import baseConfig from './vite.config.base';

const proxyConfig = {
'/api': {
target: 'http://127.0.0.1:3000',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ''),
},
'/mock': {
target: 'http://127.0.0.1:8848',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/mock/, ''),
},
};

export default mergeConfig(baseConfig, {
server: {
proxy: {
...proxyConfig,
},
},
});
18 changes: 18 additions & 0 deletions packages/toolkits/pro/template/tinyvue2/config/vite.config.prod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { mergeConfig } from 'vite';
import baseConfig from './vite.config.base';

export default mergeConfig(
{
build: {
rollupConfig: {
output: {
manualChunks: {
vue: ['vue', 'vue-router', 'pinia', '@vueuse/core', 'vue-i18n'],
},
},
},
chunkSizeWarningLimit: 2000,
},
},
baseConfig
);
31 changes: 25 additions & 6 deletions packages/toolkits/pro/template/tinyvue2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
"name": "tinypro-frontend",
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "run-p type-check build-only",
"dev": "vite --config ./config/vite.config.dev.ts --port 3031",
"build": "run-p build-only",
"preview": "vite preview --port 4173",
"build-only": "vite build",
"build-only": "vite build --config ./config/vite.config.prod.ts",
"type-check": "vue-tsc --noEmit",
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
"mock": "tsx ./src/mock/index.ts"
"mock": "tsx ./src/mock/index.ts",
"dev:wp": "webpack-dev-server --progress --config webpack.config.js",
"build:wp": "webpack --progress --config webpack.config.js",
"dev:rp": "rspack serve",
"build:rp": "rspack build"
},
"dependencies": {
"@gaonengwww/mock-server": "^1.0.5",
Expand All @@ -29,26 +33,41 @@
"vue-router": "^3.5.4"
},
"devDependencies": {
"@rspack/cli": "^0.7.5",
"@rspack/core": "^0.7.5",
"@rushstack/eslint-patch": "^1.1.0",
"@types/node": "^16.11.45",
"@types/node": "^16.18.105",
"@vitejs/plugin-legacy": "^2.0.0",
"@vitejs/plugin-vue2": "^1.1.2",
"@vue/eslint-config-prettier": "^7.0.0",
"@vue/eslint-config-typescript": "^11.0.0",
"@vue/tsconfig": "^0.1.3",
"css-loader": "^7.1.2",
"dotenv": "^16.4.5",
"esbuild": "^0.23.0",
"eslint": "^8.5.0",
"eslint-plugin-vue": "^9.0.0",
"html-webpack-plugin": "^5.6.0",
"import-meta-loader": "^1.1.0",
"less": "^4.2.0",
"less-loader": "^12.2.0",
"mockjs": "^1.1.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.5.1",
"style-loader": "^4.0.0",
"style-resources-loader": "^1.5.0",
"terser": "^5.14.2",
"ts-loader": "^9.5.1",
"tsx": "^4.17.0",
"typescript": "~4.7.4",
"vite": "^3.0.2",
"vite-plugin-mock": "^3.0.2",
"vue-i18n-composable": "^2.0.0",
"vue-tsc": "^0.38.8"
"vue-loader": "^15.10.0",
"vue-style-loader": "^4.1.3",
"vue-tsc": "^0.38.8",
"webpack": "~5.65.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "~4.7.1"
}
}
163 changes: 163 additions & 0 deletions packages/toolkits/pro/template/tinyvue2/rspack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
const { resolve } = require('path');
const { VueLoaderPlugin } = require('vue-loader');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const rspack = require('@rspack/core');
const { configDotenv, parse } = require('dotenv');
const { default: importMetaLoader } = require('import-meta-loader');
const path = require('path');
configDotenv({
path: './.env',
});

/** @type {import('@rspack/cli').Configuration} */
const config = {
context: __dirname,
entry: {
main: './src/main.ts',
},
output: {
path: resolve(__dirname, 'dist'), // 打包后的文件输出的目录
filename: `js/[name]_[chunkhash:8].js`, // 设置打包后的 js 文件名,如果在文件名前增加文件路径,会将打包后的 js 文件放在指定的文件夹下
publicPath: '/',
},
experiments: {
css: true,
},
plugins: [
new VueLoaderPlugin(),
new rspack.HtmlRspackPlugin({
template: './index.html',
}),
new rspack.DefinePlugin({
__VUE_OPTIONS_API__: JSON.stringify(true),
__VUE_PROD_DEVTOOLS__: JSON.stringify(false),
'import.meta.env.VITE_CONTEXT': '"/vue-pro/"',
'import.meta.env.VITE_BASE_API': '"/api"',
'import.meta.env.VITE_SERVER_HOST': '"http://127.0.0.1:3000"',
'import.meta.env.VITE_MOCK_HOST': '"http://127.0.0.1:8848"',
'import.meta.env.VITE_USE_MOCK': 'false',
'import.meta.env.VITE_MOCK_IGNORE':
'"/api/user/userInfo,/api/user/login,/api/user/register,/api/employee/getEmployee"',
'import.meta.env.VITE_MOCK_SERVER_HOST': '"/mock"',
BUILD_TOOLS: "'RSPACK'",
}),
],
devServer: {
historyApiFallback: true,
proxy: [
{
context: [process.env.VITE_BASE_API],
target: process.env.VITE_SERVER_HOST,
changeOrigin: true,
pathRewrite: {
'^/api': '',
},
},
{
context: [process.env.VITE_MOCK_SERVER_HOST],
target: process.env.VITE_MOCK_HOST,
changeOrigin: true,
pathRewrite: {
'^/mock': '',
},
},
],
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
experimentalInlineMatchResource: true,
},
},
{
test: /\.ts$/,
loader: 'builtin:swc-loader',
options: {
sourceMap: true,
jsc: {
parser: {
syntax: 'typescript',
},
},
},
type: 'javascript/auto',
},
{
test: /\.less$/,
loader: 'less-loader',
type: 'css',
},
{
test: /\.svg$/,
type: 'asset/resource',
},
{
test: /\.m?js/,
resolve: {
fullySpecified: false,
},
},
{
test: /\.less$/,
use: [
{
loader: 'less-loader',
options: {
additionalData: `@import "${resolve(
'./src/assets/style/breakpoint.less'
)}";`,
},
},
],
type: 'css',
},
{
test: /.(png|jpg|jpeg|gif|svg)$/, // 匹配图片文件
type: 'asset', // type选择asset
parser: {
dataUrlCondition: {
maxSize: 10 * 1024, // 小于10kb转base64位
},
},
generator: {
filename: 'static/images/[name].[contenthash:8][ext]', // 文件输出目录和命名
},
},
{
test: /.(woff2?|eot|ttf|otf)$/, // 匹配字体图标文件
type: 'asset', // type选择asset
parser: {
dataUrlCondition: {
maxSize: 10 * 1024, // 小于10kb转base64位
},
},
generator: {
filename: 'static/fonts/[name].[contenthash:8][ext]', // 文件输出目录和命名
},
},
{
test: /.(mp4|webm|ogg|mp3|wav|flac|aac)$/, // 匹配媒体文件
type: 'asset', // type选择asset
parser: {
dataUrlCondition: {
maxSize: 10 * 1024, // 小于10kb转base64位
},
},
generator: {
filename: 'static/media/[name].[contenthash:8][ext]', // 文件输出目录和命名
},
},
],
},
resolve: {
alias: {
'@': resolve(__dirname, 'src'),
assets: resolve(__dirname, 'src/assets'),
},
extensions: ['.ts', '.js', '.vue'],
},
};
module.exports = config;
2 changes: 1 addition & 1 deletion packages/toolkits/pro/template/tinyvue2/src/api/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface QueryTaskParmas {
}

export function queryEmployeeList(params: QueryTaskParmas) {
return axios.post('/mock/employee/getEmployee', params);
return axios.post('/mock/api/employee/getEmployee', params);
}
export function deleteEmployee(id: string) {
return axios.delete(`/mock/employee/delete?id=${id}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ if (BUILD_TOOLS === 'VITE' || BUILD_TOOLS === 'WEBPACK') {
}
});
}
const toRoutes = (menus: ITreeNodeData[]) => {
export const toRoutes = (menus: ITreeNodeData[]) => {
const router: any[] = [];
for (let i = 0; i < menus.length; i += 1) {
const menu = menus[i];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
// import type { Router, LocationQueryRaw } from 'vue-router';
// import NProgress from 'nprogress'; // progress bar
import { getUserInfo } from '@/api/user';
import { useUserStore } from '@/stores';
import { clearToken, isLogin } from '@/utils/auth';
import { nextTick } from 'vue';

export default function setupPermissionGuard(router: any) {
router.beforeEach(async (to, from, next) => {
// NProgress.start();
if (!isLogin()) {
if (to.name === 'login') {
next();
// NProgress.done();
return;
}
next({
Expand All @@ -17,6 +21,7 @@ export default function setupPermissionGuard(router: any) {
...to.query,
} as any,
});
// NProgress.done();
} else {
await nextTick();
const userStore = useUserStore();
Expand All @@ -32,6 +37,7 @@ export default function setupPermissionGuard(router: any) {
return;
}
next();
// NProgress.done();
}
});
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { RouteConfig } from "vue-router";
import type { RouteConfig } from 'vue-router';

const modules:Record<string, any> = import.meta.glob('./modules/*.ts', { eager: true });
const modules: Record<string, any> =
BUILD_TOOLS === 'RSPACK'
? {}
: import.meta.glob('./modules/*.ts', { eager: true });
const appRoutes: RouteConfig[] = [];

Object.keys(modules).forEach((key) => {
Expand Down
Loading

0 comments on commit bcdc535

Please sign in to comment.