How do I import all CSS files ins subdirectory with webpack / postcss @import '../Components/**/_partial.css';

2.8k views Asked by At

Using a Webpack and PostCSS environment I would like to import all CSS files in my Components folder and the subdirectory. I've tried both PostCSS plugins postcss-easy-import & postcss-import and know they both need to come first. Regular Imports with direct paths work, could it be that ** placeholders don't work?

Thanks in advance

1

There are 1 answers

3
Aspian On

The best practice to import css or scss files is importing all your css files or components in one main.css or scss file. Then import that file in your entry *.js file and finally set that *.js file in your webpack config file as an entry.

For example if you have a folder called SampleProject and a folder and file structure like this:

SampleProject
|
|--css
|  |--components
|  |  |--style1.css
|  |  |--style2.css
|  |  |--style3.css
|
|--js
|  |--components
|  |  |--script1.js
|  |  |--script2.js
|
main.css
index.js
index.html
webpack.prod.js
package.json

Then you can import all you 3 style files in your main.css file like below:

main.css

@import 'css/components/style1.css';
@import 'css/components/style2.css';
@import 'css/components/style3.css';

Then you can import the main.css file into index.js with other js chunks like this:

index.js

import './main.css';
import 'js/components/script1.js';
import 'js/components/script2.js';

Finally, you can use this index.js as an entry point into your webpack config file like this:

webpack.prod.js:

const webpack = require('webpack');
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const TerserJSPlugin = require('terser-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');

module.exports = {
  entry: {
    main: './index.js'
  },
  output: {
    path: path.join(__dirname, './build'), 
    filename: '[name].[chunkhash:8].bundle.js',
    chunkFilename: '[name].[chunkhash:8].chunk.js',
  },
  mode: 'production',
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader', 
        },
      },
      {
        test: /\.(sa|sc|c)ss$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader', 
          'postcss-loader', 
          //'sass-loader',
        ],
      },
      {
        test: /\.(png|svg|jpe?g|gif)$/i,
        use: [
          {
            loader: 'file-loader', 
            options: {
              name: '[name].[ext]',
              outputPath: 'assets/img',
              esModule: false,
            },
          },
        ],
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/,
        use: [
          {
            loader: 'file-loader', 
            options: {
              name: '[name].[ext]',
              outputPath: 'assets/font',
            },
          },
        ],
      },
      {
        test: /\.html$/i,
        use: {
          loader: 'html-loader',
          options: {
            attributes: {
              list: [
                {
                  tag: 'img',
                  attribute: 'src',
                  type: 'src',
                },
                {
                  tag: 'img',
                  attribute: 'srcset',
                  type: 'srcset',
                },
                {
                  tag: 'img',
                  attribute: 'data-src',
                  type: 'src',
                },
                {
                  tag: 'img',
                  attribute: 'data-srcset',
                  type: 'srcset',
                },
              ],

            },
          },
        },
      },
    ],
  },
  optimization: {
    minimizer: [new TerserJSPlugin(), new OptimizeCSSAssetsPlugin()],
    splitChunks: {
      cacheGroups: {
        commons: {
          test: /[\\/]node_modules[\\/]/,
          name: 'vendors',
          chunks: 'all',
        },
      },
      chunks: 'all',
    },
    runtimeChunk: {
      name: 'runtime',
    },
  },
  plugins: [
    // load jQuery
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
    }),

    new CleanWebpackPlugin(),

    new MiniCssExtractPlugin({
      filename: '[name].[chunkhash:8].bundle.css',
      chunkFilename: '[name].[chunkhash:8].chunk.css',
    }),

    new HtmlWebpackPlugin({
      chunks: ['main'],
      template: 'index.html',
      filename: 'index.html',
    }),

  ],
};

And MiniCssExtractPlugin will extract all styles of your project automatically and save them as one file in build folder in root of your project.