micro.js 4.98 KB
Newer Older
邓晓峰's avatar
邓晓峰 committed
1 2
import {
  addGlobalUncaughtErrorHandler,
3
  initGlobalState,
邓晓峰's avatar
邓晓峰 committed
4 5 6 7 8 9
  registerMicroApps,
  runAfterFirstMounted,
  setDefaultMountApp,
  start,
} from 'qiankun';

邓晓峰's avatar
邓晓峰 committed
10
import micorConfig from '../config/micor';
邓晓峰's avatar
邓晓峰 committed
11
import pkg from '../package.json';
邓晓峰's avatar
邓晓峰 committed
12
import { FILTER_FOLER_REG } from './utils/constants';
邓晓峰's avatar
邓晓峰 committed
13

邓晓峰's avatar
邓晓峰 committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27
const MICRO_STATUS = {
  NOT_LOADED: "NOT_LOADED",
  LOADING_SOURCE_CODE: "LOADING_SOURCE_CODE",
  NOT_BOOTSTRAPPED: "NOT_BOOTSTRAPPED",
  BOOTSTRAPPING: 'BOOTSTRAPPING',
  NOT_MOUNTED: 'NOT_MOUNTED',
  MOUNTING: 'MOUNTING',
  MOUNTED: 'MOUNTED',
  UPDATING: 'UPDATING',
  UNMOUNTING: 'UNMOUNTING',
  UNLOADING: 'UNLOADING',
  SKIP_BECAUSE_BROKEN: 'SKIP_BECAUSE_BROKEN',
  LOAD_ERROR: 'LOAD_ERROR'
}
邓晓峰's avatar
邓晓峰 committed
28
export const initMicroApps = loader => {
邓晓峰's avatar
邓晓峰 committed
29 30 31
  const entrys =
    process.env.NODE_ENV !== 'production' ? micorConfig.dev : micorConfig.prod;

邓晓峰's avatar
邓晓峰 committed
32
  registerMicroApps(
邓晓峰's avatar
邓晓峰 committed
33 34 35 36 37
    entrys.map(item => {
      item.loader = loader;
      item.props = {
        emitter: window.share.event,
        baseRoot: item.name,
38
        globalConfig: createStoreage.get('globalConfig'),
邓晓峰's avatar
邓晓峰 committed
39 40 41 42
        XMLHttpRequest: window.XMLHttpRequest,
      };
      return item;
    }),
邓晓峰's avatar
邓晓峰 committed
43 44 45 46 47 48
    {
      beforeLoad: [
        app => {
          console.log(
            '[LifeCycle] before load %c%s',
            'color: green;',
邓晓峰's avatar
邓晓峰 committed
49
            app.name,
邓晓峰's avatar
邓晓峰 committed
50 51 52 53 54 55
          );
        },
      ],
      beforeMount: [
        app => {
          // hookRequest()
邓晓峰's avatar
邓晓峰 committed
56 57 58 59 60 61 62 63 64
          // ReactDOM.unmountComponentAtNode(
          //   document.getElementById('subapp-container'),
          // );
          // console.log(
          //   '[LifeCycle] before mount %c%s',
          //   'color: green;',
          //   JSON.stringify(app),
          // );
          // window.share.event.removeAllListeners();
邓晓峰's avatar
邓晓峰 committed
65 66 67
          console.log(
            '[LifeCycle] before mount %c%s',
            'color: green;',
邓晓峰's avatar
邓晓峰 committed
68
            app.name,
邓晓峰's avatar
邓晓峰 committed
69 70 71
          );
        },
      ],
邓晓峰's avatar
邓晓峰 committed
72
      afterMount: [
邓晓峰's avatar
邓晓峰 committed
73 74
        app => {
          console.log(
邓晓峰's avatar
邓晓峰 committed
75
            '[LifeCycle] after mount %c%s',
邓晓峰's avatar
邓晓峰 committed
76
            'color: green;',
邓晓峰's avatar
邓晓峰 committed
77
            app.name,
邓晓峰's avatar
邓晓峰 committed
78
          );
邓晓峰's avatar
邓晓峰 committed
79
          
邓晓峰's avatar
邓晓峰 committed
80 81
        },
      ],
邓晓峰's avatar
邓晓峰 committed
82
      beforeUnmount: [
邓晓峰's avatar
邓晓峰 committed
83
        app => {
邓晓峰's avatar
邓晓峰 committed
84
          window.share.event.removeAllListeners('changeRoute');
邓晓峰's avatar
邓晓峰 committed
85 86 87
          console.log(
            '[LifeCycle] after unmount %c%s',
            'color: green;',
邓晓峰's avatar
邓晓峰 committed
88
            app.name,
邓晓峰's avatar
邓晓峰 committed
89
          );
邓晓峰's avatar
邓晓峰 committed
90 91 92 93
        },
      ],
      afterUnmount: [
        app => {
邓晓峰's avatar
邓晓峰 committed
94 95 96 97
        },
      ],
    },
  );
98 99 100 101 102 103
  const { setGlobalState } = initGlobalState({
    globalConfig: createStoreage.get('globalConfig')
  });
  setGlobalState({
    globalConfig: createStoreage.get('globalConfig')
  })
邓晓峰's avatar
邓晓峰 committed
104 105 106 107 108
  start({
    sandbox: {
      experimentalStyleIsolation: true,
      loose: true,
    },
邓晓峰's avatar
邓晓峰 committed
109
    singular: true,
邓晓峰's avatar
邓晓峰 committed
110 111 112 113
    scopedCSS: true,
    getPublicPath: window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__,
    excludeAssetFilter: url => {
      return (
邓晓峰's avatar
邓晓峰 committed
114 115 116
        url.indexOf('framework/amap/AMap.UI') !== -1 ||
        url.indexOf('framework/amap/init.js') !== -1 ||
        url.indexOf('framework/three.js') !== -1 ||
邓晓峰's avatar
邓晓峰 committed
117 118
        url.indexOf('threedimensional/frameworkthree') !== -1 ||
        url.indexOf('iframe/Civ3DLLab/js') !== -1 ||
邓晓峰's avatar
邓晓峰 committed
119 120
        url.indexOf('framework/jquery/gridify-min.js') !== -1 ||
        url.indexOf('framework/jquery/gridify.qrcode.js') !== -1 ||
邓晓峰's avatar
邓晓峰 committed
121
        url.indexOf('echarts') !== -1 ||
邓晓峰's avatar
邓晓峰 committed
122 123
        url.indexOf('lbs.amap.com') !== -1 ||
        url.indexOf('restapi.amap.com') !== -1 ||
邓晓峰's avatar
邓晓峰 committed
124
        url.indexOf('webapi.amap.com') !== -1 ||
邓晓峰's avatar
邓晓峰 committed
125
        url.indexOf('webapi.amap.com/count') !== -1 ||
邓晓峰's avatar
邓晓峰 committed
126 127 128 129
        url.indexOf('api.map.baidu.com') !== -1 ||
        url.indexOf('map.baidu.com') !== -1 ||
        url.indexOf('pv.sohu.com') !== -1 ||
        url.indexOf('mt0.google.cn') !== -1 ||
邓晓峰's avatar
邓晓峰 committed
130 131
        url.indexOf('mt1.google.cn') !== -1 ||
        url.indexOf('mt2.google.cn') !== -1 ||
邓晓峰's avatar
邓晓峰 committed
132 133 134 135 136 137 138 139
        url.indexOf('mt3.google.cn') !== -1 ||
        url.indexOf('hm.baidu.com') !== -1 || 
        url.indexOf('https://maponline0.bdimg.com') !== -1 ||
        url.indexOf('https://maponline1.bdimg.com') !== -1 ||
        url.indexOf('https://maponline2.bdimg.com') !== -1 ||
        url.indexOf('https://maponline3.bdimg.com') !== -1 ||
        url.indexOf('https://api.map.baidu.com/getscript') !== -1

邓晓峰's avatar
邓晓峰 committed
140 141 142 143 144 145
      );
    },
  });
  runAfterFirstMounted(() => {
    console.log('[MainApp] first app mounted');
  });
邓晓峰's avatar
邓晓峰 committed
146
  defaultApp();
邓晓峰's avatar
邓晓峰 committed
147
  addGlobalUncaughtErrorHandler(event => {
邓晓峰's avatar
邓晓峰 committed
148
    console.log(event);
邓晓峰's avatar
邓晓峰 committed
149 150 151 152
    
    const error = event.error;
    const reson = 'Failed to fetch';
    if(error && error.message.indexOf(MICRO_STATUS.LOADING_SOURCE_CODE) && error.message.indexOf(reson)) {
邓晓峰's avatar
邓晓峰 committed
153
      // window.history.pushState({message: '应用服务请求异常,请检查应用配置'}, null, '/civbase/500')
邓晓峰's avatar
邓晓峰 committed
154 155
    }
  });
邓晓峰's avatar
邓晓峰 committed
156 157 158 159 160
};

export const defaultApp = () => {
  const config = createStoreage.get('globalConfig');
  if (config && config.token) {
邓晓峰's avatar
邓晓峰 committed
161
    // const startWith = config.homepage ? config.homepage.split('/') : [];
邓晓峰's avatar
邓晓峰 committed
162 163
    const basePath = (config.homepage !== "" && FILTER_FOLER_REG.test(config.homepage)) ? 'civweb4': 'civweb4';
    setDefaultMountApp( `/${pkg.name.toLocaleLowerCase()}/${basePath}/?client=${config.client}`,
邓晓峰's avatar
邓晓峰 committed
164 165 166
    );
  }
};