Skip to content

Instantly share code, notes, and snippets.

@braska
Created May 15, 2018 07:45
Show Gist options
  • Save braska/e84b019d60a4da7ec323623870144d1c to your computer and use it in GitHub Desktop.
Save braska/e84b019d60a4da7ec323623870144d1c to your computer and use it in GitHub Desktop.
const NODE_ENV = process.env.NODE_ENV || 'development';
const webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const ManifestPlugin = require('webpack-manifest-plugin');
const path = require('path');
const addHash = (template, hash) =>
NODE_ENV === 'production' ? template.replace(/\.[^.]+(\.map)?$/, `.[${hash}]$&`) : template;
module.exports = ({ name, srcDir, outputDir }) => {
const config = {
name,
context: path.resolve(__dirname, path.join('src', srcDir)),
entry: {
main: (files =>
(NODE_ENV !== 'production'
? [
'@babel/polyfill',
`webpack-hot-middleware/client?path=/__webpack_hmr&name=${name}`,
'react-hot-loader/patch',
]
: ['@babel/polyfill']
).concat(files))(['./index']),
},
output: {
path: path.resolve(__dirname, 'dist', outputDir),
publicPath: '/',
filename: addHash('assets/js/[name].js', 'chunkhash'),
sourceMapFilename: addHash('assets/js/[name].js.map', 'chunkhash'),
},
mode: NODE_ENV,
devtool: NODE_ENV === 'production' ? 'source-map' : 'eval-source-map',
resolve: {
extensions: ['.js', '.jsx', '.json'],
modules: ['./src/shared', `./src/${srcDir}`, 'node_modules'],
},
module: {
rules: [
{
enforce: 'pre',
test: /\.jsx?$/,
use: 'eslint-loader',
exclude: /node_modules/,
},
{
test: /\.jsx?$/,
use: 'babel-loader',
exclude: /node_modules/,
},
{
test: /\.css$/,
use: ['css-loader'],
},
{
test: /\.(jpe?g|png|gif|svg)$/,
use: 'file-loader',
},
],
},
plugins: [
new CleanWebpackPlugin(path.join('dist', outputDir)),
new webpack.DefinePlugin({
__DEV__: JSON.stringify(NODE_ENV !== 'production'),
__SERVER__: JSON.stringify(false),
}),
new CopyWebpackPlugin(['../shared/static', 'static']),
new ManifestPlugin({
publicPath: '/',
}),
new webpack.EnvironmentPlugin([
'LOGIN_URL',
'LOGOUT_URL',
'REGISTRATION_URL',
'CABINET_URL',
'ADD_ITEM_URL',
'API_URL',
'BASE_DOMAIN',
'REALTIME_SERVER_URL',
]),
],
};
if (NODE_ENV !== 'production') {
config.plugins.push(new webpack.HotModuleReplacementPlugin());
}
return config;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment