gpt4 book ai didi

ruby-on-rails - 使用 Webpacker 和 Rails 导入 npm 包的图像

转载 作者:行者123 更新时间:2023-12-03 23:50:24 27 4
gpt4 key购买 nike

我正在尝试安装软件包 lightslideryarnwebpacker .

这是我到目前为止所做的:

yarn add lightslider
// app/webpacker/packs/application.js

require('lightslider');
// app/webpacker/src/application.scss

@import '~lightslider/dist/css/lightslider.min';

但是,由于以下错误,Webpack 编译失败:
Module not found: Error: Can't resolve '../img/controls.png'

这是lightslider正常工作所需的图像,位于 node_modules/lightslider/dist/img/controls.png :
// node_modules/lightslider/dist/css/lightslider.css

.lSAction > a {
width: 32px;
display: block;
top: 50%;
height: 32px;
background-image: url('../img/controls.png');
cursor: pointer;
position: absolute;
z-index: 99;
margin-top: -16px;
opacity: 0.5;
-webkit-transition: opacity 0.35s linear 0s;
transition: opacity 0.35s linear 0s;
}

我怎样才能在我的包中导入这个图像,以便 lightslider 找到它?

最佳答案

来自 WebpackER 文档:https://github.com/rails/webpacker/blob/76b491750993fada8b0b0cc2546dfcfbc4aaae13/docs/css.md#resolve-url-loader

Since Sass/libsass does not provide url rewriting, all linked assets must be relative to the output. Add the missing url rewriting using the resolve-url-loader. Place it directly after the sass-loader in the loader chain.



// webpack/environment.js
const { environment } = require('@rails/webpacker')

// resolve-url-loader must be used before sass-loader
environment.loaders.get('sass').use.splice(-1, 0, {
loader: 'resolve-url-loader'
});

我能够使用 @rails/webpacker@4.0.7 在没有/经过上述修改的情况下重现/修复您描述的问题和 resolve-url-loader@3.1.0 .

关于ruby-on-rails - 使用 Webpacker 和 Rails 导入 npm 包的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58727976/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com