山河好大 發表於 2019-7-15 18:05:00

前端微服务+React解决方案。

<p>&nbsp; &nbsp; 因为项目有很多互不依赖的模块,但每次发版却要一次打包都发上去,所以项目组决定进行分模块发版,看了一篇微服务前端的解决方案,还不错,但是还是不那么全面,试着用了一下,并且发布了一下,没什么太大问题,可能需要继续优化一下,简单介绍一下。</p>
<p>&nbsp; &nbsp;首先就是搭建主要的架构:</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; 1.webpack.config.js的初始化</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</p>
<div class="cnblogs_code">
<pre>const path = require('path'<span style="color: rgba(0, 0, 0, 1)">);
const CleanWebpackPlugin </span>= require('clean-webpack-plugin'<span style="color: rgba(0, 0, 0, 1)">);
const CopyWebpackPlugin </span>= require('copy-webpack-plugin'<span style="color: rgba(0, 0, 0, 1)">);
const WebpackBar </span>= require('webpackbar'<span style="color: rgba(0, 0, 0, 1)">);
const autoprefixer </span>= require('autoprefixer'<span style="color: rgba(0, 0, 0, 1)">)
const { resolve } </span>=<span style="color: rgba(0, 0, 0, 1)"> path;
module.exports </span>=<span style="color: rgba(0, 0, 0, 1)"> {
    devtool: </span>'source-map'<span style="color: rgba(0, 0, 0, 1)">,
    entry: path.resolve(__dirname, </span>'../src/index.js'<span style="color: rgba(0, 0, 0, 1)">),
    output: {
      filename: </span>'output.js'<span style="color: rgba(0, 0, 0, 1)">,
      library: </span>'output'<span style="color: rgba(0, 0, 0, 1)">,
      libraryTarget: </span>'amd'<span style="color: rgba(0, 0, 0, 1)">,
      path: resolve(__dirname, </span>'../public'<span style="color: rgba(0, 0, 0, 1)">)
    },
    mode: </span>'production'<span style="color: rgba(0, 0, 0, 1)">,
    externals: {
      react: </span>'React'<span style="color: rgba(0, 0, 0, 1)">,
      </span>'react-dom': 'ReactDOM'<span style="color: rgba(0, 0, 0, 1)">,
      jquery: </span>'jQuery'<span style="color: rgba(0, 0, 0, 1)">
    },
    module: {
      rules: [
            { parser: { System: </span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)"> } },
            {
                test: </span>/\.js?$/<span style="color: rgba(0, 0, 0, 1)">,
                exclude: ,
                loader: </span>'babel-loader'<span style="color: rgba(0, 0, 0, 1)">,
            },
            {
                test: </span>/\.less$/<span style="color: rgba(0, 0, 0, 1)">,
                use: [
                  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">MiniCssExtractPlugin.loader,</span>
<span style="color: rgba(0, 0, 0, 1)">                  {
                        loader: </span>'css-loader'<span style="color: rgba(0, 0, 0, 1)">,
                        options: {
                            sourceMap: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
                        },
                  },
                  {
                        loader: </span>'postcss-loader'<span style="color: rgba(0, 0, 0, 1)">,
                        options: Object.assign({}, autoprefixer({ overrideBrowserslist: [</span>'last 2 versions', 'Firefox ESR', '&gt; 1%', 'ie &gt;= 9'] }), { sourceMap: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)"> }),
                  },
                  {
                        loader: </span>'less-loader'<span style="color: rgba(0, 0, 0, 1)">,
                        options: {
                            javascriptEnabled: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
                            sourceMap: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
                        },
                  },
                ],
            },
            {
                test: </span>/\.css$/<span style="color: rgba(0, 0, 0, 1)">,
                exclude: ,
                use: [
                  </span>'style-loader'<span style="color: rgba(0, 0, 0, 1)">,
                  {
                        loader: </span>'css-loader'<span style="color: rgba(0, 0, 0, 1)">,
                        options: {
                            localIdentName: </span>'__'<span style="color: rgba(0, 0, 0, 1)">,
                        },
                  },
                  {
                        loader: </span>'postcss-loader'<span style="color: rgba(0, 0, 0, 1)">,
                        options: {
                            plugins() {
                              </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> [
                                    require(</span>'autoprefixer'<span style="color: rgba(0, 0, 0, 1)">)
                              ];
                            },
                        },
                  },
                ],
            },
            {
                test: </span>/\.(gif|jpg|png|woff|svg|eot|ttf)\??.*$/<span style="color: rgba(0, 0, 0, 1)">,
                loader: </span>'url-loader?limit=8192&amp;name=images/..'<span style="color: rgba(0, 0, 0, 1)">
            }
      ],
    },
    resolve: {
      modules: [
            __dirname, </span>'node_modules'<span style="color: rgba(0, 0, 0, 1)">
      ],
    },
    plugins: [
      </span><span style="color: rgba(0, 0, 255, 1)">new</span> CleanWebpackPlugin(['build'], { root: path.resolve(__dirname, '../'<span style="color: rgba(0, 0, 0, 1)">) }),
      CopyWebpackPlugin([{ from: path.resolve(__dirname, </span>'../public/index.html'<span style="color: rgba(0, 0, 0, 1)">) }]),
      </span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> WebpackBar({
            name: </span>'🐶 主模块:'<span style="color: rgba(0, 0, 0, 1)">,
            color: </span>'#2f54eb'<span style="color: rgba(0, 0, 0, 1)">,
      })
    ]
}</span></pre>
</div>
<p>配置基本上一样,主要是出口那里要选择amd。</p>
<p>下面配置开发和上产两套启动方式:</p>
<p>&nbsp; &nbsp; &nbsp; 开发环境:</p>
<p>&nbsp; &nbsp; &nbsp;&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> eslint-env node </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
const config </span>= require('./webpack.config.js'<span style="color: rgba(0, 0, 0, 1)">);
const clearConsole </span>= require('react-dev-utils/clearConsole'<span style="color: rgba(0, 0, 0, 1)">);
const WebpackDevServer </span>= require('webpack-dev-server'<span style="color: rgba(0, 0, 0, 1)">);
const webpack </span>= require('webpack'<span style="color: rgba(0, 0, 0, 1)">);
const path </span>= require('path'<span style="color: rgba(0, 0, 0, 1)">);
config.mode </span>= 'development'<span style="color: rgba(0, 0, 0, 1)">;

config.plugins.push(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> webpack.NamedModulesPlugin());
config.plugins.push(</span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> webpack.HotModuleReplacementPlugin());

const webpackConfig </span>=<span style="color: rgba(0, 0, 0, 1)"> webpack(config);
const devServer </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> WebpackDevServer(webpackConfig, {
    </span><span style="color: rgba(0, 0, 0, 1)">
    contentBase: path.resolve(__dirname, </span>'../build'<span style="color: rgba(0, 0, 0, 1)">),
    compress: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
    port: </span>3000<span style="color: rgba(0, 0, 0, 1)">,
    stats:{
      warnings: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
      errors: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
      children:</span><span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">
    },
    historyApiFallback:</span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
    clientLogLevel: </span>'none'<span style="color: rgba(0, 0, 0, 1)">,
    proxy: {
      </span>'/'<span style="color: rgba(0, 0, 0, 1)">: {
            header: { </span>"Access-Control-Allow-Origin": "*"<span style="color: rgba(0, 0, 0, 1)"> },
            target:</span>'http://srvbot-core-gat-bx-stg1-padis.paic.com.cn',<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">'http://srvbot-dev.szd-caas.paic.com.cn', </span>
            changeOrigin: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
            bypass: </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (req) {
                </span><span style="color: rgba(0, 0, 255, 1)">if</span> (/\.(gif|jpg|png|woff|svg|eot|ttf|js|jsx|json|css|pdf)$/<span style="color: rgba(0, 0, 0, 1)">.test(req.url)) {
                  </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> req.url;
                }
            }
      }
    }
});

devServer.listen(</span>3000, process.env.HOST || '0.0.0.0', (err) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (err) {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> console.log(err);
    }
    clearConsole();
});</span></pre>
</div>
<p>生产环境:</p>
<p>&nbsp; &nbsp; &nbsp;&nbsp;</p>
<div class="cnblogs_code">
<pre>process.env.NODE_ENV = 'production'<span style="color: rgba(0, 0, 0, 1)">;
process.env.BABEL_ENV </span>= 'production'<span style="color: rgba(0, 0, 0, 1)">;


const webpack </span>= require('webpack'<span style="color: rgba(0, 0, 0, 1)">);
const path </span>= require('path'<span style="color: rgba(0, 0, 0, 1)">);
const chalk </span>= require('chalk'<span style="color: rgba(0, 0, 0, 1)">);
const webpackConfig </span>= require('./webpack.config'<span style="color: rgba(0, 0, 0, 1)">);
const util </span>= require('./util'<span style="color: rgba(0, 0, 0, 1)">);
webpackConfig.mode </span>= 'production'<span style="color: rgba(0, 0, 0, 1)">;
const {emptyFolder,createFolder,copyFolder,notice,isCurrentTime,copyFile} </span>=<span style="color: rgba(0, 0, 0, 1)"> util;

createFolder();
emptyFolder(</span>'../build'<span style="color: rgba(0, 0, 0, 1)">)

webpack(webpackConfig).run((err, options) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (err) {
      console.error(</span>'错误信息:'<span style="color: rgba(0, 0, 0, 1)">, err);
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
    }
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (err ||<span style="color: rgba(0, 0, 0, 1)"> options.hasErrors()) {
      </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (options.compilation.warnings) {
            options.compilation.warnings.forEach(item </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                console.log(chalk.green(</span>'⚠️ 警告:', item.message.replace('Module Warning (from ./node_modules/happypack/loader.js):','').replace('(Emitted value instead of an instance of Error)','')), '\n'<span style="color: rgba(0, 0, 0, 1)">);
            })
      }
      console.log(chalk.red(</span>'❌ 错误信息:'<span style="color: rgba(0, 0, 0, 1)">));
      console.log(chalk.yellow(options.compilation.errors[</span>0].error.message.replace('(Emitted value instead of an instance of Error)','')), '\n'<span style="color: rgba(0, 0, 0, 1)">);
      notice(</span>'⚠️ 警告:' + options.compilation.errors.error.message)
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
    }
   
    copyFolder(path.resolve(__dirname, </span>'../public'), path.resolve(__dirname, '../build'<span style="color: rgba(0, 0, 0, 1)">));
    </span><span style="color: rgba(0, 0, 0, 1)">

    const { startTime, endTime } </span>=<span style="color: rgba(0, 0, 0, 1)"> options;
    const times </span>= (endTime - startTime) / 1e3 / 60<span style="color: rgba(0, 0, 0, 1)">;
    console.log(chalk.bgGreen(</span>'开始时间:', isCurrentTime(<span style="color: rgba(0, 0, 255, 1)">new</span> Date(startTime))), '\n'<span style="color: rgba(0, 0, 0, 1)">);
    console.log(chalk.bgGreen(</span>'结束时间:', isCurrentTime(<span style="color: rgba(0, 0, 255, 1)">new</span> Date(endTime))), '\n'<span style="color: rgba(0, 0, 0, 1)">);
    console.log(chalk.yellowBright(</span>'总共用时:', `${parseFloat(times).toFixed(2)}分钟`), '\n'<span style="color: rgba(0, 0, 0, 1)">);
})</span></pre>
</div>
<p>这里是打包完成后,将打包过后的放进build文件夹。顺便贴一下node的文件夹方法,拿起即用:</p>
<div class="cnblogs_code">
<pre>const notifier = require('node-notifier'<span style="color: rgba(0, 0, 0, 1)">);
const fs </span>= require('fs'<span style="color: rgba(0, 0, 0, 1)">);
const fe </span>= require('fs-extra'<span style="color: rgba(0, 0, 0, 1)">);
const path </span>= require('path'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* Author:zhanglei185
*
* @param {String} str
* @returns {void}
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> emptyFolder (str){
    fe.emptyDirSync(path.resolve(__dirname, str))
}

</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* Author:zhanglei185
*
* @param {String} message
* @returns {void}
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> notice(message) {
    notifier.notify({
      title: </span>'ServiceBot'<span style="color: rgba(0, 0, 0, 1)">,
      message,
      icon: path.join(__dirname, </span>'../public/img/8.jpg'<span style="color: rgba(0, 0, 0, 1)">),
      sound: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
      wait: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">
    });
}

notifier.on(</span>'click', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (notifierObject, options) {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Triggers if `wait: true` and user clicks notification</span>
<span style="color: rgba(0, 0, 0, 1)">});

notifier.on(</span>'timeout', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (notifierObject, options) {
    notice()
});

</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* Author:zhanglei185
*
* @param {String} src
* @param {String} tar
* @returns {void}
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> copyFolder(src, tar) {
    fs.readdirSync(src).forEach(path </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      const newSrc </span>= `${src}/${path}`;
      const newTar = `${tar}/${path}`
      const st =<span style="color: rgba(0, 0, 0, 1)"> fs.statSync(newSrc);
      console.log(newTar)
      </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (st.isDirectory()) {
            fs.mkdirSync(newTar)
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> copyFolder(newSrc, newTar)
      }
      </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (st.isFile()) {
            fs.writeFileSync(newTar, fs.readFileSync(newSrc))
      }
    })
}

</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* Author:zhanglei185
*
* @returns {void}
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> createFolder() {
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!fs.existsSync(path.resolve(__dirname, '../build'<span style="color: rgba(0, 0, 0, 1)">))) {
      fs.mkdirSync(path.resolve(__dirname, </span>'../build'<span style="color: rgba(0, 0, 0, 1)">))
    }
}

</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">*
* Author:zhanglei185
*
* @param {Date} time
* @returns {void}
</span><span style="color: rgba(0, 128, 0, 1)">*/</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> isCurrentTime(time) {
    const y </span>=<span style="color: rgba(0, 0, 0, 1)"> time.getFullYear();
    const month </span>= time.getMonth() + 1<span style="color: rgba(0, 0, 0, 1)">;
    const hour </span>=<span style="color: rgba(0, 0, 0, 1)"> time.getHours();
    const min </span>=<span style="color: rgba(0, 0, 0, 1)"> time.getMinutes();
    const sec </span>=<span style="color: rgba(0, 0, 0, 1)"> time.getSeconds();
    const day </span>=<span style="color: rgba(0, 0, 0, 1)"> time.getDate();
    const m </span>= month &lt; 10 ? `0<span style="color: rgba(0, 0, 0, 1)">${month}` : month;
    const h </span>= hour &lt; 10 ? `0<span style="color: rgba(0, 0, 0, 1)">${hour}` : hour;
    const mins </span>= min &lt; 10 ? `0<span style="color: rgba(0, 0, 0, 1)">${min}` : min;
    const s </span>= sec &lt; 10 ? `0<span style="color: rgba(0, 0, 0, 1)">${sec}` : sec;
    const d </span>= day &lt; 10 ? `0<span style="color: rgba(0, 0, 0, 1)">${day}` : day;
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> `${y}-${m}-<span style="color: rgba(0, 0, 0, 1)">${d} ${h}:${mins}:${s}`
}

module.exports</span>=<span style="color: rgba(0, 0, 0, 1)">{
    isCurrentTime,
    emptyFolder,
    createFolder,
    copyFolder,
    notice,
}</span></pre>
</div>
<p>2.接下来经过运行上面的开发环境,会生成一个output.js。现在增加一个html页面用来加载js</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;!</span><span style="color: rgba(255, 0, 255, 1)">DOCTYPE html</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">html</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">head</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">meta </span><span style="color: rgba(255, 0, 0, 1)">charset</span><span style="color: rgba(0, 0, 255, 1)">="utf-8"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">meta </span><span style="color: rgba(255, 0, 0, 1)">name</span><span style="color: rgba(0, 0, 255, 1)">="viewport"</span><span style="color: rgba(255, 0, 0, 1)"> content</span><span style="color: rgba(0, 0, 255, 1)">="width=device-width"</span> <span style="color: rgba(0, 0, 255, 1)">/&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">title</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>Servicebot<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">title</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">link </span><span style="color: rgba(255, 0, 0, 1)">rel</span><span style="color: rgba(0, 0, 255, 1)">="stylesheet"</span><span style="color: rgba(255, 0, 0, 1)"> href</span><span style="color: rgba(0, 0, 255, 1)">="./css/antd.css"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">head</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">body</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">div </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="root"</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">div</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">div </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="login"</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">div</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">div </span><span style="color: rgba(255, 0, 0, 1)">id</span><span style="color: rgba(0, 0, 255, 1)">="base"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
    <span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">divid</span><span style="color: rgba(0, 0, 255, 1)">="uioc"</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">div</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">div</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">script </span><span style="color: rgba(255, 0, 0, 1)">type</span><span style="color: rgba(0, 0, 255, 1)">="systemjs-importmap"</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">
      {</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">imports</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">: {
      </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">output!sofe</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">: </span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">./output.js</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">"</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">,
      }}
</span><span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">script</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 128, 0, 1)">&lt;!--</span><span style="color: rgba(0, 128, 0, 1)"> &lt;script src='./react-dom.production.min.js'&gt;&lt;/script&gt;
&lt;script src='./react.production.min.js'&gt;&lt;/script&gt; </span><span style="color: rgba(0, 128, 0, 1)">--&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">script </span><span style="color: rgba(255, 0, 0, 1)">src</span><span style="color: rgba(0, 0, 255, 1)">='./common/system.js'</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">script</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">script </span><span style="color: rgba(255, 0, 0, 1)">src</span><span style="color: rgba(0, 0, 255, 1)">='./common/amd.js'</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">script</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">script </span><span style="color: rgba(255, 0, 0, 1)">src</span><span style="color: rgba(0, 0, 255, 1)">='./common/named-exports.js'</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">script</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">script </span><span style="color: rgba(255, 0, 0, 1)">src</span><span style="color: rgba(0, 0, 255, 1)">="./common/common-deps.js"</span><span style="color: rgba(0, 0, 255, 1)">&gt;&lt;/</span><span style="color: rgba(128, 0, 0, 1)">script</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;</span><span style="color: rgba(128, 0, 0, 1)">script</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">
    System.import(</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">'</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">output!sofe</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">'</span><span style="background-color: rgba(245, 245, 245, 1); color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">script</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>
<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">body</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span>

<span style="color: rgba(0, 0, 255, 1)">&lt;/</span><span style="color: rgba(128, 0, 0, 1)">html</span><span style="color: rgba(0, 0, 255, 1)">&gt;</span></pre>
</div>
<p>主要是引入打包过后的js,摒弃引入几个必要的js。那么主要模块启动就完成了。</p>
<p>3.现在开发每个单独模块</p>
<p>&nbsp; webpack的搭建,仿照上面的就可以,但是端口号需要切换成不同的以方便,主模块加载各个模块的js,另外还需要将代理设置为跨域的,不然是不允许访问的</p>
<pre><span>headers: { "Access-Control-Allow-Origin": "*"<span> },</span></span><br>出口换成不同名,比如单独打包了登陆,那么出口为login.js。<br><br>那么我们在主模块怎么加载这个js呢<br><br>我们知道,主模块的入口文件是index.js<br><br>那么我们看一下这个index.js都做了什么</pre>
<div class="cnblogs_code">
<pre>import * as isActive from './activityFns'<span style="color: rgba(0, 0, 0, 1)">
import </span>* as singleSpa from 'single-spa'<span style="color: rgba(0, 0, 0, 1)">
import { registerApp } from </span>'./Register'<span style="color: rgba(0, 0, 0, 1)">
import { projects } from </span>'./project.config'<span style="color: rgba(0, 0, 0, 1)">
const env </span>=<span style="color: rgba(0, 0, 0, 1)"> process.env.NODE_ENV;

</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> domElementGetterCss({name,host}) {
    const getEnv </span>= (env) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (env === 'development'<span style="color: rgba(0, 0, 0, 1)">) {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> `http:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">localhost:${host}/${name}.css`</span>
      } <span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span> (env === 'production'<span style="color: rgba(0, 0, 0, 1)">) {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span> `./css/<span style="color: rgba(0, 0, 0, 1)">${name}.css`
      }
    }
    let el </span>= document.getElementsByTagName("head");
    const link </span>= document.createElement('link'<span style="color: rgba(0, 0, 0, 1)">);
    link.rel </span>= "stylesheet"<span style="color: rgba(0, 0, 0, 1)">
    link.href </span>=<span style="color: rgba(0, 0, 0, 1)"> getEnv(env)
    el.appendChild(link);
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> el
}

</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> createCss(){
    const arr </span>=<span style="color: rgba(0, 0, 0, 1)"> [
      {
            name:</span>'login'<span style="color: rgba(0, 0, 0, 1)">,
            host:</span>3100<span style="color: rgba(0, 0, 0, 1)">,
      },
      {
            name:</span>'base'<span style="color: rgba(0, 0, 0, 1)">,
            host:</span>3200<span style="color: rgba(0, 0, 0, 1)">,
      },
      {
            name:</span>'uioc'<span style="color: rgba(0, 0, 0, 1)">,
            host:</span>3300<span style="color: rgba(0, 0, 0, 1)">,
      }
    ]

    arr.forEach(item </span>=&gt;<span style="color: rgba(0, 0, 0, 1)">{
      domElementGetterCss(item)
    })
}



async </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> bootstrap() {
    createCss()
    const SystemJS </span>=<span style="color: rgba(0, 0, 0, 1)"> window.System;
</span>
    projects.forEach(element =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
      registerApp({
            name: element.name,
            main: element.main,
            url: element.prefix,
            store: element.store,
            base: element.base,
            path: element.path
      });
    });

    singleSpa.start();
}

bootstrap()



</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">singleSpa.start()</span></pre>
</div>
<p>里边有system和spa两个js的方法,我们在bootstarp这个方法里 加入不同服务下的css和js。</p>
<pre>project.config.js</pre>
<div class="cnblogs_code">
<pre>const env =<span style="color: rgba(0, 0, 0, 1)"> process.env.NODE_ENV;
console.log(env)
const getEnv </span>= (name,env) =&gt;<span style="color: rgba(0, 0, 0, 1)">{
    </span><span style="color: rgba(0, 0, 255, 1)">if</span>(env === 'development'<span style="color: rgba(0, 0, 0, 1)">){
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> `http:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">localhost:${host(name)}/${name}.js` </span>
    }<span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span>(env === 'production'<span style="color: rgba(0, 0, 0, 1)">){
      console.log(env)
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> `./js/<span style="color: rgba(0, 0, 0, 1)">${name}.js`
    }
}

</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> host(name){
    </span><span style="color: rgba(0, 0, 255, 1)">switch</span><span style="color: rgba(0, 0, 0, 1)">(name){
      </span><span style="color: rgba(0, 0, 255, 1)">case</span>'login':<span style="color: rgba(0, 0, 255, 1)">return</span> '3100'<span style="color: rgba(0, 0, 0, 1)">;
      </span><span style="color: rgba(0, 0, 255, 1)">case</span>'base':<span style="color: rgba(0, 0, 255, 1)">return</span> '3200'<span style="color: rgba(0, 0, 0, 1)">;
      </span><span style="color: rgba(0, 0, 255, 1)">case</span>'uioc':<span style="color: rgba(0, 0, 255, 1)">return</span> '3300'<span style="color: rgba(0, 0, 0, 1)">;
    }
}

export const projects </span>=<span style="color: rgba(0, 0, 0, 1)"> [
    {
      </span>"name": "login", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">模块名称</span>
      "path": "/", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">模块url前缀</span>
      "store": getEnv('login',env),<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">模块对外接口</span>
<span style="color: rgba(0, 0, 0, 1)">    },
    {
      </span>"name": "base", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">模块名称</span>
      "path": "/app", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">模块url前缀</span>
      "store": getEnv('base',env),<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">模块对外接口</span>
<span style="color: rgba(0, 0, 0, 1)">    },
    {
      </span>"name": "uioc", <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">模块名称</span>
      "path": ["/app/uiocmanage/newuioc","/app/uiocmanage/myuioc","/app/uiocmanage/alluioc"], <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">模块url前缀</span>
      "store": getEnv('uioc',env),<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">模块对外接口</span>
<span style="color: rgba(0, 0, 0, 1)">    },
]</span></pre>
</div>
<p>引入js和css都需要判断当前的环境,因为生产环境不需要本地服务</p>
<p>registry.js</p>
<div class="cnblogs_code">
<pre>import * as singleSpa from 'single-spa'<span style="color: rgba(0, 0, 0, 1)">;

import { GlobalEventDistributor } from </span>'./GlobalEventDistributor'<span style="color: rgba(0, 0, 0, 1)">
const globalEventDistributor </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> GlobalEventDistributor();
const SystemJS </span>=<span style="color: rgba(0, 0, 0, 1)"> window.System
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 应用注册</span>
const arr =<span style="color: rgba(0, 0, 0, 1)"> [];
export async </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> registerApp(params) {
    let storeModule </span>= {}, customProps =<span style="color: rgba(0, 0, 0, 1)"> { globalEventDistributor: globalEventDistributor };
    </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
      storeModule </span>= params.store ? await SystemJS.import(params.store) : { storeInstance: <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)"> };
    } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (e) {
      console.log(`Could not load store of app ${params.name}.`, e);
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">
    }

    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (storeModule.storeInstance &amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> globalEventDistributor) {
      customProps.store </span>=<span style="color: rgba(0, 0, 0, 1)"> storeModule.storeInstance;
      globalEventDistributor.registerStore(storeModule.storeInstance);
    }
   
    customProps </span>=<span style="color: rgba(0, 0, 0, 1)"> {
      store: storeModule,
      globalEventDistributor: globalEventDistributor
    };
    window.globalEventDistributor </span>=<span style="color: rgba(0, 0, 0, 1)"> globalEventDistributor
    singleSpa.registerApplication(
      params.name,
      () </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> SystemJS.import(params.store),
      (pathPrefix(params)),
      customProps
    );
}


</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> pathPrefix(params) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
      let hash </span>= window.location.hash.replace('#', ''<span style="color: rgba(0, 0, 0, 1)">);
      let isShow </span>= <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!(hash.startsWith('/'<span style="color: rgba(0, 0, 0, 1)">))) {
            hash </span>= `/${hash}`
<span style="color: rgba(0, 0, 0, 1)">      }
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">多个地址共用的情况</span>
      <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (isArray(params.path)) {
            isShow </span>= params.path.some(element =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                </span><span style="color: rgba(0, 0, 255, 1)">return</span> element!=='/app' &amp;&amp;<span style="color: rgba(0, 0, 0, 1)">hash.includes(element)
            });
      }
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (hash ===<span style="color: rgba(0, 0, 0, 1)"> params.path) {
            isShow </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">
      }
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (params.name === 'base' &amp;&amp; hash !== '/'<span style="color: rgba(0, 0, 0, 1)">) {
            isShow </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">
      }
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log('【localtion.hash】: ', hash)</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log('【params.path】: ', params.path)</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log('【isShow】: ', isShow)</span>
      <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log(' ')</span>
      <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> isShow;
    }
}

</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> isArray(arr) {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> Object.prototype.toString.call(arr) === ""<span style="color: rgba(0, 0, 0, 1)">
}</span></pre>
</div>
<p>在将每一个模块注册进的时候,将路由用到的history也注入。并且将redux注册为全局的,</p>
<p>不知道其他人怎么用的,不过我用了一个方法就是替换原来的connect,从新包装一个:</p>
<div class="cnblogs_code">
<pre>import * as React from 'react'<span style="color: rgba(0, 0, 0, 1)">

export </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> connect(fnState, dispatch) {
    const getGlobal </span>=<span style="color: rgba(0, 0, 0, 1)"> window.globalEventDistributor.getState();
    const obj </span>=<span style="color: rgba(0, 0, 0, 1)"> {
      login: getGlobal.login </span>&amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> getGlobal.login.user,
      sider: {},
      serviceCatalog: {}
    }
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取</span>
    const app =<span style="color: rgba(0, 0, 0, 1)"> fnState(obj)
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">发送</span>
    const disProps = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">typeof</span> dispatch === 'function' &amp;&amp;<span style="color: rgba(0, 0, 0, 1)"> dispatch.call(getGlobal.dispatch,getGlobal.dispatch);
    }

    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (WrappedComponent) {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> class UserUM extends React.Component {</span><span style="color: rgba(0, 0, 0, 1)">
            render() {
                </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
                  </span>&lt;WrappedComponent {...disProps()} {...app} {...<span style="color: rgba(0, 0, 255, 1)">this</span>.props} /&gt;
<span style="color: rgba(0, 0, 0, 1)">                )
            }
      }
    }
} </span></pre>
</div>
<p>通过全局,先拿到每个模块的 storeInstance,通过全局获取到,然后写一个高阶组件包含两个方法state,和dispatch,以保持connect原样,以方便不要修改太多地方。</p>
<p>然后通过props传递到组件内部,组件依然可以像原来一样拿到state和方法。</p>
<p>4.每个模块需要一个单独的入口文件</p>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">
import ReactDOM from </span>'react-dom'<span style="color: rgba(0, 0, 0, 1)">
import singleSpaReact from </span>'single-spa-react'<span style="color: rgba(0, 0, 0, 1)">
import { Route, Switch, HashRouter } from </span>'react-router-dom'<span style="color: rgba(0, 0, 0, 1)">;
import { LocaleProvider } from </span>'antd'<span style="color: rgba(0, 0, 0, 1)">;
import zh_CN from </span>'antd/lib/locale-provider/zh_CN'<span style="color: rgba(0, 0, 0, 1)">;
import { Provider } from </span>'react-redux'<span style="color: rgba(0, 0, 0, 1)">
const createHistory </span>= require("history"<span style="color: rgba(0, 0, 0, 1)">).createHashHistory
const history </span>=<span style="color: rgba(0, 0, 0, 1)"> createHistory()


import NewUioc from </span>'../src/uiocManage/startUioc'<span style="color: rgba(0, 0, 0, 1)">
import MyUioc from </span>'../src/uiocManage/myUioc/myuioc.js'<span style="color: rgba(0, 0, 0, 1)">
import AllUioc from </span>'../src/uiocManage/allUioc/alluioc.js'<span style="color: rgba(0, 0, 0, 1)">
import UiocTicket from </span>'../src/uiocManage/components'<span style="color: rgba(0, 0, 0, 1)">
import UiocTaskTicket from </span>'../src/uiocManage/components/taskTicket.js'<span style="color: rgba(0, 0, 0, 1)">
import NewUiocIt from </span>'../src/itManage/startUioc'<span style="color: rgba(0, 0, 0, 1)">


const reactLifecycles </span>=<span style="color: rgba(0, 0, 0, 1)"> singleSpaReact({
React,
ReactDOM,
rootComponent: (spa) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;Provider store={spa.store.storeInstance} globalEventDistributor={spa.globalEventDistributor}&gt;
      &lt;HashRouter history={spa.store.history}&gt;
          &lt;Switch&gt;
            &lt;Route exact path="/app/uiocmanage/newuioc" component={NewUioc} /&gt;
            &lt;Route exact path="/app/uiocmanage/myuioc" component={MyUioc} /&gt;
            &lt;Route exact path="/app/uiocmanage/alluioc" component={AllUioc} /&gt;
            &lt;Route exact path="/app/uiocmanage/alluioc/:ticketId" component={UiocTicket} /&gt;
            &lt;Route exact path="/app/uiocmanage/alluioc/:ticketId/:taskId" component={UiocTaskTicket} /&gt;
          &lt;/Switch&gt;
      &lt;/HashRouter&gt;
      &lt;/Provider&gt;
<span style="color: rgba(0, 0, 0, 1)">    )
},
domElementGetter
})

export const bootstrap </span>=<span style="color: rgba(0, 0, 0, 1)"> [
reactLifecycles.bootstrap,
]

export const mount </span>=<span style="color: rgba(0, 0, 0, 1)"> [
reactLifecycles.mount,
]

export const unmount </span>=<span style="color: rgba(0, 0, 0, 1)"> [
reactLifecycles.unmount,
]

export const unload </span>=<span style="color: rgba(0, 0, 0, 1)"> [
reactLifecycles.unload,
]

</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> domElementGetter() {
let el </span>= document.getElementById("uioc"<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">el) {
    el </span>= document.createElement('div'<span style="color: rgba(0, 0, 0, 1)">);
    el.id </span>= 'uioc'<span style="color: rgba(0, 0, 0, 1)">;
    document.getElementById(</span>'base').querySelector('.zl-myContent'<span style="color: rgba(0, 0, 0, 1)">).appendChild(el);
}

</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> el;
}

import { createStore, combineReducers } from </span>'redux'<span style="color: rgba(0, 0, 0, 1)">

const initialState </span>=<span style="color: rgba(0, 0, 0, 1)"> {
refresh: </span>20<span style="color: rgba(0, 0, 0, 1)">
}

</span><span style="color: rgba(0, 0, 255, 1)">function</span> render(state =<span style="color: rgba(0, 0, 0, 1)"> initialState, action) {
</span><span style="color: rgba(0, 0, 255, 1)">switch</span><span style="color: rgba(0, 0, 0, 1)"> (action.type) {
    </span><span style="color: rgba(0, 0, 255, 1)">case</span> 'REFRESH'<span style="color: rgba(0, 0, 0, 1)">:
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
      ...state,
      refresh: state.refresh </span>+ 1<span style="color: rgba(0, 0, 0, 1)">
      }
    </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)">:
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> state
}
}

export const storeInstance </span>= createStore(combineReducers({ namespace: () =&gt; 'uioc'<span style="color: rgba(0, 0, 0, 1)">, render, history }))
export { history }</span></pre>
</div>
<p>在这个页面需要生成一个id ,去渲染这个模块的js,并且将这个模块的storeInstance传出,一个单独的模块就打包完了。</p>
<p>完事之后,在单独模块打包完成后需要将这个模块的js和css复制到主模块的build文件夹相应的位置,这样,直接全部发布的时候不需要再自己移动。</p>
<div>
<div>copyFile(path.resolve(__dirname, '../build/uioc.js'), path.resolve(__dirname, '../../../../build/js/uioc.js'));</div>
<div>copyFile(path.resolve(__dirname, '../build/uioc.css'), path.resolve(__dirname, '../../../../build/css/uioc.css'));</div>
<div>之后打包出来的样子就变成这个样子。</div>
<div>&nbsp;</div>
<div><img src="https://img2018.cnblogs.com/blog/874905/201907/874905-20190715180044140-471320034.png"></div>
</div>
<p>当然,再加上happypack会更快一下打包。之后会将eslint加上,目前发现新版的eslint不支持箭头函数,不知道谁有好的办法,</p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/leijuan/p/11190482.html
頁: [1]
查看完整版本: 前端微服务+React解决方案。