Skip to content

Instantly share code, notes, and snippets.

@jamesslock
Created January 17, 2017 01:17
Show Gist options
  • Save jamesslock/b6815e34975338221b40c88e7d0562c4 to your computer and use it in GitHub Desktop.
Save jamesslock/b6815e34975338221b40c88e7d0562c4 to your computer and use it in GitHub Desktop.
import webpack from 'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import autoprefixer from 'autoprefixer';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import ReplacePlugin from 'replace-bundle-webpack-plugin';
import OfflinePlugin from 'offline-plugin';
import path from 'path';
import V8LazyParseWebpackPlugin from 'v8-lazy-parse-webpack-plugin';
import ScriptExtHtmlWebpackPlugin from "script-ext-html-webpack-plugin";
const ENV = process.env.NODE_ENV || 'development';
module.exports = {
context: path.resolve(__dirname, "src"),
entry: './index.js',
output: {
path: path.resolve(__dirname, "build"),
publicPath: '/',
filename: 'bundle.js'
},
resolve: {
extensions: ['', '.jsx', '.js', '.json'],
modulesDirectories: [
path.resolve(__dirname, "src/lib"),
path.resolve(__dirname, "node_modules"),
'node_modules'
],
alias: {
components: path.resolve(__dirname, "src/components"), // used for tests
'react': 'preact-compat',
'react-dom': 'preact-compat'
}
},
module: {
preLoaders: [
{
test: /\.jsx?$/,
exclude: path.resolve(__dirname, 'src'),
loader: 'source-map'
}
],
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel'
},
{
test: /\.json$/,
loader: 'json'
},
{
test: /\.(xml|html|txt|md)$/,
loader: 'raw'
},
{
test: /\.(svg|woff2?|ttf|eot|jpe?g|png|gif)(\?.*)?$/i,
loader: ENV==='production' ? 'file?name=[path][name]_[hash:base64:5].[ext]' : 'url'
}
]
},
plugins: ([
new webpack.NoErrorsPlugin(),
new webpack.DefinePlugin({
'process': {},
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
}),
new HtmlWebpackPlugin({
template: './index.ejs',
minify: { collapseWhitespace: true }
}),
new ScriptExtHtmlWebpackPlugin({
defaultAttribute: "async"
}),
new CopyWebpackPlugin([
{ from: './manifest.json', to: './' },
{ from: './favicon.ico', to: './' }
])
]).concat(ENV==='production' ? [
new V8LazyParseWebpackPlugin(),
new webpack.optimize.UglifyJsPlugin({
output: {
comments: false
},
compress: {
warnings: false,
conditionals: true,
unused: true,
comparisons: true,
sequences: true,
dead_code: true,
evaluate: true,
if_return: true,
join_vars: true,
negate_iife: false
}
}),
// strip out babel-helper invariant checks
new ReplacePlugin([{
// this is actually the property name https://github.com/kimhou/replace-bundle-webpack-plugin/issues/1
partten: /throw\s+(new\s+)?[a-zA-Z]+Error\s*\(/g,
replacement: () => 'return;('
}]),
new OfflinePlugin({
relativePaths: false,
AppCache: false,
ServiceWorker: {
events: true
},
publicPath: '/'
})
] : []),
stats: { colors: true },
node: {
global: true,
process: false,
Buffer: false,
__filename: false,
__dirname: false,
setImmediate: false
},
devtool: ENV==='production' ? 'source-map' : 'cheap-module-eval-source-map',
devServer: {
port: process.env.PORT || 8080,
host: 'localhost',
colors: true,
publicPath: '/',
contentBase: './src',
historyApiFallback: true,
open: false,
proxy: {
// OPTIONAL: proxy configuration:
// '/optional-prefix/**': { // path pattern to rewrite
// target: 'http://target-host.com',
// pathRewrite: path => path.replace(/^\/[^\/]+\//, '') // strip first path segment
// }
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment