同様の設定が必要でしたが、Angularの設定が必要でした。ここで私が持っているのWebPACKの設定は、(私は関連部分のみを残した)次のとおりです。私の場合は
const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const apps = [
{
name: 'app1',
baseUrl: '/app1'
},
{
name: 'app2',
baseUrl: '/app2'
}
];
module.exports = function (args = {}) {
const isDev = !args.PROD;
const distPath = 'dist';
var config = {};
config.entry = {};
apps.forEach(function (app) {
config.entry[getAppBundleName(app)] = './src/apps/' + app.name + '/main.ts';
});
config.output = {
path: root(distPath),
filename: '[name].js',
chunkFilename: '[name].[chunkhash].js',
publicPath: '/dist/'
};
config.resolve = {
extensions: ['.ts', '.js', '.json'],
modules: [root('src'), root('node_modules')]
};
config.module = {
rules: [
// Add your loaders here
]
};
config.plugins = [
// Add your plugins here
// This enables tree shaking of the vendor modules
new CommonsChunkPlugin({
name: 'vendor',
chunks: ['admin'].concat(apps.map(getAppBundleName)),
minChunks: module => /node_modules/.test(module.resource)
}),
new CommonsChunkPlugin({
name: 'shared',
chunks: ['admin'].concat(apps.map(getAppBundleName)),
minChunks: module => /src(\\|\/)shared/.test(module.resource)
})
];
apps.forEach(function (app) {
var otherApps = apps.slice();
var appItemIndex = otherApps.indexOf(app);
if (appItemIndex > -1) {
otherApps.splice(appItemIndex, 1);
}
config.plugins.push(new HtmlWebpackPlugin({
template: 'index_template.html',
title: app.name,
filename: getAppDevServerHtmlFileName(app),
excludeChunks: otherApps.map(getAppBundleName),
chunksSortMode: 'manual',
chunks: ['vendor', 'shared', getAppBundleName(app)],
inject: 'head',
metadata: {
baseUrl: app.baseUrl
}
}));
});
config.devServer = {
port: 4200,
stats: stats,
historyApiFallback: {
rewrites: apps.map(function (app) {
return {
from: new RegExp('^' + app.baseUrl),
to: '/dist/' + getAppDevServerHtmlFileName(app)
}
}),
},
};
return config;
}
function getAppBundleName(app) {
return app.name;
}
function getAppDevServerHtmlFileName(app) {
return app.name + '_index.html';
}
function root(args) {
args = Array.prototype.slice.call(arguments, 0);
return path.join.apply(path, [__dirname].concat(args));
}`
、私はあなたに似ている、以下のフォルダ構造、持っている:
node_modules
src/
apps/
app1/
...
main.ts
app2/
...
main.ts
shared/
shared-module1/
...
shared-module2/
...
index.ts
...
webpack.config.js
をそしてここにありますdist
フォルダ内のコンパイル後の出力:
dist/
app1.js
app1_index.html
app2.js
app2_index.html
vender.js
shared.js
あなたが見ることができるように、vendor.js
とshared.js
、アプリ間の共有コードが含まれています。