【问题标题】:How do I import all CSS files ins subdirectory with webpack / postcss @import '../Components/**/_partial.css';如何使用 webpack / postcss @import '../Components/**/_partial.css' 将所有 CSS 文件导入到子目录中;
【发布时间】:2020-07-24 11:05:31
【问题描述】:

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

提前致谢

【问题讨论】:

    标签: css webpack postcss webpack-style-loader postcss-import


    【解决方案1】:

    导入 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 个样式文件,如下所示:

    main.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 会自动提取您项目的所有样式,并将它们作为一个文件保存在项目根目录下的build 文件夹中。

    【讨论】:

    • 非常感谢鲁哈尼的详细回答。我只是想知道是否可以做一个通用的 @import '../Components/**/_partial.css';在 PostCSS 中没有引用每个 css 文件。我以前经常使用 SASS/SCSS,可以这么说,它不需要任何额外的脚本就可以工作。我确实有一个主 css 文件,然后我将其加载到我的 js 中,但我喜欢使用可重用组件,而不必考虑导入 css 文件...
    • 不客气。我只是想帮你。是的,在编程中总有一种方法可以做所有事情,但是当涉及到 webpack 时,你不能轻易找到稀有的技巧。如果您有问题,我认为也检查一下这个问题可能是个好主意。
    • Stackoverflow中提到的问题及其答案链接:stackoverflow.com/questions/40356239/…
    猜你喜欢
    • 2016-05-09
    • 2018-03-05
    • 1970-01-01
    • 2017-09-03
    • 2016-04-20
    • 2017-11-12
    • 2017-03-14
    • 1970-01-01
    • 2018-03-03
    相关资源
    最近更新 更多