Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails with Webpack

I am using ReactOnRails app and I have node-modules folder with packages installed using npm. How can I make these javascript libaries available in app/assets/javascript/application.js, so I do not have to for example install jquery in my modules and in my Gemfile?

This is my webpack config file:

/* eslint comma-dangle: ["error",
  {"functions": "never", "arrays": "only-multiline", "objects":
"only-multiline"} ] */

const webpack = require('webpack');
const path = require('path');

const devBuild = process.env.NODE_ENV !== 'production';
const nodeEnv = devBuild ? 'development' : 'production';

const config = {
  entry: [
    'es5-shim/es5-shim',
    'es5-shim/es5-sham',
    'babel-polyfill',
    './app/bundles/Home/startup/registration',
  ],

  output: {
    filename: 'webpack-bundle.js',
    path: '../app/assets/webpack',
  },

  resolve: {
    extensions: ['', '.js', '.jsx'],
    alias: {
      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),
      },
    }),
  ],
  module: {
    loaders: [
      {
        test: require.resolve('react'),
        loader: 'imports?shim=es5-shim/es5-shim&sham=es5-shim/es5-sham',
      },
      {
        test: /\.jsx?$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
      },
    ],
  },
};

module.exports = config;

if (devBuild) {
  console.log('Webpack dev build for Rails'); // eslint-disable-line no-console
  module.exports.devtool = 'eval-source-map';
} else {
  config.plugins.push(
    new webpack.optimize.DedupePlugin()
  );
  console.log('Webpack production build for Rails'); // eslint-disable-line no-console
}
like image 271
mdmb Avatar asked Mar 11 '23 02:03

mdmb


1 Answers

Rails 5.1 comes with webpacker and removed default jquery-rails dependency, but you could still use it if you like. In template:

<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

will compile JS using traditional assets pipeline (from app/assets/javascript/application.js).

While

<%= javascript_pack_tag 'application' %>

will compile your JS module from app/javascript/packs/application.js using webpacker. You can trigger manual module compilation using:

rails webpacker:compile
like image 129
Tombart Avatar answered Apr 08 '23 19:04

Tombart