样式加载器已使用与 API 架构不匹配的选项对象进行初始化

发布于 2025-01-10 17:48:42 字数 1414 浏览 1 评论 0原文

我对编程非常陌生。因此,请阅读几篇文章并尝试配置 web-pack 以实现以下目的。

我正在构建一个 preact 小部件,CSS 与父网站发生冲突。因此,我尝试创建一个 Shadow DOM,然后将 css 加载到 Shadow DOM 中,而不是添加到文档标题中。

我创建了一个 Shadow DOM,并配置了 webpack 将样式注入到 Shadow 根中,如下所示。

rules: [
        // packs SVG's discovered in url() into bundle
        { test: /\.svg/, use: 'svg-url-loader' },
        {
          test: /\.css$/i,
          use: [
            {
              loader: require.resolve('style-loader'),
              options: {
                insertInto: function () {
                  return document.getElementById('#widget-_hw').shadowRoot;
                },
              },
            },
            {
              // allows import CSS as modules
              loader: 'css-loader',
              options: {
                modules: {
                  // css class names format
                  localIdentName: '[name]-[local]-[hash:base64:5]'
                },
                sourceMap: isDevBuild
              }
            }
          ]
   

但我收到以下错误

Module build failed (from ./node_modules/style-loader/dist/cjs.js):
ValidationError: Invalid options object. Style Loader has been initialized using an options object that does not match the API schema.
 - options has an unknown property 'insertInto'. These properties are valid:
   object { injectType?, attributes?, insert?, base?, esModule? }

I am a very new to programming. So read a few articles and trying to configure web-pack for the following purpose.

I'm building a preact widget and the CSS is clashing with parent website. Hence I'm trying to create a shadow DOM and then load css into shadow DOM instead of adding into the document header.

I have created a shadow DOM and I have configured webpack to inject styles into the shadow root like below.

rules: [
        // packs SVG's discovered in url() into bundle
        { test: /\.svg/, use: 'svg-url-loader' },
        {
          test: /\.css$/i,
          use: [
            {
              loader: require.resolve('style-loader'),
              options: {
                insertInto: function () {
                  return document.getElementById('#widget-_hw').shadowRoot;
                },
              },
            },
            {
              // allows import CSS as modules
              loader: 'css-loader',
              options: {
                modules: {
                  // css class names format
                  localIdentName: '[name]-[local]-[hash:base64:5]'
                },
                sourceMap: isDevBuild
              }
            }
          ]
   

But I'm getting the following error

Module build failed (from ./node_modules/style-loader/dist/cjs.js):
ValidationError: Invalid options object. Style Loader has been initialized using an options object that does not match the API schema.
 - options has an unknown property 'insertInto'. These properties are valid:
   object { injectType?, attributes?, insert?, base?, esModule? }

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

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

发布评论

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

评论(1

素衣风尘叹 2025-01-17 17:48:42

该错误准确地告诉您出了什么问题:insertInto 不是 style-loader 上的有效选项。

insert(错误消息告诉您这是有效选项之一)可能就是您想要的。

The error tells you exactly what's wrong: insertInto is not a valid option on style-loader.

insert, which the error message tells you is one of the valid options, is likely what you want.

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