简体   繁体   English

如何在rails 6 webpacker中添加jquery第三方插件

[英]How to add jquery third party plugin in rails 6 webpacker

I know its simple but with update of rails 6. there is new syntax in rails 6 for manage javascript assets which is maintained by webpacker.我知道它很简单,但是随着 rails 6 的更新。在 rails 6 中有新的语法来管理由 webpacker 维护的 javascript 资产。

//application.js
require("@rails/ujs") //.start()
require("turbolinks").start()
require("@rails/activestorage").start()
require('jquery').start()
require('jquery_ujs').start()
require('bootstrap-daterangepicker').start()
require("custom/custom").start()
require("bootstrap").start()
require("channels")

i am able to add custom/custom but bootstrap and jquery is not working i have install jquery and bootstrap via npm我可以添加custom/custom但引导程序和 jquery 不工作我已经通过 npm 安装了 jquery 和引导程序

run below command to add jQuery.运行下面的命令来添加 jQuery。

$ yarn add jquery

Add below code in config/webpack/environment.jsconfig/webpack/environment.js添加以下代码

const webpack = require('webpack')
environment.plugins.prepend('Provide',
  new webpack.ProvidePlugin({
    $: 'jquery/src/jquery',
    jQuery: 'jquery/src/jquery'
  })
)

Require jquery in application.js file.在 application.js 文件中需要 jquery。

require('jquery')

No more need to add jquery-rails gem!不再需要添加 jquery-rails gem!

to resolve jquery third party plugin issue add jquery via yarn解决jquery第三方插件问题通过yarn添加jquery

yarn add jquery

for adding jquery support in rails 6 application first we need to add below configuration为了首先在 rails 6 应用程序中添加 jquery 支持,我们需要添加以下配置

# app/config/webpack/environment.js
const {environment} = require('@rails/webpacker');

const webpack = require('webpack');
environment.plugins.append('Provide', new webpack.ProvidePlugin({
  $: 'jquery',
  jQuery: 'jquery' # or if its not work specify path `'jquery/src/jquery'` which node_modules path for jquery
}));

module.exports = environment;

for import any jquery related plugin in app/javascripts/packs/application.js用于在app/javascripts/packs/application.js导入任何与 jquery 相关的插件

use below instructions使用以下说明

import 'bootstrap/dist/js/bootstrap';
import 'bootstrap-daterangepicker/daterangepicker'

updated with expose-loader for jQuery更新了 jQuery 的expose-loader

yarn add expose-loader

Then add this to config/webpack/environment.js然后将其添加到 config/webpack/environment.js

   environment.loaders.append('jquery', {
      test: require.resolve('jquery'),
      use: [{
        loader: 'expose-loader',
        options: '$',
      }, {
        loader: 'expose-loader',
        options: 'jQuery',
      }],
    });
    module.exports = environment;

Apparently expose-loader 1.0.0 has a different format:显然,expose-loader 1.0.0 有不同的格式:

environment.loaders.append('jquery', {
  test: require.resolve('jquery'),
  rules: [
    {
      loader: 'expose-loader',
      options: {
        exposes: ['$', 'jQuery'],
      },
    },
  ],
});

Ensure you have yarn installed and updated to the latest version, then create your rails application.确保您已安装 yarn 并更新到最新版本,然后创建您的 rails 应用程序。

First Run the following command to install Bootstrap, Jquery and Popper.js首先运行以下命令安装Bootstrap、Jquery和Popper.js

yarn add bootstrap@4.5 jquery popper.js

On the above ofcourse you can change to the latest version of Bootstrap.在上面的课程中,您可以更改为最新版本的 Bootstrap。

If you open package.json file, you will notice Bootstrap 4.5, Jquery latest version and Popper.js latest versions have been added for you.如果你打开 package.json 文件,你会注意到 Bootstrap 4.5、Jquery 最新版本和 Popper.js 最新版本已经为你添加。

Next go to config/webpack/environment.js and amend the file.接下来转到 config/webpack/environment.js 并修改文件。

const { environment } = require('@rails/webpacker')
    
const webpack = require("webpack")

environment.plugins.append("Provide", new webpack.ProvidePlugin({

$: 'jquery',

jQuery: 'jquery',

Popper: ['popper.js', 'default']

}))

module.exports = environment

Next go to app/assets/stylesheets/application.css and amend the file make sure to require bootstrap.接下来转到 app/assets/stylesheets/application.css 并修改文件确保需要引导程序。

*= require bootstrap

*= require_tree .

*= require_self

Finally go to application.js file and amend the file by adding import 'bootstrap';最后转到 application.js 文件并通过添加import 'bootstrap';修改文件import 'bootstrap'; in order for bootstrap javascript to work.为了使引导 javascript 工作。

import 'bootstrap';
require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()

Save all changes, restart rails server.保存所有更改,重新启动 rails 服务器。

That should work.那应该工作。

In webpacker v. 6 there is no config/webpack/environment.js and other files structure在 webpacker v. 6 中没有config/webpack/environment.js和其他文件结构

Firstly you need add JQuery to your project using yarn :首先,您需要使用yarn将 JQuery 添加到您的项目中:

yarn add jquery

After that you can integrate JQuery using one of ways:之后,您可以使用以下方法之一集成 JQuery:

  1. Directly update base config:直接更新基础配置:
// config/webpack/base.js

const { webpackConfig } = require('@rails/webpacker')
const webpack = require('webpack')

webpackConfig.
  plugins.
  push(
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery'
    })
  )

module.exports = webpackConfig
  1. Use custom config and merge it to base config:使用自定义配置并将其合并到基本配置:
// config/webpack/base.js

const { webpackConfig, merge } = require('@rails/webpacker')
const customConfig = require('./custom')

module.exports = merge(webpackConfig, customConfig)
// config/webpack/custom.js

const webpack = require('webpack')

module.exports = {
  plugins: [
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery'
    })
  ]
}

On my opinion second way is more flexible在我看来第二种方式更灵活

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

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