Skip to content

Commit

Permalink
Make webpack configuration be templates #34
Browse files Browse the repository at this point in the history
This closes #34
  • Loading branch information
yamadapc committed Sep 27, 2016
1 parent 6a5efe4 commit edb8d42
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 115 deletions.
56 changes: 3 additions & 53 deletions webpack.config.js
@@ -1,53 +1,3 @@
'use strict';

var path = require('path');
var webpack = require('webpack');

module.exports = {
devtool: 'cheap-eval-source-map',
entry: [
'webpack-hot-middleware/client?reload=true',
path.join(__dirname, 'src/index.js')
],
output: {
path: path.join(__dirname, '/js'),
filename: 'app.min.js',
publicPath: '/js'
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new webpack.DefinePlugin({
'process.env': {
// This has effect on the react lib size
NODE_ENV: JSON.stringify('development'),
ENABLE_LOGGER: JSON.stringify(process.env.ENABLE_LOGGER),
BUSYWS_HOST: JSON.stringify(process.env.BUSYWS_HOST || 'https://ws.busy6.com'),
IS_BROWSER: JSON.stringify(true),
}
})
],
module: {
loaders: [
{
test: /\.js?$/,
exclude: /node_modules/,
loader: 'babel',
},
{
test: /\.json?$/,
loader: 'json',
},
{
test: /\.s[ac]ss$/,
loaders: [
'style',
'css?sourceMap',
'autoprefixer-loader?browsers=last 2 version',
'sass?sourceMap&sourceMapContents',
],
},
],
},
};
module.exports = require('./webpack/makeConfig')({
isDevelopmen: true
});
65 changes: 3 additions & 62 deletions webpack.prod.config.js
@@ -1,62 +1,3 @@
'use strict';

var ExtractTextPlugin = require('extract-text-webpack-plugin');
var Visualizer = require('webpack-visualizer-plugin');
var path = require('path');
var webpack = require('webpack');

module.exports = {
devtool: 'source-map',
entry: [
path.join(__dirname, 'src/index.js')
],
output: {
path: path.join(__dirname, '/public/js'),
filename: 'app.min.js',
publicPath: '/js'
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({
minimize: true,
compress: {
warnings: false,
}
}),
new Visualizer({
filename: './statistics.html'
}),
new webpack.optimize.AggressiveMergingPlugin(),
new webpack.DefinePlugin({
'process.env': {
// This has effect on the react lib size
NODE_ENV: JSON.stringify('production'),
ENABLE_LOGGER: JSON.stringify(process.env.ENABLE_LOGGER),
BUSYWS_HOST: JSON.stringify(process.env.BUSYWS_HOST || 'https://ws.busy6.com'),
IS_BROWSER: JSON.stringify(true),
},
}),
new ExtractTextPlugin('../css/base.css'),
],
module: {
loaders: [
{
test: /\.js?$/,
exclude: /node_modules/,
loader: 'babel',
},
{
test: /\.json?$/,
loader: 'json',
},
{
test: /\.s[ac]ss$/,
loader: ExtractTextPlugin.extract(
'style-loader',
'css!autoprefixer-loader?browsers=last 2 version!sass'
),
},
],
},
};
module.exports = require('./webpack/makeConfig')({
isDevelopment: false,
});
125 changes: 125 additions & 0 deletions webpack/makeConfig.js
@@ -0,0 +1,125 @@
'use strict';
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const Visualizer = require('webpack-visualizer-plugin');
const _ = require('lodash');
const path = require('path');
const webpack = require('webpack');

const DEFAULTS = {
isDevelopment: process.env.NODE_ENV !== 'production',
baseDir: path.join(__dirname, '..'),
};

function makePlugins(options) {
const isDevelopment = options.isDevelopment;

let plugins = [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.DefinePlugin({
'process.env': {
// This has effect on the react lib size
NODE_ENV: isDevelopment
? JSON.stringify('development')
: JSON.stringify('production'),
ENABLE_LOGGER: JSON.stringify(process.env.ENABLE_LOGGER),
BUSYWS_HOST: JSON.stringify(process.env.BUSYWS_HOST || 'https://ws.busy6.com'),
IS_BROWSER: JSON.stringify(true),
},
}),
new Visualizer({
filename: './statistics.html'
}),
];

if (isDevelopment) {
plugins = plugins.concat([
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
]);
} else {
plugins = plugins.concat([
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({
minimize: true,
compress: {
warnings: false,
}
}),
new webpack.optimize.AggressiveMergingPlugin(),
new ExtractTextPlugin('../css/base.css'),
]);
}

return plugins;
}

function makeStyleLoaders(options) {
if (options.isDevelopment) {
return [
{
test: /\.s[ac]ss$/,
loaders: [
'style',
'css?sourceMap',
'autoprefixer-loader?browsers=last 2 version',
'sass?sourceMap&sourceMapContents',
],
},
];
}

return [
{
test: /\.s[ac]ss$/,
loader: ExtractTextPlugin.extract(
'style-loader',
'css!autoprefixer-loader?browsers=last 2 version!sass'
),
},
];
}

function makeConfig(options) {
if (!options) options = {};
_.defaults(options, DEFAULTS);

const isDevelopment = options.isDevelopment;

return {
devtool: isDevelopment ? 'cheap-eval-source-map' : 'source-map',
entry: (isDevelopment ? [
'webpack-hot-middleware/client?reload=true',
] : []).concat([
path.join(options.baseDir, 'src/index.js')
]),
output: {
path: path.join(options.baseDir, '/public/js'),
filename: 'app.min.js',
publicPath: '/js'
},
plugins: makePlugins(options),
module: {
loaders: [
{
test: /\.js?$/,
exclude: /node_modules/,
loader: 'babel',
},
{
test: /\.json?$/,
loader: 'json',
},
].concat(makeStyleLoaders(options)),
},
};
}

if (!module.parent) {
console.log(makeConfig({
isDevelopment: process.env.NODE_ENV !== 'production',
}));
}

exports = module.exports = makeConfig;
exports.DEFAULTS = DEFAULTS;

0 comments on commit edb8d42

Please sign in to comment.