Answer the question
In order to leave comments, you need to log in
How to separate css files in webpack?
Started disassembling webpack, and ran into a problem. For example, in the index.js file, I include normalize and bootstrap.css, but they are combined into one vendors~main.css file. How to separate them?
Code with webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const isDev = process.env.NODE_ENV === "development";
const isProd = process.env.NODE_ENV === "production";
module.exports = {
mode: 'development', // метод по умолчанию
// context: path.resolve(__dirname, "src"), // папка исходных файлов
entry: {
main: ["@babel/polyfill", './src/index.js'],
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'assets'),
},
resolve: {
alias: {
"@": path.resolve(__dirname, "src"), // теперь @ - ссылка на путь корневой папки, если прописывать в import в index.js
}
},
optimization: {
splitChunks: {
// chunks: "all", // разделить подключенные файлы
cacheGroups: {
styles: {
name: 'styles',
test: /\.css$/,
chunks: 'all',
enforce: true,
},
},
}
},
devtool: 'inline-source-map',
devServer: {
contentBase: './assets',
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
template: "./src/index.html",
minify: {
collapseWhitespace: isProd,
}
}),
new MiniCssExtractPlugin({
filename: '/styles/css/[name].css',
chunkFilename: '/styles/css/[name].css',
}),
new CopyPlugin([
{ from: 'src/styles/scss/', to: 'styles/scss/' },
// { from: 'src/fonts/', to: 'fonts/' },
// { from: 'src/images/', to: 'images/' },
// { from: 'src/files/', to: 'files/' },
]),
],
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: {
loader: "babel-loader",
options: {
presets: [
"@babel/preset-env"
]
}
}
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader',
{
loader: "postcss-loader",
options: {
sourceMap: true,
config: {
path: "src/js-config/postcss.config.js"
}
}
},
],
},
{
test: /\.s[ac]ss$/i,
use: [
'style-loader',
MiniCssExtractPlugin.loader,
{
loader: 'css-loader',
options: {
sourceMap: true,
},
},
{
loader: "postcss-loader",
options: {
sourceMap: true,
config: {
path: "src/js-config/postcss.config.js"
}
}
},
{
loader: 'sass-loader',
options: {
sourceMap: true,
},
},
],
},
{
test: /\.(png|svg|jpg|jpeg|gif)$/,
use: [
'file-loader',
],
},
{
test: /\.(woff|woff2|eot|ttf|otf)$/,
use: [
{
loader: 'file-loader',
options: {
// context: 'public',
name: 'assets/images/[name].[ext]?v=[hash]',
// publicPath: './',
},
},
],
},
{
test: /\.(csv|tsv)$/,
use: [
'csv-loader',
],
},
{
test: /\.xml$/,
use: [
'xml-loader',
],
},
],
},
};
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