Webpack 停止使用 postcss-variables-loader 加载 CSS 变量

发布于 2025-01-13 08:26:52 字数 2306 浏览 3 评论 0原文

我正在将基于旧版本的 webpack 应用程序迁移到一些新版本,但我在加载 variable.css 时遇到了麻烦,现在它似乎不再被识别

You may need an appropriate loader to handle this file type, 
currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> :root {
|   --monospace-font-family: 'Consolas', 'Inconsolata', 'Droid Sans Mono',
|     monospace;
    "css-loader": "2.1.1",
    "postcss-loader": "3.0.0",
    "typescript": "4.0.3",
    "webpack": "4.30.0",
    "webpack-dev-server": "3.11.0",

旧的 webpack.config.js 曾经是:

      test: /\.css$/,
          exclude: /variables\.css$/,
          use: ExtractTextPlugin.extract({
            fallback: 'style-loader',
            use: [{
                loader: 'css-loader', options: { sourceMap: true, },
              },
              'postcss-loader',
            ],}),
        },
        {
          test: /variables\.css$/,
          loader: 'postcss-variables-loader?es5=1',
        }

现在,我使用了新版本

    "postcss": "^8.4.8",
    "postcss-loader": "^6.2.1",
    "mini-css-extract-plugin": "^2.6.0",
    "typescript": "^4.6.2",
    "webpack": "^5.70.0",
    "webpack-dev-server": "^4.7.0",

,我必须更改 che webpack.config.js 以包含新的 mini-css-extract-plugin

        {
          test: /\.css$/i,
          exclude: /variables\.css$/,
          use: [MiniCssExtractPlugin.loader, 'css-loader','postcss-loader',],
        },
        {
          test: /\.variables.css$/,
          use: [ {loader: 'postcss-variables-loader?es5=1',},],
        },

文件variable.css包含一些 css 基本 var 之类的内容

:root {
  --monospace-font-family: 'Consolas', 'Inconsolata', 'Droid Sans Mono',
    'Monaco', monospace;
}

@custom-media --small-viewport (max-width: 780px);

,并且使用它导入到各种 .tsx 文件中,

import variables from './variables.css';

我无法理解 css 的整个结构及其加载方式是否必须更改,或者我可能还需要一个修复。

I am migrating a webpack application based on old versions to some new ones and I am getting troubles with the load of variable.css which now seems not recognized anymore

You may need an appropriate loader to handle this file type, 
currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> :root {
|   --monospace-font-family: 'Consolas', 'Inconsolata', 'Droid Sans Mono',
|     monospace;
    "css-loader": "2.1.1",
    "postcss-loader": "3.0.0",
    "typescript": "4.0.3",
    "webpack": "4.30.0",
    "webpack-dev-server": "3.11.0",

Old webpack.config.js used to be:

      test: /\.css$/,
          exclude: /variables\.css$/,
          use: ExtractTextPlugin.extract({
            fallback: 'style-loader',
            use: [{
                loader: 'css-loader', options: { sourceMap: true, },
              },
              'postcss-loader',
            ],}),
        },
        {
          test: /variables\.css$/,
          loader: 'postcss-variables-loader?es5=1',
        }

Now, I am with new versions

    "postcss": "^8.4.8",
    "postcss-loader": "^6.2.1",
    "mini-css-extract-plugin": "^2.6.0",
    "typescript": "^4.6.2",
    "webpack": "^5.70.0",
    "webpack-dev-server": "^4.7.0",

and I had to change che webpack.config.js to include the new mini-css-extract-plugin

        {
          test: /\.css$/i,
          exclude: /variables\.css$/,
          use: [MiniCssExtractPlugin.loader, 'css-loader','postcss-loader',],
        },
        {
          test: /\.variables.css$/,
          use: [ {loader: 'postcss-variables-loader?es5=1',},],
        },

The file variable.css includes some css basic var like

:root {
  --monospace-font-family: 'Consolas', 'Inconsolata', 'Droid Sans Mono',
    'Monaco', monospace;
}

@custom-media --small-viewport (max-width: 780px);

and it is imported into various .tsx files using just

import variables from './variables.css';

I cannot understand if the entire structure of css and the way they are loaded must change or there's one more fix I might need.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无远思近则忧 2025-01-20 08:26:52

问题不在于加载器,而在于测试。缺少一个反斜杠。

应该是:

test: /\.variables\.css$/

The trouble is not the loader, but the test. A backslash was missing.

It should be:

test: /\.variables\.css$/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文