繁体   English   中英

如何使用 webpack / postcss @import '../Components/**/_partial.css' 将所有 CSS 文件导入子目录中;

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

使用 Webpack 和 PostCSS 环境,我想在我的 Components 文件夹和子目录中导入所有 CSS 文件。 我已经尝试过 PostCSS 插件 postcss-easy-import 和 postcss-import 并且知道它们都需要先来。 具有直接路径的常规导入工作,可能是 ** 占位符不起作用?

提前致谢

导入 css 或 scss 文件的最佳做法是将所有 css 文件或组件导入一个 main.css 或 scss 文件中。 然后将该文件导入到您的条目 *.js 文件中,最后在 webpack 配置文件中将该 *.js 文件设置为条目。

例如,如果您有一个名为SampleProject的文件夹以及如下文件夹和文件结构:

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

然后您可以在 main.css 文件中导入所有 3 个样式文件,如下所示:

主.css

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

然后您可以将 main.css 文件与其他 js 块一起导入 index.js,如下所示:

index.js

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

最后,您可以使用这个 index.js 作为 webpack 配置文件的入口点,如下所示:

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',
    }),

  ],
};

MiniCssExtractPlugin将自动提取您项目的所有 styles 并将它们作为一个文件保存在项目根目录下的build文件夹中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM