Rails 6: How to add jquery-ui through webpacker?

后端 未结 8 906
失恋的感觉
失恋的感觉 2021-01-05 04:12

I\'m currently trying to implement a datepicker into my application, the problem is that there is no documentation on how to add the jquery-ui-rails gem through

相关标签:
8条回答
  • 2021-01-05 04:42

    None of these answers quite worked for me. Here's how I ended up getting it implemented:

    yarn add jquery
    

    then

    yarn add jquery-ui-dist
    

    in your app/javascript/packs/application.js file:

    // jquery
    import $ from 'jquery';
    
    global.$ = $
    global.jQuery = $
    
    
    require('jquery-ui');
    
    // jquery-ui theme
    require.context('file-loader?name=[path][name].[ext]&context=node_modules/jquery-ui-dist!jquery-ui-dist', true,    /jquery-ui\.css/ );
    require.context('file-loader?name=[path][name].[ext]&context=node_modules/jquery-ui-dist!jquery-ui-dist', true,    /jquery-ui\.theme\.css/ );
    

    and in config/webpack/environment.js:

    const { environment } = require('@rails/webpacker');
    
    const webpack = require('webpack');
    
    
    // resolve-url-loader must be used before sass-loader
    environment.loaders.get('sass').use.splice(-1, 0, {
        loader: 'resolve-url-loader',
        options: {
            attempts: 1
        }
    });
    
    
    // Add an additional plugin of your choosing : ProvidePlugin
    
    environment.plugins.prepend('Provide', new webpack.ProvidePlugin({
            $: 'jquery',
            JQuery: 'jquery',
            jquery: 'jquery',
            'window.Tether': "tether",
            Popper: ['popper.js', 'default'], // for Bootstrap 4
        })
    )
    
    const aliasConfig = {
        'jquery': 'jquery/src/jquery',
        'jquery-ui': 'jquery-ui-dist/jquery-ui.js'
    
    };
    
    environment.config.set('resolve.alias', aliasConfig);
    
    //
    module.exports = environment;
    

    A restart to my server got it working fine for me. Here is a link with details on webpacker that I used to get this to work:

    https://gist.github.com/maxivak/2612fa987b9f9ed7cb53a88fcba247b3#jquery-jquery-ui

    0 讨论(0)
  • 2021-01-05 04:47

    Kalman's answer puts jQuery within the scope of the scripts in the app/javascript directory but not with any in-line javascript that you may have on your webpages.

    If you want to access jQuery from the scope of the webpage, you could can put jQuery under the public directory then modify app/views/layouts/application.html.erb to link to it like this:

    <!DOCTYPE html>
    <html>
      <head>
        <title>JqueryTest</title>
        <%= csrf_meta_tags %>
        <%= csp_meta_tag %>
    
        <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    
    
        <%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
        <script src="/jquery-3.4.1.js"></script>
        <%= stylesheet_link_tag "/jquery-ui-1.12.1.custom/jquery-ui.min.css" %>
        <script src="/jquery-ui-1.12.1.custom/jquery-ui.min.js"></script>
    
    
      </head>
    
      <body>
        <%= yield %>
      </body>
    </html>
    
    
    0 讨论(0)
提交回复
热议问题