Answer the question
In order to leave comments, you need to log in
How to bring plugins into global scope in webpack?
I'm trying to bring plugins into the global scope via providePlugin, but it doesn't work. It displays everything in index.js, but if I access plugins from other files, I don't see the plugins.
// index.js
console.log($) - это выведет
import './item.js';
// item.js
console.log($) -уже не видит
const path = require('path');
const webpack = require('webpack');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const autoprefixer = require('autoprefixer');
const isDev = process.env.NODE_ENV === 'development'
const isProd = !isDev
const plugins = () => {
const base = [
new CleanWebpackPlugin(),
new MiniCssExtractPlugin({
filename: '[name].bundle.css',
}),
new webpack.ProvidePlugin({
'window.jQuery': 'jquery',
'window.$': 'jquery',
jQuery: 'jquery',
$: 'jquery',
'window.Swiper': 'swiper',
Swiper: 'swiper',
}),
]
return base;
}
module.exports = {
mode: 'development',
entry: ["./js/index.js", "./scss/template.scss"],
output: {
filename: '[name].bundle.js',
path: path.resolve(__dirname, 'dist')
},
optimization: {
splitChunks: {
chunks: 'all',
name: 'vendor'
}
},
devtool: isDev ? 'source-map' : false,
plugins: plugins(),
module: {
rules: [
{
test: /\.css$/,
use: ['css-loader']
},
{
test: /\.(sass|scss)$/,
include: path.resolve(__dirname, "./scss"),
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {},
},
{
loader: "css-loader",
options: {
sourceMap: true,
url: false,
},
},
{
loader: 'postcss-loader',
options: {
sourceMap: true,
postcssOptions: {
config: path.resolve(__dirname, 'postcss.config.js'),
}
}
},
{
loader: "sass-loader",
options: {
sourceMap: true,
},
},
],
},
{
test: /\.m?js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ['@babel/preset-env']
}
}
},
]
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question