我需要导入到我的项目与网页5的一些CSS文件,我需要内联所有这些资源(这是一个遗憾的要求).CSS中有一些字体和图像具有相对URI,如下所示:

@font-face { font-family: "MyFont"; src: url(./fonts/Roboto-Regular.ttf) format("truetype"); font-weight: normal;}
@font-face { font-family: "MyFont"; src: url(./fonts/Roboto-Bold.ttf) format("truetype"); font-weight: bold;}
@font-face { font-family: "MyFont"; src: url(./fonts/Roboto-Italic.ttf) format("truetype"); font-weight: normal; font-style: italic;}
@font-face { font-family: "MyFont"; src: url(./fonts/Roboto-BoldItalic.ttf) format("truetype"); font-weight: bold; font-style: italic;}
@font-face { font-family: 'Material Icons'; font-style: normal; font-weight: 400; src: url(./fonts/material-icons.woff2) format('woff2'); }
@font-face { font-family: 'Material Icons Outlined'; font-style: normal; font-weight: 400; src: url(./fonts/material-icons-outlined.woff2) format('woff2'); }
    

 * { font-family: "MyFont", "Roboto-Light", "Noto Sans CJK SC", "DejaVu Sans"; }

.UICheckbox         { width:80px; height:89px; background-image:url("img/checkboxOFF.png"); background-repeat:no-repeat; }
.UICheckbox.checked { background-image:url("img/checkboxON.png"); }

由于我需要将CSS文件作为base64导入,因此实际上无法自动处理其中的资源(与PostCs或类似文件的处理方式相反).

      {
        test: /\.(png|jpg|gif)$/i,
        type: "asset/inline",
      },
      {
        test: /\.(woff|woff2|eot|ttf|otf)$/i,
        type: "asset/inline",
      },
      {
        test: /\.css$/i,
        type: "asset/inline",
      },

有更好的方法处理这个问题吗?

推荐答案

我找到了一个解决方案,它不是真正通用的或可靠的,但至少在我的 case 场景中可以做到这一点.

导入与固定的源路径相关,因此我们的 idea 是读取在url()个规则中找到的资源,并在base64编码中将其作为DataURI进行处理.

我发现使用datauri非常有用,它提供了一种方法,可以像外部资源一样在线包含数据,并自动管理模拟类型.

npm install datauri --save

然后,我不得不修改webpack.config.js中的生成器处理程序,以利用datauri包手动处理资源.

const path = require("path");
const Datauri = require("datauri/sync");

const EXTERNAL_ROOT_PATH = "./src/external/dev/";

module.exports = {
  ...

  module: {
    rules: [
       {
        test: /\.css$/i,
        type: "asset/inline",
        generator: {
          dataUrl: (content) => {
            content = content.toString();

            // Get the resource paths inside the CSS url() rules
            let asset_urls = [];
            let match,
              regex = /url\((.*?)\)/gi;
            while ((match = regex.exec(content))) {
              asset_urls.push(match[1]);
            }
            // console.log(asset_urls);

            // Convert the resource to a DataURI and replace it inside url()
            asset_urls.forEach((file_path) => {
              // Sanitize the file path first
              sanitized_file_path = file_path.replace(/[\"\']/g, "").replace(/^(?:\.\.\/)+/, "");

              const data_uri = Datauri(path.join(EXTERNAL_ROOT_PATH, sanitized_file_path));
              // console.log(data_uri.content); //=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA..."
              // console.log(data_uri.mimetype); //=> "image/png"
              // console.log(data_uri.base64); //=> "iVBORw0KGgoAAAANSUhEUgAA..."
              // console.log(data_uri.buffer); //=> file buffer

              content = content.replace(file_path, data_uri.content);
            });

            return "data:text/css;base64," + Buffer.from(content).toString("base64");
          },
        },
      },
    ],
  },
};

Css相关问答推荐

如何以Angular 调整下拉控件的高度

如何在CSS中平滑线性平移动画

显示Reaction组件上的问题.使用FlexBox时的意外大小

媒体查询在生产中没有响应:React 18 应用程序

使用普通 CSS 的视口对角线长度

CSS:使用 Material Icon 作为 ::marker 或 ::before 使 li 元素不起作用

如何减少弹性项目之间的差距

来自 A 队和 B 队的类污染了同一个全局命名空间

使用 styled-components,我如何定位组件的子类?

Angular Datepicker - 更改日期范围 Select 样式

使用 React-Router 和 Outlet 时如何确保内容保持在下方

选中时从 HTML 文本输入中移除蓝色光晕

禁用输入的 CSS Select 器 type="submit"

CSS3 变换:旋转;在 IE9 中

如何将 bootstrap 列高设为 100% 行高?

带有 flexbox 的 css 正方形网格

CSS3 box-shadow:inset 可以只做一侧或两侧吗?喜欢边界顶部?

打印html时在页面上打印页码

将 CSS 类添加到 Html.BeginForm()

CSS属性中的auto值是什么意思.