Answer the question
In order to leave comments, you need to log in
How to make friends Webpack + jQuery + jQuery plugins?
Hello!
How to make jquery available in the global scope for plugins and how to actually connect / call these plugins in modules without messing up the paths?
jQuery would like to be included as a module, not a tag.
When building what is, it gives:
ERROR in ./blocks/slider/index.js
Module not found: Error: Cannot resolve 'file' or 'directory' ./vendor/jquery.js in D:\DEV\webpackbuilder\frontend\ blocks\slider
@ ./blocks/slider/index.js 1:0-29
There is structure and code:
index.js:
blocks
> slider
>> index.js:
require('./blocks/slider');
require('../../vendors/jquery.js');
require('../../vendors/jquery.slider.js');
var NODE_ENV = process.env.NODE_ENV || 'dev',
webpack = require('webpack'),
path = require('path');
module.exports = {
context: path.resolve(__dirname, "frontend"),
entry: {
index: "./index.js",
about: "./about.js",
},
output: {
publicPath: "./public/built/",
path: "./public/built/",
filename: "[name].js"
},
watch: NODE_ENV == 'dev',
devtool: NODE_ENV == 'dev'? 'cheap-inline-module-source-map' : null,
plugins: [
new webpack.EnvironmentPlugin('NODE_ENV'),
new webpack.NoErrorsPlugin(),
new webpack.ProvidePlugin({
$: "./vendor/jquery.js"
/*,
jQuery: "jquery",
"window.jQuery": "jquery"*/
})
],
module: {
loaders: [
{
test: /\.css$/,
loader: "style!css"
},
{
test: /\.styl$/,
loader: 'style!css!stylus'
},
{
test: /\.(png|jpg|svg|ttf|eot|woff|woff2)$/,
loader: 'file-loader?name=[name].[ext]'
}
],
},
stylus: {
use: [require('kouto-swiss')()],
import: ['~kouto-swiss/index.styl', __dirname + '/frontend/commons/vars.styl', __dirname + '/frontend/commons/mixins.styl']
}
};
if (NODE_ENV == 'prod') {
module.exports.plugins.push(
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
})
)
}
Answer the question
In order to leave comments, you need to log in
It was enough for me to include jQuery in the plugin
plugins: [
new webpack.ProvidePlugin({
$: "jquery/dist/jquery.min.js",
jQuery: "jquery/dist/jquery.min.js",
"window.jQuery": "jquery/dist/jquery.min.js"
})
];
resolve: {
root: ['./bower_components']
}
require('../../vendors/jquery.js')
inside blocks\slider\index.js will no longer be needed, it will be enough just to include the jquery.slider.js file
Alexey Zuev : Thank you.
I figured it out differently, I used the script loader
require('script!../../vendor/slider/jquery.slider.js');
I was still able to connect jQuery, everything was
already so mixed up that I don’t remember exactly, but it seems that I
first installed it
later in the main file - the entry point:
and then in webpack.config.js
$: 'jquery',
'$': 'jquery',
jquery: 'jquery',
jQuery: 'jquery',
'window.jquery': 'jquery',
'window.jQuery': 'jquery',
(function($) {
"use strict"; // Start of use strict
// jQuery for page scrolling feature - requires jQuery Easing plugin
$('a.page-scroll').bind('click', function(event) {
var $anchor = $(this);
$('html, body').stop().animate({
scrollTop: ($($anchor.attr('href')).offset().top - 50)
}, 1250, 'easeInOutExpo');
event.preventDefault();
});
// Offset for Main Navigation
$('#mainNav').affix({
offset: {
top: 100
}
})
})(jQuery);
So after all, how to correctly connect scripts to the global scope via webpack?
while version 1 is of interest, but it would be nice to understand version 2
on stackoverflow, and not only there, that even jquery, not to mention modular libraries, is now not connected via webpack.ProvidePlugin, which didn’t work for me either, through the script loader, it is strictly not recommended to do this in the documentation
, how to be?
Webpack 2: https://webpack.js.org/plugins/provide-plugin/
Install jquery via npm and enjoy
In new webpack.ProvidePlugin add
You have registered
And all calls are made throughwindow.$
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question