Hello,
I’m working on an isomorphic React app, in which I would like to require jQuery modules from Semantic UI. Each module adds itself to jQuery by accessing the global variable. I have spent hours scouring pretty much every corner of Github and Stackoverflow (and other random websites) looking for solutions, but so far, not a single one has worked.
Methods I have tried:
- ProvidePlugin for $, jQuery, and window.jQuery – if I understand correctly, this provides new instances of jQuery each call so the window does not reflect any changes to the object made in required files
- expose loader (https://github.com/webpack/expose-loader) – crashes dev server upon usage
- imports loader (http://reactkungfu.com/2015/10/integrating-jquery-chosen-with-webpack-using-imports-loader/) – does absolutely nothing
- various other combinations of requiring jQuery at different locations in my stack
I’m clearly missing something here. Can someone point me to a (working) tutorial or explain the process by which I could get this working? I’ve spent so many hours on it now that I gave up and included both libraries manually…which completely defeats the purpose of converting to webpack anyway.
Thanks.
My webpack.config.js (for development)
var webpack = require('webpack');
var path = require('path');
var webpackConfig = {
resolve: {
extensions: ['', '.js', '.jsx', '.less', '.css'],
resolve: {
alias: {
jquery: "jquery/src/jquery"
}
}
},
entry: [
'webpack-dev-server/client?http://localhost:3000',
'webpack/hot/only-dev-server',
'./client.js'
],
output: {
path: path.resolve('./dist'),
publicPath: '/public/',
filename: 'dev.js'
},
module: {
loaders: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loaders: [
require.resolve('react-hot-loader'),
require.resolve('babel-loader'),
]
},
{
test: /\.css$/,
loader: "style-loader!css-loader"
},
{
test: /\.less$/,
loader: "style!css!less"
},
{
test: /\.json$/,
loader: 'json-loader'
}
]
},
node: {
setImmediate: false
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(process.env.NODE_ENV),
BROWSER: JSON.stringify(true)
}
})
// ,
// new webpack.ProvidePlugin({
// $: "jquery",
// jQuery: "jquery"
// }),
],
devtool: 'eval'
};
module.exports = webpackConfig;
window.$ = window.jQuery = require(“jquery”);
This always worked for me… Just do this in your entry and you should be golden.
Could make it work, but without explicit jQuery import/quire. It is global. Tried 1.000 variations and its the cleanest working one I’v found.
ProvidePlugin. mapped global variables to jquery2 node lib.
module.loaders, you might need to adjust your include regex.
see the last loader, that does the trick, make jquery,$ available at the global level. I am using “webpack”: “1.13.2”,
“webpack-dev-server”: “^1.16.1”
Have you tried using externals in lieu of the expose loader?