Skip to content

Instantly share code, notes, and snippets.

@spalenza
Created February 5, 2017 08:49
Show Gist options
  • Save spalenza/e174fb8fb88e0afe4840d205881239f4 to your computer and use it in GitHub Desktop.
Save spalenza/e174fb8fb88e0afe4840d205881239f4 to your computer and use it in GitHub Desktop.
Rails 5 - Webpack
// 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');
// 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';
var config = {
entry: {
// Sources are expected to live in $app_root/webpack
'application': './webpack/application.js'
},
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', 'webpack'),
publicPath: '/webpack/',
filename: production ? '[name]-[chunkhash].js' : '[name].js'
},
module: {
rules: [
{
test: /\.es6$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader?presets[]=es2015'
},
{
test: /\.(jpe?g|png|gif|svg|ico|ttf)$/i,
loader: 'file-loader'
},
{
test: /\.(css|scss|sass)$/,
loader: [ExtractTextPlugin.extract({ fallbackLoader: 'style-loader', loader: ['css-loader'] }),
'css-loader',
'sass-loader'
],
}
]
},
resolve: {
modules: [
path.join(__dirname, '..', 'webpack'),
'node_modules'
],
extensions: ['.js', '.es6']
},
plugins: [
new ExtractTextPlugin({
filename: production ? '[name]-[chunkhash].css' : '[name].css',
allChunks: true,
}),
// must match config.webpack.manifest_filename
new StatsPlugin('manifest.json', {
// We only need assetsByChunkName
chunkModules: false,
source: false,
chunks: false,
modules: false,
assets: true
})]
};
if (production) {
config.plugins.push(
new webpack.NoErrorsPlugin(),
new webpack.optimize.UglifyJsPlugin(),
new webpack.DefinePlugin({
'process.env': { NODE_ENV: JSON.stringify('production') }
}),
new webpack.optimize.DedupePlugin()
);
} else {
config.devServer = {
port: devServerPort,
headers: { 'Access-Control-Allow-Origin': '*' }
};
config.output.publicPath = '//localhost:' + devServerPort + '/webpack/';
// Source maps
config.devtool = 'cheap-module-eval-source-map';
}
module.exports = config;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment