GitBucket
4.6.0
Toggle navigation
Sign in
Files
Branches
1
Tags
Issues
Pull Requests
Labels
Milestones
Wiki
08335
/
hivui-platform-template
hivui平台项目模板
Browse code
vite-plugin-func.js
master
1 parent
fda1471
commit
d63523be9909f15f1bbcf1607b1af9933b593333
12112
authored
on 9 Sep 2021
Showing
4 changed files
.env.development
.gitignore
vite-plugin-func.js
vite.config.js
Ignore Space
Show notes
View
.env.development
VITE_APP_BASE_API = 'http://192.168.4.106:7777/' VITE_APP_SERVER='hirun' VITE_APP_QUERYDATA = '/amrept-web/server/WebServiceWadl/[projectName]/cxfService/queryForm' VITE_APP_SAVEDATA = '/amrept-web/server/WebServiceWadl/[projectName]/cxfService/saveForm'
VITE_APP_BASE_API = 'http://192.168.4.106:7777/' VITE_APP_QUERYDATA = '/amrept-web/server/WebServiceWadl/[projectName]/cxfService/queryForm' VITE_APP_SAVEDATA = '/amrept-web/server/WebServiceWadl/[projectName]/cxfService/saveForm'
Ignore Space
Show notes
View
.gitignore
.DS_Store node_modules/ /dist/ npm-debug.log* yarn-debug.log* yarn-error.log* /test/unit/coverage/ /test/e2e/reports/ selenium-debug.log # Editor directories and files .idea .vscode *.suo *.ntvs* *.njsproj *.sln /eaptpl /project/12112 /project/eaptpl
.DS_Store node_modules/ /dist/ npm-debug.log* yarn-debug.log* yarn-error.log* /test/unit/coverage/ /test/e2e/reports/ selenium-debug.log # Editor directories and files .idea .vscode *.suo *.ntvs* *.njsproj *.sln
Ignore Space
Show notes
View
vite-plugin-func.js
0 → 100644
var querystring = require('querystring'); import Cookies from 'js-cookie' var url = require('url'); const fs = require('fs') var https = require('https'); var request = require('request'); var ejs = require('ejs'); function myPlugin(rawOptions) { const options = { isProduction: process.env.NODE_ENV === 'production', ...rawOptions, root: process.cwd(), }; let postData; let config; return { name: 'vite-plugin-func', // 必须的,将会显示在 warning 和 error 中 configResolved(resolvedConfig) { // 存储最终解析的配置 config = resolvedConfig }, configureServer(server) { server.middlewares.use((req, res, next) => { console.log(new Date(), 'req.url:', req.url) if (req.url.indexOf(".flow") != -1) { let url = req.url; postData = ''; let fmodelpath = url.split("?")[0] var post_data = { fmodelpath: fmodelpath } let path = process.env.VITE_APP_BASE_API + process.env.VITE_APP_SERVER + "/flow/open?pn="; let pn = url.split("/")[1]; const TokenKey = 'EAP-Token'; req.on('data', (chunk) => { post += chunk; postData = querystring.parse(post); // console.log(new Date(), 'PostData:', postData); }); let token = Cookies.get(TokenKey) || "ef5d714e-d9ef-4011-ac15-80b477d312d2"; request({ url: path + pn, method: "POST", json: true, headers: { "token": token, "Authorization": "Bearer " + token, "Content-Type": "application/json;charset=UTF-8" }, body: post_data }, function (error, response, body) { if (!error && response.statusCode == 200) { // console.log(body) // 请求成功的处理逻辑 let pcform = body.outParameter.task.pcform; // var fullPath = "eaptpl/12112/mokuai/shitumulu/biaodan/dtv/1.0.0/desktop/index.html"; let content; try { content = fs.readFileSync(pcform, { encoding: 'utf8' }) } catch (e) { } const statusCode = 200; if (!body) throw new Error(`No body text found for the ${statusCode} status code`); var html = ejs.render(content, rawOptions) let postDataStr = JSON.stringify(postData || {}); let varName = options.varName || 'viteRequestData'; let funcName = new Date().valueOf(); let queryScript = ` function _viteGetQuery${funcName}() { var url = window.location.search; var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for(var i = 0; i < strs.length; i ++) { theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]); } } return theRequest; }` let titleStr = html.match(/<head(.*?)>/g)[0] || ""; html = html.replace( /<head(.*?)>/g, `${titleStr}<script>${queryScript};window.${varName} = Object.assign(_viteGetQuery${funcName}(),${postDataStr}) </script>` ) res.writeHead(statusCode, { 'Content-Length': html.length, 'Content-Type': 'html' }); res.write(html) res.end(); } else { console.log(body) } }); return; } else { next(); } }) }, transformIndexHtml(html) { console.log('config.command:', config.command) if (config.command === 'build') { return html; } let postDataStr = JSON.stringify(postData || {}); let varName = options.varName || 'viteRequestData'; let funcName = new Date().valueOf(); let queryScript = ` function _viteGetQuery${funcName}() { var url = window.location.search; var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for(var i = 0; i < strs.length; i ++) { theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]); } } return theRequest; }` postData = ''; let titleStr = html.match(/<head(.*?)>/g)[0] || ""; return html.replace( /<head(.*?)>/g, `${titleStr}<script>${queryScript};window.${varName} = Object.assign(_viteGetQuery${funcName}(),${postDataStr}) </script>` ) } } } export default myPlugin;
Ignore Space
Show notes
View
vite.config.js
// vite.config.js 利害浏览器自带的 es 动态导入,使项目冷启动 const { resolve } = require('path'); import { defineConfig, loadEnv } from 'vite' import { createVuePlugin } from "vite-plugin-vue2"; import vueJsx from '@vitejs/plugin-vue-jsx' import pageData from "vite-plugin-pagedata" import vitefunc from './vite-plugin-func.js' import { injectHtml } from 'vite-plugin-html'; const pkg = require("./package.json"); // https://vitejs.dev/config/ export default ({ mode }) => { process.env = { ...process.env, ...loadEnv(mode, process.cwd()) }; let aliases = {} let build = { sourcemap: false, //编译 //指定 多个 .html 文件作为入口点 rollupOptions: { // 请确保外部化那些你的库中不需要的依赖 // external: ['vue'], input: { main: resolve(__dirname, 'index.html'), nested: resolve(__dirname, 'nested/index.html') }, output: { format: 'umd', } } } if (mode == 'development') { aliases = { '@project': resolve(`${pkg.name == 'hi-vui-template' ? 'project' : pkg.name}`), '@main': resolve(`${pkg.name == 'hi-vui-template' ? 'project' : pkg.name}/hivuiMain`), } // console.log('aliases@main:', aliases['@main']); } else { } const postDataName = "scpRequestData"; return defineConfig({ build: build, css: { preprocessorOptions: { less: { javascriptEnabled: true, } }, }, resolve: { extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue', '.dtvevt', '.dtv'], alias: aliases, }, plugins: [ vitefunc({ varName: postDataName, htmlWebpackPlugin: { vite: true } }), injectHtml({ // 向预览时注入变量,这个变量跟打包时 html-webpack-plugin 注入的一致,用户判断是否是编译状态 injectData: { htmlWebpackPlugin: { vite: true } } }), pageData({ varName: postDataName }), createVuePlugin(), vueJsx({ // options are passed on to @vue/babel-plugin-jsx }), ], optimizeDeps: { //vite 排除,并同时js 文件中不引用。如果有引用还是会取vite 模板缓存中的文件,这样就无法引用全局 exclude: ["vue", "element-ui"] }, devServer: { port: 227, proxy: 'http://192.168.1.183:5001/' } }) };
// vite.config.js 利害浏览器自带的 es 动态导入,使项目冷启动 const { resolve } = require('path'); import { defineConfig, loadEnv } from 'vite' import { createVuePlugin } from "vite-plugin-vue2"; import vueJsx from '@vitejs/plugin-vue-jsx' import pageData from 'vite-plugin-pagedata' import { injectHtml } from 'vite-plugin-html'; const pkg = require("./package.json"); // https://vitejs.dev/config/ export default ({ mode }) => { process.env = { ...process.env, ...loadEnv(mode, process.cwd()) }; let aliases = {} let build = { sourcemap: false, //编译 //指定 多个 .html 文件作为入口点 rollupOptions: { // 请确保外部化那些你的库中不需要的依赖 // external: ['vue'], input: { main: resolve(__dirname, 'index.html'), nested: resolve(__dirname, 'nested/index.html') }, output: { format: 'umd', } } } if (mode == 'development') { aliases = { '@project': resolve(`${pkg.name == 'hi-vui-template' ? 'project' : pkg.name}`), '@main': resolve(`${pkg.name == 'hi-vui-template' ? 'project' : pkg.name}/hivuiMain`), } // console.log('aliases@main:', aliases['@main']); } else { } return defineConfig({ build: build, css: { preprocessorOptions: { less: { javascriptEnabled: true, } }, }, resolve: { extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue', '.dtvevt', '.dtv'], alias: aliases, }, plugins: [ injectHtml({ //向预览时注入变量,这个变量跟打包时 html-webpack-plugin 注入的一致,用户判断是否是编译状态 injectData:{ htmlWebpackPlugin:{ vite:true } } }), pageData({ varName:'scpRequestData' }), createVuePlugin(), vueJsx({ // options are passed on to @vue/babel-plugin-jsx }), ], optimizeDeps: { //vite 排除,并同时js 文件中不引用。如果有引用还是会取vite 模板缓存中的文件,这样就无法引用全局 exclude:["vue","element-ui"] }, devServer: { port: 227, proxy: 'http://192.168.1.183:5001/' } }) };
Show line notes below