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

后端 未结 8 956
失恋的感觉
失恋的感觉 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

提交回复
热议问题