I’m writing a plugin on native JS and encountered a problem. When the development, Webpack works properly, I create a class and call it elsewhere. But I don’t need to call a class in production, and the Webpack just doesn’t process that code. How to force it to process code if this code is not called
const path = require('path');
const HTMLWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const isDev = process.env.NODE_ENV === 'development';
const isProd = !isDev;
const fileName = ext => isDev ? `[name].${ext}` : `script.${ext}`;
module.exports = {
context: path.resolve(__dirname, 'src'),
mode: 'development',
entry: {
script: './index.js'
},
output: {
filename: `js/${fileName('js')}`,
path: path.resolve(__dirname, 'dist'),
publicPath: ''
},
devServer: {
historyApiFallback: true,
contentBase: path.resolve(__dirname, 'dist'),
open: true,
compress: true,
hot: true,
port: 8080,
},
plugins: [
new CleanWebpackPlugin(),
new HTMLWebpackPlugin({
template: path.resolve(__dirname, 'src/index.html'),
filename: 'index.html',
}),
new MiniCssExtractPlugin({
filename: `css/${fileName('css')}`
}),
new CopyWebpackPlugin({
patterns: [{
from: path.resolve(__dirname, 'src/img'),
to: path.resolve(__dirname, 'dist/img')
}]
})
],
devtool: isProd ? false : 'source-map',
module: {
rules: [
{
test: /.html$/i,
loader: 'html-loader',
options: {
minimize: {
removeComments: false,
collapseWhitespace: false,
},
},
},
{
test: /.css$/i,
use: [{
loader: MiniCssExtractPlugin.loader,
options: {
hmr: isDev,
publicPath: '/'
}
},
'css-loader',
],
},
{
test: /.s[ac]ss$/i,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '/'
}
},
'css-loader',
'postcss-loader',
'sass-loader'
]
},
{
test: /.js$/i,
use: ['babel-loader'],
exclude: /node_modules/
},
{
test: /.(?:|gif|png|jpg|jpeg|svg)$/,
use: [{
loader: 'file-loader',
options: {
name: 'img/[name].[ext]',
}
}],
}
]
}
}