micro.js 4.2 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',
};
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
const excludeAssetFilter = [
  'framework/amap/AMap.UI',
  'framework/amap/init.js',
  'configuration/js',
  'framework/three.js',
  'threedimensional/frameworkthree',
  'iframe/Civ3DLLab/js',
  'framework/jquery/gridify-min.js',
  'framework/jquery/gridify.qrcode.js',
  'echarts',
  'lbs.amap.com',
  'restapi.amap.com',
  'lbs.amap.com',
  'restapi.amap.com',
  'webapi.amap.com',
  'webapi.amap.com/count',
  'api.map.baidu.com',
  'map.baidu.com',
  'pv.sohu.com',
  'mt0.google.cn',
  'mt1.google.cn',
  'mt2.google.cn',
  'mt3.google.cn',
  'hm.baidu.com',
  'https://maponline0.bdimg.com',
  'https://maponline1.bdimg.com',
  'https://maponline2.bdimg.com',
  'https://maponline3.bdimg.com',
  'https://api.map.baidu.com/getscript',
];
邓晓峰's avatar
邓晓峰 committed
59
export const initMicroApps = loader => {
邓晓峰's avatar
邓晓峰 committed
60 61 62
  const entrys =
    process.env.NODE_ENV !== 'production' ? micorConfig.dev : micorConfig.prod;

邓晓峰's avatar
邓晓峰 committed
63
  registerMicroApps(
邓晓峰's avatar
邓晓峰 committed
64 65 66 67 68
    entrys.map(item => {
      item.loader = loader;
      item.props = {
        emitter: window.share.event,
        baseRoot: item.name,
邓晓峰's avatar
邓晓峰 committed
69
        // eslint-disable-next-line no-undef
70
        globalConfig: createStoreage.get('globalConfig'),
邓晓峰's avatar
邓晓峰 committed
71 72 73 74
        XMLHttpRequest: window.XMLHttpRequest,
      };
      return item;
    }),
邓晓峰's avatar
邓晓峰 committed
75 76 77
    {
      beforeLoad: [
        app => {
邓晓峰's avatar
邓晓峰 committed
78
          Logger.info('[LifeCycle] before load %c%s');
邓晓峰's avatar
邓晓峰 committed
79 80 81 82
        },
      ],
      beforeMount: [
        app => {
邓晓峰's avatar
邓晓峰 committed
83
          Logger.info(`[LifeCycle] before mount %c%s ${app.name}`);
邓晓峰's avatar
邓晓峰 committed
84 85
        },
      ],
邓晓峰's avatar
邓晓峰 committed
86
      afterMount: [
邓晓峰's avatar
邓晓峰 committed
87
        app => {
邓晓峰's avatar
邓晓峰 committed
88
          Logger.info(`[LifeCycle] after mount %c%s ${app.name}`);
邓晓峰's avatar
邓晓峰 committed
89 90
        },
      ],
邓晓峰's avatar
邓晓峰 committed
91
      beforeUnmount: [
邓晓峰's avatar
邓晓峰 committed
92
        app => {
邓晓峰's avatar
邓晓峰 committed
93
          window.share.event.removeAllListeners('changeRoute');
邓晓峰's avatar
邓晓峰 committed
94
          Logger.info(`[LifeCycle] after unmount %c%s ${app.name}`);
邓晓峰's avatar
邓晓峰 committed
95 96
        },
      ],
邓晓峰's avatar
邓晓峰 committed
97
      afterUnmount: [app => {}],
邓晓峰's avatar
邓晓峰 committed
98 99
    },
  );
100
  const { setGlobalState } = initGlobalState({
邓晓峰's avatar
邓晓峰 committed
101 102
    // eslint-disable-next-line no-undef
    globalConfig: createStoreage.get('globalConfig'),
103 104
  });
  setGlobalState({
邓晓峰's avatar
邓晓峰 committed
105 106 107
    // eslint-disable-next-line no-undef
    globalConfig: createStoreage.get('globalConfig'),
  });
邓晓峰's avatar
邓晓峰 committed
108 109 110 111 112
  start({
    sandbox: {
      experimentalStyleIsolation: true,
      loose: true,
    },
邓晓峰's avatar
邓晓峰 committed
113
    singular: true,
邓晓峰's avatar
邓晓峰 committed
114
    scopedCSS: true,
邓晓峰's avatar
邓晓峰 committed
115
    // prefetch: 'all',
邓晓峰's avatar
邓晓峰 committed
116
    // eslint-disable-next-line no-underscore-dangle
邓晓峰's avatar
邓晓峰 committed
117
    getPublicPath: window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__,
118
    excludeAssetFilter: url => excludeAssetFilter.includes(url),
邓晓峰's avatar
邓晓峰 committed
119 120
  });
  runAfterFirstMounted(() => {
邓晓峰's avatar
邓晓峰 committed
121
    Logger.info('[MainApp] first app mounted');
邓晓峰's avatar
邓晓峰 committed
122
  });
邓晓峰's avatar
邓晓峰 committed
123
  // eslint-disable-next-line no-use-before-define
邓晓峰's avatar
邓晓峰 committed
124
  defaultApp();
邓晓峰's avatar
邓晓峰 committed
125
  addGlobalUncaughtErrorHandler(event => {
邓晓峰's avatar
邓晓峰 committed
126
    const { error } = event;
邓晓峰's avatar
邓晓峰 committed
127
    const reson = 'Failed to fetch';
邓晓峰's avatar
邓晓峰 committed
128 129 130 131 132
    if (
      error &&
      error.message.indexOf(MICRO_STATUS.LOADING_SOURCE_CODE) &&
      error.message.indexOf(reson)
    ) {
邓晓峰's avatar
邓晓峰 committed
133
      // window.history.pushState({message: '应用服务请求异常,请检查应用配置'}, null, '/civbase/500')
邓晓峰's avatar
邓晓峰 committed
134 135
    }
  });
邓晓峰's avatar
邓晓峰 committed
136 137 138
};

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