webpack - PhotoSwipe 和 Webpack : Can't resolve path

标签 webpack photoswipe webpacker

我已将 Webpacker 安装到我的 Rails 应用程序中,并且只是尝试对其进行全部配置。我的自定义 CSS 中的所有图像都能正确解析,但我在使用 Photoswipe 时遇到问题。

Photoswipe 作为节点模块安装。我的主 stylesheets.scss 文件开始如下:

@import "node_modules/susy/sass/susy";
@import "node_modules/include-media/dist/include-media";
@import "node_modules/sass-toolkit/stylesheets/toolkit";
@import "node_modules/bourbon/app/assets/stylesheets/bourbon";

@import "../src/stylesheets/base/variables";

@import "node_modules/photoswipe/src/css/main";
@import "node_modules/photoswipe/src/css/default-skin/default-skin";

我的变量文件在顶部设置了以下 Photoswipe 变量:

$pswp__show-hide-transition-duration: 333ms;
$pswp__controls-transition-duration: 333ms;
$pswp__background-color: #000;
$pswp__placeholder-color: #222;
$pswp__box-sizing-border-box: true; // disable .pswp * { box-sizing:border-box } (in case you already have it in your site css)
$pswp__root-z-index: 1500;
$pswp__assets-path: ''; // path to skin assets folder (preloader, PNG and SVG sprite)
$pswp__error-text-color: #CCC; // "Image not loaded" text color
$pswp__include-minimal-style: true;

我尝试将 $pswp__assets-path 更改为 allsorts,希望能够解决此问题,但无论我尝试做什么,都无法解决它。

我收到的错误是:

ERROR in ./app/webpacker/packs/stylesheets.scss Module build failed: ModuleNotFoundError: Module not found: Error: Can't resolve './default-skin.png' in '/vagrant/app/webpacker/packs'

有人能帮我解决这个分辨率问题吗?

我几乎尝试了所有我能想到的路径,但仍然无法找到该文件。图像 100% 也存在。

我应该从哪里引用它?包含的scss文件?、scss入口点文件、实际引用图像的scss文件?

我已经尝试了这三种方法,但现在却感到沮丧。

最佳答案

Webpack's sass-loader在其自述文件中声明以下内容:

Problems with url(...)

Since Sass implementations don't provide url rewriting, all linked assets must be relative to the output.

  • If you pass the generated CSS on to the css-loader, all urls must be relative to the entry-file (e.g. main.scss).
  • If you're just generating CSS without passing it to the css-loader, it must be relative to your web root.

You will be disrupted by this first issue. It is natural to expect relative references to be resolved against the .sass/.scss file in which they are specified (like in regular .css files). Thankfully there are a two solutions to this problem:

  • Add the missing url rewriting using the resolve-url-loader. Place it before sass-loader in the loader chain.
  • Library authors usually provide a variable to modify the asset path. bootstrap-sass for example has an $icon-font-path.

值得庆幸的是,PhotoSwipe 还提供了一种指定资源路径的方法。以下内容适用于我的案例。

$pswp__assets-path: "../node_modules/photoswipe/src/css/default-skin/";
@import "~photoswipe/src/css/main";
@import "~photoswipe/src/css/default-skin/default-skin";

关于webpack - PhotoSwipe 和 Webpack : Can't resolve path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50589943/

相关文章:

javascript - Photoswipe 无法按预期工作

javascript - Webpacker 重构后 react 组件不呈现

javascript - 将 python TensorFlow Layers 模型加载到 JavaScript 中

javascript - 为什么 'Error: Couldn' t 在 ReactJS for React Hot Loader 中找到相对于目录的预设 "react-hot"...”?

javascript - webpack 可以为浏览器和 Node 输出单独的脚本和模块文件吗?

html - 联机时,Photoswipe Gallery导航按钮不起作用

javascript - jQuery 从标准 href 链接 CSS/HTML 触发 PhotoSwipe 独占模式

javascript - Rails,webpacker : where/when to use the import statement in classes, 以及在 pack/application.js 中要求的目的是什么?

node.js - 将RoR项目推送到服务器后,出现Node.js版本错误【Buildpack问题】

javascript - 如何防止 webpack 为 css 创建 js 文件?