简体   繁体   English

如何在我的库中声明/包含一个导入的 scss 文件夹,以便可以在我的应用程序的 scss 文件中调用 @import 它?

[英]How do I declare/include an imported scss folder in my library so I can call @import it in my application's scss files?

The Goal:目标:

I am trying to include a path from my library so I can use an @import statement to include the scss files in my application like so:我正在尝试从我的库中包含一个路径,以便我可以使用@import语句在我的应用程序中包含 scss 文件,如下所示:

@import "some-scss-in-my-lib"

The Problem:问题:

Unfortunately everytime I try to serve my application I am getting the following compiler error:不幸的是,每次我尝试为我的应用程序提供服务时,我都会收到以下编译器错误:

SassError: Can't find stylesheet to import.

What I've already tried:我已经尝试过的:

  • Looking up the search on Stackoverflow brought me this thread which describes the same problem as I'm facing now.在 Stackoverflow 上查找搜索给我带来了这个线程,它描述了与我现在面临的相同问题。 And I've tried to include the path with the styleIncludePaths property just like Zainu suggested but when trying to serve the application I've ran in to the exact Problem as described in The Problem -Section above.并且我尝试像Zainu建议的那样将路径包含在styleIncludePaths属性中,但是在尝试为应用程序提供服务时,我遇到了上面“问题 -部分”中描述的确切问题。

  • I tried to set the stylePreprocessorOptions property like GreatHawkeye in the first post and it was possible to @import the scss file after using ng build , but I can't understand how and why this is working for stylePreprocessorOptions but not styleIncludePaths ?我尝试在第一篇文章中设置stylePreprocessorOptions属性,如GreatHawkeye ,并且可以在使用ng build@import scss 文件,但我不明白这对stylePreprocessorOptions而不是styleIncludePaths的工作方式和原因?

  • I also tried searching on google which lead me to the Thread Unable to get styles bundled using styleIncludePaths option where I found an answer by Alan Aguis , but unfortunately there was no real explanation what styleIncludePaths really is for.我还尝试在 google 上搜索,这导致我使用 styleIncludePaths 选项找到了 Thread Unable to get styles 捆绑,我在其中找到了Alan Aguis的答案,但不幸的是没有真正的解释 styleIncludePaths 的真正用途。 The only thing that Alan Aguis said was: Alan Aguis 唯一说的就是:

that is because styleIncludePaths is indented for something else.那是因为 styleIncludePaths 缩进了别的东西。

But as said, he didn't say for what exactly styleIncludePaths is, which confused me even more.但正如所说,他没有说明 styleIncludePaths 到底是什么,这让我更加困惑。

I also tried to lookup the ng-packagr github page regarding the documentation about Add Style Include Paths on the Thread How to use "styleIncludePaths" but the result was still the same and I couldn't find a clear answer to my problem.我还尝试查找有关在线程上添加样式包含路径如何使用“styleIncludePaths”的文档的ng-packagr github 页面,但结果仍然相同,我找不到我的问题的明确答案。

Questions about this Issue:关于这个问题的问题:

  1. When is styleIncludePaths in ng-package.json used? ng-package.json 中的 styleIncludePaths 什么时候使用? Is it used for published packages only?它仅用于已发布的软件包吗?

  2. When is stylePreprocessorOptions in angular.json used? angular.json 中的 stylePreprocessorOptions 何时使用? Is it used for the dist version only after running ng build?只有在运行 ng build 后才用于dist版本吗?

  3. What in general is the difference between styleIncludePaths from ng-package.json compared to stylePreprocessorOptions from angular.json?一般来说,ng-package.json 的 styleIncludePaths 与 angular.json 的 stylePreprocessorOptions 有什么区别?

  4. How do I declare/include an imported scss folder in my library so I can call @import it in my application's scss files?如何在我的库中声明/包含一个导入的 scss 文件夹,以便可以在我的应用程序的 scss 文件中调用 @import 它?

I'm pretty much sure that I'm wrong in my suggestions but I'd like to know what is going on and how to resolve my issue.我很确定我的建议是错误的,但我想知道发生了什么以及如何解决我的问题。

Any help on this is as always highly appreciated!对此的任何帮助一如既往地受到高度赞赏!

As answered on github , I quote alan-agius4 :正如在github上回答的那样, 我引用 alan-agius4

The includes paths are used by CSS processors during the build of a library to resolve imports when processing scss/less/stylus files. CSS 处理器在构建库期间使用包含路径来解析处理 scss/less/stylus 文件时的导入。 In your ship unprocessed scss files, these paths will need to be included at application level instead of the library level, because such files will not be handled by the library build pipeline, but the application's.在您的未处理 scss 文件中,这些路径需要包含在应用程序级别而不是库级别,因为此类文件不会由库构建管道处理,而是由应用程序处理。 They are just copied as static files.它们只是被复制为 static 文件。 In case you don't want to include such paths at application level, you can leverage webpack's tilde import syntax ex:如果您不想在应用程序级别包含此类路径,您可以利用 webpack 的波浪号导入语法,例如:

@import "~/my-lib/scss/mixins”.

Note: webpack specific features are not official supported by the Angular tooling team and might break without warning.注意:webpack 特定功能不受 Angular 工具团队的官方支持,可能会在没有警告的情况下中断。

I previously attempted what sounds like a pretty similar setup to yours that lead to a lot of research and trial and error.我之前尝试过听起来与您的设置非常相似的设置,这导致了大量的研究和反复试验。 From everything I've tried, it essentially boiled done to:从我尝试过的所有事情来看,它基本上都是煮熟的:

styleIncludePaths is used when building a library and the stylePreprocessorOptions path is used when building/serving an angular application. styleIncludePaths 用于构建库,stylePreprocessorOptions 路径用于构建/服务 angular 应用程序。

It would depend on the setup of your project, but if your library is configured outside of your app, and you then imported into your app without being built separately, the stylePreprocessorOptions would be used as the style import path for that library.这将取决于您的项目的设置,但如果您的库是在您的应用程序之外配置的,然后您在没有单独构建的情况下导入到您的应用程序中,那么 stylePreprocessorOptions 将用作该库的样式导入路径。 Even though if it is configured outside of your application and has a defined ng-package file, if you are just importing it locally, angular will treat it the same as any other component/module already inside of the app and will need to use the stylePreprocessorOptions to resolve any scss import paths.即使它是在您的应用程序外部配置并具有已定义的 ng-package 文件,如果您只是在本地导入它,angular 会将其视为与应用程序内部已经存在的任何其他组件/模块相同,并且需要使用stylePreprocessorOptions 来解析任何 scss 导入路径。

On the other hand, if you were to build the library separately, and then pull in that built library, then the styleIncludePaths would be used to resolve any scss import paths when that library is built separately.另一方面,如果您要单独构建库,然后拉入该构建的库,则 styleIncludePaths 将用于在单独构建该库时解析任何 scss 导入路径。

I wouldn't necessarily say the is a "difference" between the two.我不一定会说这是两者之间的“差异”。 They both serve the same purpose to provide a base path to resolve scss imports, but styleIncludePaths is the syntax in a library's ng-package file when that library is built and stylePreprocessorOptions is the syntax for an angular application for when it is built.它们都用于提供解析 scss 导入的基本路径的相同目的,但 styleIncludePaths 是构建库时库的 ng-package 文件中的语法,而 stylePreprocessorOptions 是构建 angular 应用程序时的语法。

As for your last question on how to "declare/include an imported scss folder in my library so I can call @import it in my application's scss files", I was not able to work out a more elegant solution, so they only way I could get that type of setup to work was to have the style sheets I wanted outside of the app/library I was working on and just configure the styleIncludePaths and stylePreprocessorOptions to reference the same files.至于您关于如何“在我的库中声明/包含导入的 scss 文件夹以便我可以在我的应用程序的 scss 文件中调用 @import 它”的最后一个问题,我无法制定出更优雅的解决方案,所以他们只能这样可以让这种类型的设置工作是在我正在处理的应用程序/库之外拥有我想要的样式表,只需配置 styleIncludePaths 和 stylePreprocessorOptions 以引用相同的文件。 That way I had one source for my style sheets which I could reference in both the styleIncludePaths and stylePreprocessorOptions.这样,我的样式表就有了一个来源,我可以在 styleIncludePaths 和 stylePreprocessorOptions 中引用它。

在此处输入图像描述

This was definitely a pain of a process for me to wrap my head around, so I hope this helps.这对我来说绝对是一个痛苦的过程,所以我希望这会有所帮助。

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

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