简体   繁体   English

与Webpack的Rails

[英]Rails with Webpack

I am using ReactOnRails app and I have node-modules folder with packages installed using npm . 我正在使用ReactOnRails应用程序,我有node-modules文件夹,包含使用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 ? 如何在app/assets/javascript/application.js提供这些javascript库,所以我不必在我的模块和我的Gemfile安装jquery

This is my webpack config file: 这是我的webpack配置文件:

/* 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
}

Rails 5.1 comes with webpacker and removed default jquery-rails dependency, but you could still use it if you like. Rails 5.1附带webpacker并删除了默认的jquery-rails依赖项,但如果你愿意,你仍然可以使用它。 In template: 在模板中:

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

will compile JS using traditional assets pipeline (from app/assets/javascript/application.js ). 将使用传统资产管道(来自app/assets/javascript/application.js )编译JS。

While

<%= javascript_pack_tag 'application' %>

will compile your JS module from app/javascript/packs/application.js using webpacker. 将使用webpacker从app/javascript/packs/application.js编译您的JS模块。 You can trigger manual module compilation using: 您可以使用以下命令触发手动模块编译:

rails webpacker:compile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM