How to add jquery third party plugin in rails 6 webpacker

后端 未结 4 671
滥情空心
滥情空心 2020-12-06 10:19

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.

//application.js
         


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-06 10:21

    to resolve jquery third party plugin issue add jquery via yarn

    yarn add jquery
    

    for adding jquery support in rails 6 application first we need to add below configuration

    # 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

    use below instructions

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

    updated with expose-loader for jQuery

    yarn add expose-loader
    

    Then add this to 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;
    

提交回复
热议问题