Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 webpacker.

Probably there is another way to add gems or another gem that would fit my needs?

like image 949
arnasklas Avatar asked Aug 19 '19 11:08

arnasklas


People also ask

What is the default Webpack for rails 6?

Rails 6 comes with webpacker by default so you don’t need to do anything for it! When you create a brand new jquery Rails 6 app it will add webpacker and will do the configurations for you. Rails webpack doesn’t have difficult configurations during installation.

What is webpacker gem in rails?

Webpacker gem uses Webpack to manage app-like JavaScript modules in Rails jquery install and configuration. Webpacker makes it easy to use the JavaScript pre-processor and bundler webpack 4.x.x+ to manage application-like JavaScript in Rails 6 jquery.

Can I use jQuery in Ruby on rails 6?

Ruby on Rails, Technology Adding jQuery to Rails 6 with webpacker will enable you to write ES 6 flavored JavaScrip applications in no time. An essential thing to note is that Rails 6 comes installed with webpacker by default so you don’t need to install it separately.

How do I include a plugin in a Webpack package?

The ProvidePlugin makes a package available as a variable in every module compiled through webpack. If webpack sees that variable used, it will include the given package in the final bundle. Open config/webpack/environment.js , and then add the provide plugin, and provide “ $ ”, “ jQuery ” and “ Popper ” variables.


1 Answers

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

like image 174
sdempsey13 Avatar answered Oct 17 '22 04:10

sdempsey13