简体   繁体   English

没有资产管道的 Rails 6.1 sass-loader (webpacker)

[英]Rails 6.1 sass-loader without asset pipeline (webpacker)

Auto-including all nested SCSS files with Rails 6.1 and sass-loader使用 Rails 6.1 和 sass-loader 自动包含所有嵌套的 SCSS 文件

With Rails 6.0.3.4 it was possible to use Sass-loader to glob all SCSS files without including each and every one manually.在 Rails 6.0.3.4 中,可以使用 Sass-loader 来 glob 所有 SCSS 文件,而无需手动包含每个文件。

Problem - syntax v7问题 - 语法 v7

This fails in Rails 6.1 with the following webpacker error (with webpacker config, see below, .options = { importer: globImporter() }; // syntax for sass-loader v7 )这在 Rails 6.1 中失败,出现以下 webpacker 错误(使用 webpacker 配置,见下文, .options = { importer: globImporter() }; // syntax for sass-loader v7

ERROR in ./app/javascript/packs/application.scss (./node_modules/css-loader/dist/cjs.js??ref--6-1!./node_modules/postcss-loader/src??ref--6-2!./node_modules/sass-loader/dist/cjs.js??ref--6-3!./app/javascript/packs/application.scss)
Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
ValidationError: Invalid options object. Sass Loader has been initialized using an options object that does not match the API schema.
 - options has an unknown property 'importer'. These properties are valid:
   object { implementation?, sassOptions?, prependData?, sourceMap?, webpackImporter? }
    at validate (/workspace/node_modules/sass-loader/node_modules/schema-utils/dist/validate.js:98:11)
    at Object.loader (/workspace/node_modules/sass-loader/dist/index.js:36:28)

Problem - syntax v8问题 - 语法 v8

If I change the config to sass-loader v8 syntax ( .sassOptions = { importer: globImporter() }; ), I get different error:如果我将配置更改为 sass-loader v8 语法( .sassOptions = { importer: globImporter() }; ),我会得到不同的错误:

Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
 - configuration.module.rules[2].use[3] has an unknown property 'sassOptions'. These properties are valid:
   object { ident?, loader?, options?, query? }

Question问题

How to configure it with Rails 6.1 and sass-loader v8?如何使用 Rails 6.1 和 sass-loader v8 配置它?

Original pre 6.1 working config: 6.1 之前的原始工作配置:

  1. yarn add node-sass-glob-importer , Rails 6.1 uses sass-loader@8.0.2 yarn add node-sass-glob-importer ,Rails 6.1 使用sass-loader@8.0.2
  2. Adjust webpack configuration to pass loader options:调整 webpack 配置以传递加载程序选项:
# config/webpack/environment.js

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

const globImporter = require('node-sass-glob-importer');

environment
    .loaders
    .get('sass')
    .use
    .find(item => item.loader === 'sass-loader')
    .options = { importer: globImporter() }; // syntax for sass-loader v7
    // .sassOptions = { importer: globImporter() }; // for v8

module.exports = environment
  1. Then in然后在
// app/javascript/packs/application.scss
import './application.scss';
  1. and finally in最后在
// app/javascript/packs/application.scss
@import '../stylesheets/**/*.scss';

All nested SCSS stylesheets under app/javascript/packs would then be automatically globbed. app/javascript/packs下的所有嵌套 SCSS 样式表将被自动全局化。

Found a solution.找到了解决方案。 For Rails 6.1 you need the following environment.js webpacker configuration:对于 Rails 6.1,您需要以下environment.js webpacker 配置:

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

const globImporter = require('node-sass-glob-importer');

environment
    .loaders
    .get('sass')
    .use
    .find(item => item.loader === 'sass-loader')
    .options = { sassOptions: { importer: globImporter() } }; // <-- this!

module.exports = environment

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

相关问题 使用Webpacker和Heroku在Rails资产中获取错误:管道 - Getting errors in Rails asset:pipeline with Webpacker and Heroku 使用SASS的Rails 3.1资产管道 - Rails 3.1 asset pipeline with SASS Rails 7 css 资产在生产中不起作用,需要帮助了解资产管道在没有 webpacker 的情况下如何工作 - Rails 7 css assets are not working in production, need help understanding how the asset pipeline works without webpacker Docker 中的 Webpacker 和 Rails 资产管道 - 视图未指向更新的 JS 资产 - Webpacker & Rails Asset Pipeline in Docker - View is not pointing to updated JS asset Rails 6.1 刺激未加载资产管道 - Rails 6.1 stimulus not loading with asset pipeline 如何使用 Webpacker 而不是资产管道集成 gem? [导轨 6] - How to integrate gem using Webpacker instead of the asset pipeline? [Rails 6] Webpacker / Typescript无法解析Rails资产管道文件 - Webpacker/Typescript can't resolve rails asset pipeline file 资产管道未预编译Sass - Asset pipeline not precompiling sass Rails Asset Pipeline / Compass / SASS在开发模式下编译非常慢 - Rails Asset Pipeline/Compass/SASS extremely slow to compile in development mode 如何使用Webpacker将“includePaths”添加到Sass加载器中 - How to add `includePaths` to Sass loader with Webpacker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM