Skip to content

Instantly share code, notes, and snippets.

@neocoder
Created September 21, 2017 13:59
Show Gist options
  • Save neocoder/1a6a9914bf895c42480e7bd47d2ab205 to your computer and use it in GitHub Desktop.
Save neocoder/1a6a9914bf895c42480e7bd47d2ab205 to your computer and use it in GitHub Desktop.
var path = require('path');
var _ = require('lodash');
var webpack = require('webpack');
var CleanWebpackPlugin = require('clean-webpack-plugin');
var ManifestPlugin = require('webpack-manifest-plugin');
var env = process.env.WEBPACK || 'dev';
var hotMiddlewareScript = 'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000'; // &reload=true';
var reactHotLoaderPatch = 'react-hot-loader/patch';
// var polyfill = 'core-js/es6/object';
// var polyfill = './shared-modules/browser-polyfills';
var polyfill = 'babel-polyfill';
// dev
// testserver
// production
var GAPPS_API = require('./app/config/api/' + env + '.json');
var devConfig = {
context: path.resolve('./frontend'),
entry: {
bundle: [reactHotLoaderPatch, hotMiddlewareScript, polyfill, './main/index.js'],
admin: [reactHotLoaderPatch, hotMiddlewareScript, polyfill, './admin/index.js'],
'shared-test': [
reactHotLoaderPatch,
hotMiddlewareScript,
polyfill,
'./shared-test/index.js'
],
st: [reactHotLoaderPatch, hotMiddlewareScript, polyfill, './st/index.js'],
vendor: [polyfill, 'moment', 'lodash', 'async', 'redux-form']
},
target: 'web',
resolve: {
alias: {
SharedModules: path.resolve(__dirname, 'frontend/shared-modules/'),
Shared: path.resolve(__dirname, 'shared'),
Main: path.resolve(__dirname, 'frontend/main/'),
Admin: path.resolve(__dirname, 'frontend/admin/')
},
extensions: ['.js', '.json', '.jsx', '.css']
},
output: {
filename: '[name].js',
// 'filename': '[name].[hash].js',
path: path.resolve('./app/public/js'),
publicPath: '/js/'
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader'
},
{ test: /\.css$/, loader: 'style-loader!css-loader' },
{ test: /\.png$/, loader: 'url-loader?limit=100000' },
{ test: /\.jpg$/, loader: 'file-loader' },
{
test: /\.json$/,
loader: 'json-loader'
}
]
},
devServer: {
contentBase: './app/public/',
port: 8008,
hot: true,
historyApiFallback: true
},
// devtool: 'eval',
// devtool: 'cheap-module-eval-source-map',
// devtool: 'eval-source-map',
devtool: 'cheap-eval-source-map',
plugins: [
new webpack.DefinePlugin({
GAPPS_API: JSON.stringify(GAPPS_API),
'process.env': {
NODE_ENV: JSON.stringify('dev')
}
}),
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor'] // Specify the common bundle's name.
}),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin()
]
};
function filterHotParts(name) {
return !name.match(/(webpack-hot-middleware|react-hot)/);
}
function getProductionConfig() {
var newConf = _.extend({}, devConfig);
newConf.output.filename = '[name].[chunkhash].js';
// newConf.devtool = 'cheap-module-source-map';
newConf.devtool = 'source';
newConf.plugins = [
new CleanWebpackPlugin(['app/public/js'], {
root: __dirname,
verbose: true,
dry: false,
exclude: [
'flot-charts',
'maps',
'charts.js',
'functions.js',
'htmllint-browserify.js',
'libs.js',
'autotrack.js',
'libs.min.js',
'libs-admin.js',
'libs-admin.min.js',
'prism.js'
]
}),
new webpack.DefinePlugin({
GAPPS_API: JSON.stringify(GAPPS_API),
'process.env': {
NODE_ENV: JSON.stringify('production')
}
}),
// redefining the NODE_ENV variable for the bundle
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor', 'manifest'] // Specify the common bundle's name.
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: true
}
}),
new ManifestPlugin(
{
// fileName: 'my-manifest.json',
// basePath: '/app/'
}
)
];
if (_.isArray(newConf.entry)) {
newConf.entry = _.filter(newConf.entry, filterHotParts);
} else if (_.isPlainObject(newConf.entry)) {
newConf.entry = Object.keys(newConf.entry).reduce((newEntryObj, entryName) => {
newEntryObj[entryName] = _.filter(newConf.entry[entryName], filterHotParts);
return newEntryObj;
}, {});
}
return newConf;
}
var cfg;
switch (env) {
case 'production':
case 'testserver':
cfg = getProductionConfig();
break;
default:
cfg = devConfig;
break;
}
if (process.argv[2] !== '--json') {
console.log('LOADING ' + env + ' WEBPACK CONFIG');
// console.log(require('util').inspect(cfg, { depth: null, colors: true }));
}
module.exports = cfg;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment