micro.js 5.02 KB
Newer Older
邓晓峰's avatar
邓晓峰 committed
1 2
import {
  addGlobalUncaughtErrorHandler,
3
  initGlobalState,
邓晓峰's avatar
邓晓峰 committed
4 5 6 7 8
  registerMicroApps,
  runAfterFirstMounted,
  setDefaultMountApp,
  start,
} from 'qiankun';
邓晓峰's avatar
邓晓峰 committed
9
import 'kit_logger';
邓晓峰'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 14
// eslint-disable-next-line no-undef
const Logger = logger('micro');
邓晓峰's avatar
邓晓峰 committed
15
const MICRO_STATUS = {
邓晓峰's avatar
邓晓峰 committed
16 17 18
  NOT_LOADED: 'NOT_LOADED',
  LOADING_SOURCE_CODE: 'LOADING_SOURCE_CODE',
  NOT_BOOTSTRAPPED: 'NOT_BOOTSTRAPPED',
邓晓峰's avatar
邓晓峰 committed
19 20 21 22 23 24 25 26
  BOOTSTRAPPING: 'BOOTSTRAPPING',
  NOT_MOUNTED: 'NOT_MOUNTED',
  MOUNTING: 'MOUNTING',
  MOUNTED: 'MOUNTED',
  UPDATING: 'UPDATING',
  UNMOUNTING: 'UNMOUNTING',
  UNLOADING: 'UNLOADING',
  SKIP_BECAUSE_BROKEN: 'SKIP_BECAUSE_BROKEN',
邓晓峰's avatar
邓晓峰 committed
27 28
  LOAD_ERROR: 'LOAD_ERROR',
};
邓晓峰's avatar
邓晓峰 committed
29
export const initMicroApps = loader => {
邓晓峰's avatar
邓晓峰 committed
30 31 32
  const entrys =
    process.env.NODE_ENV !== 'production' ? micorConfig.dev : micorConfig.prod;

邓晓峰's avatar
邓晓峰 committed
33
  registerMicroApps(
邓晓峰's avatar
邓晓峰 committed
34 35 36 37 38
    entrys.map(item => {
      item.loader = loader;
      item.props = {
        emitter: window.share.event,
        baseRoot: item.name,
邓晓峰's avatar
邓晓峰 committed
39
        // eslint-disable-next-line no-undef
40
        globalConfig: createStoreage.get('globalConfig'),
邓晓峰's avatar
邓晓峰 committed
41 42 43 44
        XMLHttpRequest: window.XMLHttpRequest,
      };
      return item;
    }),
邓晓峰's avatar
邓晓峰 committed
45 46 47
    {
      beforeLoad: [
        app => {
邓晓峰's avatar
邓晓峰 committed
48
          Logger.info('[LifeCycle] before load %c%s');
邓晓峰's avatar
邓晓峰 committed
49 50 51 52 53
        },
      ],
      beforeMount: [
        app => {
          // hookRequest()
邓晓峰's avatar
邓晓峰 committed
54 55 56 57 58 59 60 61 62
          // 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
63
          Logger.info(`[LifeCycle] before mount %c%s ${app.name}`);
邓晓峰's avatar
邓晓峰 committed
64 65
        },
      ],
邓晓峰's avatar
邓晓峰 committed
66
      afterMount: [
邓晓峰's avatar
邓晓峰 committed
67
        app => {
邓晓峰's avatar
邓晓峰 committed
68
          Logger.info(`[LifeCycle] after mount %c%s ${app.name}`);
邓晓峰's avatar
邓晓峰 committed
69 70
        },
      ],
邓晓峰's avatar
邓晓峰 committed
71
      beforeUnmount: [
邓晓峰's avatar
邓晓峰 committed
72
        app => {
邓晓峰's avatar
邓晓峰 committed
73
          window.share.event.removeAllListeners('changeRoute');
邓晓峰's avatar
邓晓峰 committed
74
          Logger.info(`[LifeCycle] after unmount %c%s ${app.name}`);
邓晓峰's avatar
邓晓峰 committed
75 76
        },
      ],
邓晓峰's avatar
邓晓峰 committed
77
      afterUnmount: [app => {}],
邓晓峰's avatar
邓晓峰 committed
78 79
    },
  );
80
  const { setGlobalState } = initGlobalState({
邓晓峰's avatar
邓晓峰 committed
81 82
    // eslint-disable-next-line no-undef
    globalConfig: createStoreage.get('globalConfig'),
83 84
  });
  setGlobalState({
邓晓峰's avatar
邓晓峰 committed
85 86 87
    // eslint-disable-next-line no-undef
    globalConfig: createStoreage.get('globalConfig'),
  });
邓晓峰's avatar
邓晓峰 committed
88 89 90 91 92
  start({
    sandbox: {
      experimentalStyleIsolation: true,
      loose: true,
    },
邓晓峰's avatar
邓晓峰 committed
93
    singular: true,
邓晓峰's avatar
邓晓峰 committed
94
    scopedCSS: true,
邓晓峰's avatar
邓晓峰 committed
95
    // eslint-disable-next-line no-underscore-dangle
邓晓峰's avatar
邓晓峰 committed
96
    getPublicPath: window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__,
邓晓峰's avatar
邓晓峰 committed
97 98 99
    excludeAssetFilter: url =>
      url.indexOf('framework/amap/AMap.UI') !== -1 ||
      url.indexOf('framework/amap/init.js') !== -1 ||
100
      url.indexOf('configuration/js') !== -1 ||
邓晓峰's avatar
邓晓峰 committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
      url.indexOf('framework/three.js') !== -1 ||
      url.indexOf('threedimensional/frameworkthree') !== -1 ||
      url.indexOf('iframe/Civ3DLLab/js') !== -1 ||
      url.indexOf('framework/jquery/gridify-min.js') !== -1 ||
      url.indexOf('framework/jquery/gridify.qrcode.js') !== -1 ||
      url.indexOf('echarts') !== -1 ||
      url.indexOf('lbs.amap.com') !== -1 ||
      url.indexOf('restapi.amap.com') !== -1 ||
      url.indexOf('webapi.amap.com') !== -1 ||
      url.indexOf('webapi.amap.com/count') !== -1 ||
      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 ||
      url.indexOf('mt1.google.cn') !== -1 ||
      url.indexOf('mt2.google.cn') !== -1 ||
      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
124 125
  });
  runAfterFirstMounted(() => {
邓晓峰's avatar
邓晓峰 committed
126
    Logger.info('[MainApp] first app mounted');
邓晓峰's avatar
邓晓峰 committed
127
  });
邓晓峰's avatar
邓晓峰 committed
128
  // eslint-disable-next-line no-use-before-define
邓晓峰's avatar
邓晓峰 committed
129
  defaultApp();
邓晓峰's avatar
邓晓峰 committed
130
  addGlobalUncaughtErrorHandler(event => {
邓晓峰's avatar
邓晓峰 committed
131
    const { error } = event;
邓晓峰's avatar
邓晓峰 committed
132
    const reson = 'Failed to fetch';
邓晓峰's avatar
邓晓峰 committed
133 134 135 136 137
    if (
      error &&
      error.message.indexOf(MICRO_STATUS.LOADING_SOURCE_CODE) &&
      error.message.indexOf(reson)
    ) {
邓晓峰's avatar
邓晓峰 committed
138
      // window.history.pushState({message: '应用服务请求异常,请检查应用配置'}, null, '/civbase/500')
邓晓峰's avatar
邓晓峰 committed
139 140
    }
  });
邓晓峰's avatar
邓晓峰 committed
141 142 143
};

export const defaultApp = () => {
邓晓峰's avatar
邓晓峰 committed
144
  // eslint-disable-next-line no-undef
邓晓峰's avatar
邓晓峰 committed
145 146
  const config = createStoreage.get('globalConfig');
  if (config && config.token) {
邓晓峰's avatar
邓晓峰 committed
147
    // const startWith = config.homepage ? config.homepage.split('/') : [];
邓晓峰's avatar
邓晓峰 committed
148 149 150 151 152 153
    const basePath =
      config.homepage !== '' && FILTER_FOLER_REG.test(config.homepage)
        ? 'civweb4'
        : 'civweb4';
    setDefaultMountApp(
      `/${pkg.name.toLocaleLowerCase()}/${basePath}/?client=${config.client}`,
邓晓峰's avatar
邓晓峰 committed
154 155 156
    );
  }
};