简体   繁体   English

Npm package 包括 CSS 中的图像

[英]Npm package include images in CSS

I have an npm package with this structure:我有一个 npm package 结构如下:

--src
  --styles
     -image.png
     -style.scss

The style.scss file is referencing the image like this: style.scss文件引用了这样的图像:

.test {
   background-image: url(./image.png);
}

The problem is when I consume the package, CSS is looking the image from the root and not relative to my package, how we can solve this issue?问题是当我使用 package、CSS 时,正在从根目录查找图像,而不是相对于我的 package,我们如何解决这个问题?

This is how I'm importing the package:这就是我导入 package 的方式:

@import "mypackage/src/styles/style";

I have this issue too and I do not find a elegant way to this issue.我也有这个问题,我没有找到解决这个问题的优雅方法。 Finally I copied the referenced files to the build directory to solve this issue.最后我将引用的文件复制到构建目录以解决此问题。 The "copy-webpack-plugn" plugin was used( https://github.com/kevlened/copy-webpack-plugin )使用了“copy-webpack-plugn”插件( https://github.com/kevlened/copy-webpack-plugin

You may refer to the following issue too( Include assets from webpack bundled npm package )您也可以参考以下问题( 包括来自 webpack 捆绑的 npm 包的资产

All you need just install file loader like this.你只需要像这样安装文件加载器。

npm install file-loader --save-dev

and then add this line to module part in your config :然后将此行添加到配置中的模块部分:

module: {
        rules: [{
                 test: /\.(png|svg|jpg|gif)$/,
                 use: ['file-loader']
               }]
        }

I have had this issue recently and the information on StackOverflow was outdated (as expected, this is a question that relates to NPM & Web Development and everything moves fast in this space).我最近遇到了这个问题,并且 StackOverflow 上的信息已经过时(正如预期的那样,这是一个与 NPM 和 Web 开发相关的问题,并且在这个空间中一切都在快速发展)。

Fundamentally, this problem is solved entirely by Webpack 5 as it comes out of the box.从根本上说,这个问题完全由开箱即用的 Webpack 5 解决。 Only the webpack.config.js file needs to be updated.只需要更新webpack.config.js文件。

The part of webpack's documentation that relates to this is here: https://webpack.js.org/guides/asset-modules/与此相关的 webpack 文档部分在这里: https://webpack.js.org/guides/asset-modules/

What you want to do is Base64 encode your static assets and inline them to your CSS/JavaScript.您要做的是 Base64 编码您的 static 资产并将它们内联到您的 CSS/JavaScript。

Essentially, when you are packing up your source code to distribute it on NPM, and you have some CSS file which refers to static images as such: background-image: url(./image.png) what you need to do is inline your assets to your style file, and then process your style file with the style-loader and css-loader packages.本质上,当您打包源代码以将其分发到 NPM 上时,您有一些 CSS 文件,该文件引用 static 图像,例如: background-image: url(./image.png) assets 到您的样式文件,然后使用style-loadercss-loader包处理您的样式文件。

In short, making my webpack.config.js contain the lines below solved my issue and allowed me to export one single index.js file with my package, which I imported into my other projects.简而言之,使我的webpack.config.js包含以下行解决了我的问题,并允许我使用我的 package 导出一个index.js文件,该文件已导入到我的其他项目中。

What really matters here is the type: asset/inline line, when we are testing for images.这里真正重要的是type: asset/inline行,当我们测试图像时。

webpack.config.js
...
...

module: {
    rules: [
        {
          test: /\.(js|jsx)$/,
          exclude: /node_modules/,
          use: ["babel-loader"],
        },
        {
          test: /\.css$/i,
          use: ["style-loader", "css-loader"],
        },
        {
          test: /\.(png|jpg|gif)$/i,
          type: "asset/inline",
        },
      ],
    },
 
...
...

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

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