【问题标题】:Including jQuery in a Rails + Webpack Application在 Rails + Webpack 应用程序中包含 jQuery
【发布时间】:2017-12-25 09:44:37
【问题描述】:

我在使用 Webpack 在我的 rails 应用程序中启动和运行 jQuery 时遇到了一些问题。我目前正在从 Rails 资产管道迁移。我删除了 jquery ruby​​ gem 的所有实例并重新捆绑。我已将 jquery 添加到我的 package.json 文件中:

{
  "dependencies": {
    "jquery": "^3.2.1"
  }
}

然后我创建了一个新的 webpack 插件:

const webpack = require('webpack')
const merge = require('webpack-merge')
const CompressionPlugin = require('compression-webpack-plugin')
const sharedConfig = require('./shared.js')

module.exports = merge(sharedConfig, {
  output: { filename: '[name]-[chunkhash].js' },
  devtool: 'source-map',
  stats: 'normal',

  plugins: [
    new webpack.providePlugin({
      $: 'jquery',
      jQuery: 'jquery'
    }),

    new webpack.optimize.UglifyJsPlugin({
      minimize: true,
      sourceMap: true,

      compress: {
        warnings: false
      },

      output: {
        comments: false
      }
    }),

    new CompressionPlugin({
      asset: '[path].gz[query]',
      algorithm: 'gzip',
      test: /\.(js|css|html|json|ico|svg|eot|otf|ttf)$/
    })
  ]
})

然后我在 application.js 中需要 jquery

import 'jquery';

console.log('Hello World')

require('script')

我还缺少其他配置步骤吗?我不知道为什么将 jQuery 包含到 rails/webpack 如此困难,我一直无法找到完成此操作的好文档。

【问题讨论】:

标签: javascript jquery ruby-on-rails webpack webpack-2


【解决方案1】:

不是这个

$: 'jquery',
jQuery: 'jquery'

试试这个:

$: 'jquery/src/jquery',
jQuery: 'jquery/src/jquery'

【讨论】:

    猜你喜欢
    • 2018-03-07
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    • 2015-09-29
    • 2017-07-05
    • 2017-08-29
    • 1970-01-01
    • 2016-06-14
    相关资源
    最近更新 更多