Skip to content

Instantly share code, notes, and snippets.

@hrumhrumble
Created November 28, 2016 12:14
Show Gist options
  • Save hrumhrumble/da12c1cdf951e26ef51a79f12881a10c to your computer and use it in GitHub Desktop.
Save hrumhrumble/da12c1cdf951e26ef51a79f12881a10c to your computer and use it in GitHub Desktop.
// Example webpack configuration with asset fingerprinting in production.
'use strict';
var path = require('path');
var webpack = require('webpack');
var StatsPlugin = require('stats-webpack-plugin');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var autoprefixer = require('autoprefixer');
var CompressionPlugin = require("compression-webpack-plugin");
// must match config.webpack.dev_server.port
var devServerPort = 3808;
// set NODE_ENV=production on the environment to add asset fingerprints
var production = process.env.NODE_ENV === 'production';
console.log("production? ", production);
var config = {
entry: {
// Sources are expected to live in $app_root/webpack
vendor: [
'babel-polyfill',
'es5-shim/es5-shim',
'es5-shim/es5-sham'
],
application: './assets/javascripts/application'
},
module: {
loaders: [
{
test: /\.js/,
exclude: /(node_modules|bower_components|vendor)/,
loader: "babel?presets[]=es2015"
},
{ test: /\.(jpe?g|png|gif|svg|woff|ttf|eot)$/i, loader: "file" },
{ test: /\.php$/,
loaders: [
'php-loader'
] }
]
},
output: {
// Build assets directly in to public/webpack/, let webpack know
// that all webpacked assets start with webpack/
// must match config.webpack.output_dir
path: path.join(__dirname, './public/assets'),
// publicPath: './public/assets',
// filename: production ? '[name]-[chunkhash].js' : '[name].js'
filename: '[name].js'
},
resolve: {
root: path.join(__dirname, '..', 'assets'),
extensions: ["", ".js", ".css", ".sass"],
modules: ['node_modules', 'src']
},
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
}),
// must match config.webpack.manifest_filename
new StatsPlugin('manifest.json', {
// We only need assetsByChunkName
chunkModules: false,
source: false,
chunks: false,
modules: false,
assets: true
}),
new webpack.optimize.CommonsChunkPlugin('vendor', 'vendor.js', Infinity)
],
postcss: [autoprefixer(({ browsers: ['IE >= 9'] }))]
};
if (production) {
config.module.loaders = config.module.loaders.concat([
{ test: /\.sass$/, loader: ExtractTextPlugin.extract("css!postcss!sass!import-glob") },
{ test: /\.scss$/, loader: ExtractTextPlugin.extract("css!postcss!sass!import-glob") },
{ test: /\.css$/, loader: ExtractTextPlugin.extract("css") },
]);
config.plugins.push(
new ExtractTextPlugin("all", "[name].css", {allChunks: true}),
new webpack.NoErrorsPlugin(),
new webpack.optimize.UglifyJsPlugin({
compressor: { warnings: false },
sourceMap: false
}),
new webpack.DefinePlugin({
'process.env': { NODE_ENV: JSON.stringify('production') }
}),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new CompressionPlugin({
asset: "[path].gz",
algorithm: "gzip",
test: /\.js$|\.css$/,
threshold: 4096,
minRatio: 0.8
})
);
} else {
config.module.loaders = config.module.loaders.concat([
{ test: /\.sass$/, loader: "style!css?sourceMap!postcss?sourceMap!sass?sourceMap!import-glob" },
{ test: /\.scss$/, loader: "style!css?sourceMap!postcss?sourceMap!sass?sourceMap!import-glob" },
{ test: /\.css$/, loader: "style!css?sourceMap!postcss?sourceMap" },
]);
config.devServer = {
port: devServerPort,
headers: { 'Access-Control-Allow-Origin': '*' },
proxy: {
'*': 'http://localhost:8888/roche/public'
}
};
config.output.publicPath = 'http://localhost:' + devServerPort + '/public/assets';
// Source maps
config.devtool = 'cheap-module-eval-source-map';
}
//console.log(config);
module.exports = config;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment