Last active
November 28, 2016 21:22
-
-
Save brandonaaskov/e44e8ff0adb2bc89435523a9e81ad2e5 to your computer and use it in GitHub Desktop.
extract-text-webpack-plugin example
This file contains 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 _ = require('lodash') | |
const path = require('path') | |
const webpack = require('webpack') | |
const ExtractTextPlugin = require('extract-text-webpack-plugin') | |
const pkg = require('./package.json') | |
// Webpack: The Confusing Parts | |
// https://medium.com/@rajaraodv/webpack-the-confusing-parts-58712f8fcad9 | |
const isDevelopment = process.env.NODE_ENV === 'development' | |
const plugins = [ | |
new webpack.optimize.OccurenceOrderPlugin(), | |
new webpack.optimize.DedupePlugin(), | |
new webpack.NoErrorsPlugin(), | |
new webpack.DefinePlugin({ | |
'process.env': { | |
NODE_ENV: JSON.stringify(process.env.NODE_ENV) | |
} | |
}), | |
new ExtractTextPlugin(`${pkg.name}.css`) | |
] | |
if (!isDevelopment) { | |
plugins.push(new webpack.optimize.UglifyJsPlugin()) | |
} | |
const entry = {} | |
entry[pkg.name] = './src/index' | |
entry[pkg.name] = './assets/styles/index.scss' | |
// this default is production | |
const production = { | |
devtool: 'source-map', | |
entry, | |
output: { | |
path: path.join(__dirname, 'dist'), | |
filename: '[name].js', | |
publicPath: '/' | |
}, | |
resolve: { | |
extensions: ['', '.js'] | |
}, | |
module: { | |
loaders: [ | |
{ | |
test: /\.js?$/, | |
loaders: ['babel'], | |
include: path.join(__dirname, 'src') | |
}, | |
{ | |
test: /\.scss$/, | |
loader: ExtractTextPlugin.extract('style', 'css!sass'), | |
include: [ | |
path.resolve(__dirname, 'assets/styles') | |
] | |
}, | |
{ | |
test: /\.png$/, | |
loader: 'url-loader?limit=1' | |
} | |
] | |
}, | |
postcss: function () { | |
const autoPrefixerOptions = { | |
browsers: ['last 2 versions'] | |
} | |
return [ | |
require('autoprefixer')(autoPrefixerOptions) | |
] | |
}, | |
plugins | |
} | |
let config = _.cloneDeep(production) | |
if (isDevelopment) { | |
_.assign(config, { | |
devtool: 'eval', | |
entry: [ | |
'react-hot-loader/patch', | |
'./src/index' | |
], | |
devServer: { | |
host: '0.0.0.0' | |
} | |
}) | |
_.set(config, 'output.filename', `${pkg.name}.js`) | |
} | |
module.exports = config |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment