-
-
Save jjmontesl/608cfb8bb248098133f51f04105e8395 to your computer and use it in GitHub Desktop.
Webpack.config.js for multiple applications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | |
}` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment