Skip to content

Instantly share code, notes, and snippets.

@restebanez
Created September 29, 2015 14:47
Show Gist options
  • Select an option

  • Save restebanez/5a7bd350833e4752fad2 to your computer and use it in GitHub Desktop.

Select an option

Save restebanez/5a7bd350833e4752fad2 to your computer and use it in GitHub Desktop.
Integration of webpack-dev-server with rails without using CORS, just redirecting in the action_controller.asset_host
/*
/Users/restebanez/mdsol/repos/medistrano/app/views/ecs/_index.html.erb
<%= javascript_include_tag 'wpbundle' %> # <script src='/assets/wpbundle.js'></script> goes wronly to routes.rb
/Users/restebanez/mdsol/repos/medistrano/config/environments/development.rb
config.action_controller.asset_host = Proc.new { |source|
if source =~ /wpbundle\.js$/i
"http://localhost:8080"
end
}
*/
// run like this:
// node /Users/restebanez/mdsol/repos/medistrano/client/node_modules/webpack-dev-server/bin/webpack-dev-server.js --config develop.webpack.config.js --hot --inline --progress
var path = require("path");
var webpack = require('webpack');
module.exports = {
context: __dirname,
entry: {
app: [
// 'webpack-dev-server/client?http://localhost:8080/assets/' is added with the --inline option
// 'webpack/hot/dev-server' is added with the --hot option
'jquery', 'jquery-ujs', "./assets/javascripts/App"]
},
output: {
path: path.resolve(__dirname, "../app/assets/javascripts/generated"), // this is useless since webpack-dev-server doesn't write the bundle in disk
publicPath: 'http://localhost:8080/assets',
filename: 'wpbundle.js' // it will get served here: http://localhost:8080/assets/wpbundle.js
},
// When using WebpackDevServer CLI flag --hot, the plugin new HotModuleReplacementPlugin() should not be used and vice versa
// plugins: [
// new webpack.HotModuleReplacementPlugin()
// ],
resolve: {
root: [
path.join(__dirname, 'assets/javascripts')
],
extensions: ['', '.webpack.js', '.web.js', '.js', '.jsx', '.scss', '.css', 'config.js']
},
module: {
loaders: [
// https://github.com/webpack/expose-loader
// React is necessary for the client rendering:
// the loders order matter, they're processed from right to left
// the loader can use its module name suffiexed by -loder. ie: jsx or jsx-loader
{test: /\.jsx?$/, loaders: ['react-hot', 'jsx', 'babel'], exclude: /node_modules/ },
{test: require.resolve('react'), loader: 'expose?React'}, // This line works to expose React to the web browser to enable the Chrome React devtools
{test: require.resolve('jquery'), loader: 'expose?jQuery'},
{test: require.resolve('jquery'), loader: 'expose?$'}
]
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment