I use laravel-mix (including webpack) to bundle JS files.
Using BundleAnalyzerPlugin I found that my output file includes more than one copy of JQuery lib which boosts the output file size.
It seams several modules includes JQuery by themselves.
Any ideas how to avoid this and remove all redundant jquery inclusions?
(UPDATED with more info)
Image of Bundle analyzer output
webpack.mix.js
const { mix } = require('laravel-mix');
const webpack = require('webpack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
mix.js('resources/assets/js/admin.js', 'public/js')
mix.webpackConfig({
plugins: [
new BundleAnalyzerPlugin(),
new webpack.ProvidePlugin({ // Added as a suggestion. Makes no difference
'$': 'jquery',
'jQuery': 'jquery',
'window.jQuery': 'jquery'
})
],
});
admin.js
import 'jquery'
import 'toastr'
import 'jquery' in your entry file and add the below block to your webpack.config
plugins: [
new webpack.ProvidePlugin({
'$': 'jquery',
'jQuery': 'jquery',
'window.jQuery': 'jquery'
})
]
Refer: Webpack Provide Plugin
Related
I am attempting to use jQuery as $ in my webpack application's entry index.js file, and I am receiving this error when running my application in the browser:
Uncaught TypeError: Cannot read property 'fn' of undefined
This is due to a line in a module I am importing called bootstrap-switch, and the line in question is:
$.fn.bootstrapSwitch = function() {
So I do not have $ working as a global variable. I followed instructions on the ProvidePlugin docs, as shown below. I also tried the answer provided in this question but that didn't work.
This is an abbreviated version of my webpack.config.js file:
module.exports = {
entry: {
main: './src/index.js'
},
plugins: {
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
})
}
}
And this is my src/index.js file:
import 'bootstrap-switch';
console.log('I am running');
Any help would be much appreciated.
EDIT
A previous version of this question asked about a build error that was actually an ESLint error. Thank you to #UjinT34 for helping me resolve that problem and focus on the actual error outlined above.
I'm adding this as an answer for future users to find.
Try adding 'window.jQuery': 'jquery' to your webpack.ProvidePlugin() settings.
module.exports = {
...
plugins: [
new webpack.ProvidePlugin( {
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
} )
]
};
no-undef is a ESlint error. It doesn't know about your webpack setup. You can specify global variables in esling config:
{
"globals": {
"$": "readonly",
"jQuery": "readonly"
}
}
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
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
run below command to add jQuery.
$ yarn add jquery
Add below code in config/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.
require('jquery')
No more need to add jquery-rails gem!
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;
Apparently expose-loader 1.0.0 has a different format:
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.
First Run the following command to install Bootstrap, Jquery and Popper.js
yarn add bootstrap#4.5 jquery popper.js
On the above ofcourse you can change to the latest version of 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.
Next go to config/webpack/environment.js and amend the file.
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.
*= require bootstrap
*= require_tree .
*= require_self
Finally go to application.js file and amend the file by adding import 'bootstrap'; in order for bootstrap javascript to work.
import 'bootstrap';
require("#rails/ujs").start()
require("turbolinks").start()
require("#rails/activestorage").start()
Save all changes, restart rails server.
That should work.
In webpacker v. 6 there is no config/webpack/environment.js and other files structure
Firstly you need add JQuery to your project using yarn:
yarn add jquery
After that you can integrate JQuery using one of ways:
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
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
So Bootstrap 4 Beta is out... yey! However Tether has been replaced by Popper.js for tooltip (and other features). I saw an error thrown in the console fast enough to advise me of the change to Popper.js:
Bootstrap dropdown require Popper.js
Seems easy enough, I went and updated my webpack.config.js (the entire config can be seen here) and Bootstrap then started working (the only change I did was to replace Tether with Popper):
plugins: [
new ProvidePlugin({
'Promise': 'bluebird',
'$': 'jquery',
'jQuery': 'jquery',
'window.jQuery': 'jquery',
'window.$': 'jquery',
Popper: 'popper.js'
}),
I also did the import 'bootstrap' in my main.ts file.
However I now have another problem (which I did not have with Tether), a new error is thrown in the console:
Uncaught TypeError: Popper is not a constructor
If I try to debug in Chrome, I do have Popper loaded as an Object (which is why Bootstrap stopped complaining) as you can see in the print screen below.
Finally to include all my code. I use Bootstrap tooltip with a simple custom element built with Aurelia and TypeScript (which used to work with previous Bootstrap alpha 6 and Tether)
import {inject, customAttribute} from 'aurelia-framework';
import * as $ from 'jquery';
#customAttribute('bootstrap-tooltip')
#inject(Element)
export class BootstrapTooltip {
element: HTMLElement;
constructor(element: HTMLElement) {
this.element = element;
}
bind() {
$(this.element).tooltip();
}
unbind() {
$(this.element).tooltip('dispose');
}
}
Looks like I did not import Popper correctly, if so then what is the best way to achieve that with Webpack 3.x?
While browsing Bootstrap 4 documentation. I actually found a section about Webpack which explains how to install it correctly. Following the Bootstrap - installing with Webpack documentation, the answer is to simply modify the webpack.config.js with the following:
plugins: [
// ...
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
Popper: ['popper.js', 'default']
})
// ...
]
and let's not forget to import it in the main.ts
import 'bootstrap';
and voilĂ ! We are back in business :)
If you are using Webpack Do this:
window.$ = window.jQuery = require('jquery');
window.Popper = require('popper.js').default; // pay attention to "default"
require('bootstrap/dist/js/bootstrap');
In bootstrap": "^4.1.1" no need to import jquery and popper.js because those plugins will be already included when 'bootstrap' or bootstrap's plugins imported individually.
Notice that if you chose to import plugins individually, you must also
install exports-loader
No need to require files require('exports-loader?file ... '); as mentioned here because this will be taken care automatically by just installing $ npm install exports-loader --save-dev
import 'bootstrap'; // Import all plugins at once
//
// Or, import plugins individually
//
// import 'bootstrap/js/src/alert';
// import 'bootstrap/js/src/button';
// import 'bootstrap/js/src/carousel';
// import 'bootstrap/js/src/collapse';
// import 'bootstrap/js/src/dropdown';
// import 'bootstrap/js/src/modal';
// import 'bootstrap/js/src/popover';
// import 'bootstrap/js/src/scrollspy';
// import 'bootstrap/js/src/tab';
// import 'bootstrap/js/src/tooltip';
// import 'bootstrap/js/src/util';
There is no need to do anything like below:
const webpack = require('webpack');
module.exports = {
configureWebpack: {
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
Popper: ['popper.js', 'default']
})
]
}
}
I am a vue.js developer and in new vue-cli-3, we create vue.config.js in root and place code like above to register new plugin, but as said there is no need to do all this in bootstrap": "^4.1.1".
Bootstrap's tooltip plugin is depend on popper.js and need to be enabled manually, so you can do like below in the component where you use tooltip element:
<script>
import $ from 'jquery';
export default {
mounted() {
$('[data-toggle="tooltip"]').tooltip();
},
};
</script>
I just ran into the same issue, and the solution is described here: https://github.com/FezVrasta/popper.js/issues/287
My main.ts now looks like something like the following:
import "jquery";
import Popper from "popper.js";
(<any>window).Popper = Popper;
require("bootstrap");
And I had to run npm install #types/requirejs --save to get the call to require working.
EDIT: I totally missed this the first time around, but the documention actually has a better way to solve this https://getbootstrap.com/docs/4.0/getting-started/webpack/
plugins: [
...
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
Popper: ['popper.js', 'default'],
// In case you imported plugins individually, you must also require them here:
Util: "exports-loader?Util!bootstrap/js/dist/util",
Dropdown: "exports-loader?Dropdown!bootstrap/js/dist/dropdown",
...
})
...
]
In ASP.net Core 2 project add the following scripts to of main HTML file ("_Layout.cshtml" file)
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/js/popper.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.min.js"></script>
For me it's working.
I have defined webpack ProvidePlugin to load Backbone-relational library as follows.
plugins: [
new webpack.ProvidePlugin({
$ : "jquery",
Backbone : "backbone",
_ : "underscore",
"Backbone.Relational": "backbone-relational",
})
However Backbone-relational also exports:
Backbone.RelationalModel
Backbone.HasOne
Backbone.HasMany
etc...
My problem is that I can not have them all exported. What is the right way to do this with webpack?
Try to use relational shim as described here
I've been trying to provide my webpack compiled modules with grunt. All my files need at least 1 or 2 globals (React, Backbone and underscore).
These assets will be compiled into js, react views. I'm wondering how I can use ProvidePlugin to give all my modules some base packages, with webpack and/or grunt configs?
I have no clue where this code would live! Gruntfile.js? Entry js file?
plugins: [
webpack.ProvidePlugin({
"_": "underscore"
})
]
or
new webpack.ProvidePlugin({
$: "jquery"
})
I researched and found this is the closest:
Webpack ProvidePlugin vs externals?
Thank you very much for your help!
Figured it out! Ends up the grunt webpack config takes the same options? Looks like it :)
grunt.initConfig({
pkg: pkgConfig,
loyalty: loyaltyConfig,
webpack: {
development: {
// resolve: {
// modulesDirectories: [ 'vendors' ]
// },
amd: {
$: true
},
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
_: 'underscore',
React: 'react/addons',
config: 'json!../../config.json'
})
],
This will include the required elements if and only if they're used apparently.