Skip to content

Instantly share code, notes, and snippets.

@agmcleod
Created April 24, 2016 21:45
Show Gist options
  • Save agmcleod/55ead7d082131e3d54ecfcf6ceef26e7 to your computer and use it in GitHub Desktop.
Save agmcleod/55ead7d082131e3d54ecfcf6ceef26e7 to your computer and use it in GitHub Desktop.
React on rails configs
// DO NOT REQUIRE jQuery or jQuery-ujs in this file!
// DO NOT REQUIRE TREE!
// CRITICAL that vendor-bundle must be BEFORE bootstrap-sprockets and turbolinks
// since it is exposing jQuery and jQuery-ujs
//= require vendor-bundle
//= require server-bundle
//= require turbolinks
// bootstrap-sprockets depends on vendor-bundle for jQuery.
//= require bootstrap-sprockets
// Common client-side webpack configuration used by webpack.hot.config and webpack.rails.config.
const webpack = require('webpack');
const path = require('path');
const devBuild = process.env.NODE_ENV !== 'production';
const nodeEnv = devBuild ? 'development' : 'production';
var config = {
// the project dir
context: __dirname,
entry: {
// See use of 'vendor' in the CommonsChunkPlugin inclusion below.
vendor: [
'babel-polyfill',
'jquery'
],
// This will contain the app entry points defined by webpack.hot.config and
// webpack.rails.config
admin: [
'./app/bundles/Projects/startup/registration'
]
},
resolve: {
extensions: ['', '.js', '.jsx'],
alias: {
lib: path.join(process.cwd(), 'app', 'lib'),
react: path.resolve('./node_modules/react'),
'react-dom': path.resolve('./node_modules/react-dom'),
},
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(nodeEnv),
},
}),
// https://webpack.github.io/docs/list-of-plugins.html#2-explicit-vendor-chunk
new webpack.optimize.CommonsChunkPlugin({
// This name 'vendor' ties into the entry definition
name: 'vendor',
// We don't want the default vendor.js name
filename: 'vendor-bundle.js',
// Passing Infinity just creates the commons chunk, but moves no modules into it.
// In other words, we only put what's in the vendor entry definition in vendor-bundle.js
minChunks: Infinity,
}),
],
module: {
loaders: [
// Not all apps require jQuery. Many Rails apps do, such as those using TurboLinks or
// bootstrap js
{ test: require.resolve('jquery'), loader: 'expose?jQuery' },
{ test: require.resolve('jquery'), loader: 'expose?$' }
],
},
};
if (devBuild) {
config.devtool = '#inline-source-map';
}
module.exports = config;
// Webpack configuration for server bundle
const webpack = require('webpack');
const path = require('path');
const devBuild = process.env.NODE_ENV !== 'production';
const nodeEnv = devBuild ? 'development' : 'production';
module.exports = {
// the project dir
context: __dirname,
entry: [
'babel-polyfill',
'./app/bundles/ProjectsConsumerApp/startup/registration'
],
output: {
filename: 'server-bundle.js',
path: '../app/assets/webpack'
},
resolve: {
extensions: ['', '.js', '.jsx'],
alias: {
lib: path.join(process.cwd(), 'app', 'lib'),
},
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(nodeEnv),
},
}),
],
module: {
loaders: [
{ test: /\.jsx?$/, loader: 'babel-loader', exclude: /node_modules/ }
],
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment