Commit aee2fbd4 authored by 邓晓峰's avatar 邓晓峰

feat: 代理配置热更新

parent 6ff4ced2
......@@ -4,7 +4,7 @@ const defaultSetting = require('./defaultSetting');
const cesiumBuild = '../node_modules/cesium/Build/Cesium';
const pkgName = require('../package.json').name;
// eslint-disable-next-line import/order
const { REACT_APP_ENV } = process.env;
const { NODE_ENV } = process.env;
// eslint-disable-next-line import/order
const path = require('path');
const CESIUM_BASE_URL = `/${pkgName}`;
......@@ -45,7 +45,7 @@ module.exports = {
'Cesium',
],
},
proxy: proxy[REACT_APP_ENV || 'dev'],
proxy: proxy[NODE_ENV],
// openAPI: {
// requestLibPath: "import { request } from '@wisdom-utils/utils'",
// schemaPath: 'http://192.168.10.150:8777/Publish/OMS/swagger/v1/swagger.json',
......
......@@ -15,7 +15,7 @@ export default {
// },
{
name: 'civ_water',
entry: `//${window.location.hostname}:8080/civ_water`,
entry: `//${window.location.hostname}:8081/civ_water`,
container: '#micro-container',
activeRule: '/civbase/civ_water',
},
......
......@@ -4,10 +4,9 @@
// const proxyURL = 'http://192.168.12.47:8082';
const proxyURL = 'https://panda-water.cn';
module.exports = {
assetsRoot: process.env.NODE_ENV !== 'production' ? proxyURL : './',
dev: {
development: {
'/PandaOMS':{
target: proxyURL,
changeOrigin: true,
......
......@@ -3,7 +3,7 @@ const { writeFileSync, mkdirSync, existsSync } = require('fs');
const rimraf = require('rimraf');
const serveStatic = require('serve-static');
const { resolve } = require('path');
const { portfinder, chalk, delay } = require('@umijs/utils');
const { portfinder, chalk, delay, createDebug, chokidar, signale, glob } = require('@umijs/utils');
const argv = require('./argv');
const setup = require('./middlewares/frontendMiddleware');
const pkg = require('../package.json');
......@@ -11,20 +11,19 @@ const config = require('../config/config');
const mockMiddewares = require('./mock');
const emitter = require('./event');
const Server = require('./server');
const debug = createDebug('preset-build-in:proxy:createMiddleware');
const proxyConfig = require('../config/proxy');
const loadDotEnv = require('./utils/loadDotEnv');
const { getSchema } = require('./openapi');
const cleanRequireCache = require('./utils/cleanRequireCache');
(async () => {
const defaultPort =
process.env.PORT ||
argv.port ||
(config && config.devServer && config.devServer.port);
const defaultPort = process.env.PORT || argv.port || (config && config.devServer && config.devServer.port);
const port = await portfinder.getPortPromise({
port: defaultPort ? parseInt(String(defaultPort), 10) : 8080,
});
const homename =
process.env.HOST ||
(config && config.devServer && config.devServer.host) ||
'127.0.0.1';
const homename = process.env.HOST || (config && config.devServer && config.devServer.host) || '127.0.0.1';
console.log(chalk.cyan('Starting the development server...'));
// process.send && process.send({ type: 'UPDATE_PORT', port });
const isHTTPS = process.env.HTTPS || (argv && argv.https);
......@@ -81,6 +80,54 @@ const { getSchema } = require('./openapi');
},
);
// proxy config 热更新
const ignore = [
// ignore mock files under node_modules
'node_modules/**'
];
const cwd = process.cwd();
const proxyWatcherPaths = [
...(glob.sync('config/proxy.js', {
cwd,
ignore,
}) || []),
...(glob.sync('**/proxy.js', {
cwd,
ignore,
}) || []),
...(glob.sync('.env', {
cwd,
ignore,
}) || []),
...(glob.sync('.env.local', {
cwd,
ignore,
}) || [])
];
const watcher = chokidar.watch(proxyWatcherPaths, {
ignoreInitial: true,
});
const errors = [];
watcher.on('ready', () => debug('Initial scan complete. Ready for changes')).on('all', async (event, file) => {
debug(`[${event}] ${file}, reload proxy config`);
errors.splice(0, errors.length);
cleanRequireCache(proxyWatcherPaths);
// eslint-disable-next-line no-const-assign
if (!errors.length) {
server.setupProxy && server.setupProxy(proxyConfig[process.env.NODE_ENV], true);
signale.success(`Proxy config parse success`);
}
});
process.once('SIGINT', async () => {
await watcher.close();
});
emitter.on('onDevCompileDone', async () => {
try {
const openAPIConfig = config.openAPI;
......@@ -96,6 +143,7 @@ const { getSchema } = require('./openapi');
}
});
await server.listen({
port,
homename,
......
......@@ -4,6 +4,7 @@ const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const DevCompileDonePlugin = require('../../internals/webpack/plugins/DevCompileDonePlugin');
const emitter = require('../event');
function getIgnoredWatchRegExp() {
// const absOutputPath = winPath(path.join(process.cwd(), outputPath));
return process.env.WATCH_IGNORED === 'none'
......@@ -29,6 +30,7 @@ module.exports = function addDevMiddlewares(
config,
{ port = 8080, hostname = '127.0.0.1' },
) {
if (!config.mfsu) {
webpackConfig.plugins.push(
new DevCompileDonePlugin({
......@@ -63,8 +65,15 @@ module.exports = function addDevMiddlewares(
app.use(middleware);
app.use(webpackHotMiddleware(compiler));
const fs = middleware.context.outputFileSystem;
app.get('*', (req, res) => {
fs.readFile(path.join(compiler.outputPath, 'index.html'), (err, file) => {
if (err) {
......
......@@ -181,6 +181,7 @@ class Server {
},
});
}
return;
};
let startIndex = null;
......
const { winPath } = require('@umijs/utils')
function cleanRequireCache(paths) {
Object.keys(require.cache).forEach(file => {
if (paths.some(p => winPath(file).indexOf(p) > -1)) {
delete require.cache[file];
}
});
};
module.exports = cleanRequireCache;
\ No newline at end of file
......@@ -116,11 +116,11 @@ export const initMicroApps = () => {
const globalConfig = window.globalConfig;
// globalConfig.homepage = globalConfig.homepage && /civweb4/.test(globalConfig.homepage) ? config.homepage.replace(/civweb4\//, ''): globalConfig.homepage;
// actions.offGlobalStateChange();
actions.setGlobalState({
// eslint-disable-next-line no-undef
globalConfig: globalConfig
//createStoreage.get('globalConfig'),
});
// actions.setGlobalState({
// // eslint-disable-next-line no-undef
// globalConfig: globalConfig
// //createStoreage.get('globalConfig'),
// });
// actions.offGlobalStateChange();
microStore.set("event:globalConfig", {
globalConfig: globalConfig,
......@@ -306,7 +306,8 @@ window.app.define('kit_global_config', require.context('../node_modules/kit_glob
window.app.define('@wisdom-utils/utils', require.context('../node_modules/@wisdom-utils/utils/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('@wisdom-utils/runtime', require.context('../node_modules/@wisdom-utils/runtime/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('@wisdom-utils/components', require.context('../node_modules/@wisdom-utils/components/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'index.js');
window.app.define('axios', require.context('../node_modules/axios/lib', true, /^.\/(lib\/)?[^\/]+\.js$/), 'axios.js');
window.app.define('js-base64', require.context('../node_modules/js-base64', true, /[^\/]+\.js$/), 'base64.js');
/**
* arcgismap
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment