英文:
How to include css files using yarn pnp
问题
我创建了一个新项目并使用 yarn 安装了 reset-css
和 sass
包。
现在我想在我的主样式表中引入已安装包中的 reset.css
样式表并使用 sass
进行构建。
我应该如何定位 reset-css
文件以获取其路径,并如何在我的 scss 文件中使用 @use
/@import
来引入它?
通常我可以从 node_modules 中引用它,但在使用 yarn pnp 时该文件夹不再存在。
英文:
Ive created a new project and used yarn to install reset-css
and sass
package.
In my main stylesheet I now want to include the reset.css
stylesheet from the installed package and build it with sass
.
How can I locate where reset-css
file is to get its path and how can I @use
/@import
it in my scss file?
Normally I could just reference it from node_modules but the folder does not exist anymore when using yarn pnp.
答案1
得分: 1
当使用Yarn Plug'n'Play(PnP)时,不会创建传统的基于磁盘的node_modules
目录。相反,Yarn通过内存或zip文件管理依赖解析,绕过了这些目录的需要。但是,这种新的架构提出了一个问题:在没有基于磁盘文件路径的情况下,如何引用已安装的包?
在Yarn PnP中,通常可以像通常一样在JavaScript文件中导入依赖项,但对于像CSS这样的情况,可能有点棘手。
以下是在使用Yarn PnP时在Sass/SCSS文件中导入reset-css
的一些建议:
方法1:在package.json
中使用resolutions
-
如果尚未添加
reset-css
,请将其添加到您的package.json
中。{ "dependencies": { "reset-css": "^x.y.z" } }
-
运行
yarn install
。 -
使用Yarn的
resolutions
字段创建一个可以在Sass文件中引用的符号链接。{ "dependencies": { "reset-css": "^x.y.z" }, "resolutions": { "**/reset-css": "portal:./path/to/symlink/reset-css" } }
-
在您的Sass/SCSS文件中:
@import "./path/to/symlink/reset-css/reset.css";
方法2:使用sass
的includePaths
选项
-
当您运行
sass
或使用使用sass
的构建工具(如Webpack的sass-loader
)时,可以指定includePaths
以包含Yarn缓存目录。例如,在Webpack配置中:
{ test: /\.scss$/, use: [{ loader: 'sass-loader', options: { sassOptions: { includePaths: [path.resolve(__dirname, '.yarn/cache')], }, }, }] }
-
然后,您应该能够像这样在
.scss
文件中导入reset-css
:@import "reset-css/reset.css";
方法3:在构建过程中复制
您可以在构建步骤期间将reset.css
复制到您的源目录中。这个想法是使用构建工具来在Yarn缓存中找到文件并将其复制到您的项目中,以便可以轻松引用它。
- 确定Yarn缓存中的包位置。
- 使用像Webpack或一个脚本之类的构建工具将
reset.css
复制到您的项目目录中。 - 像任何其他本地样式表一样引用它。
选择最适合您项目结构和构建流程的方法。
英文:
When using Yarn Plug'n'Play (PnP), traditional disk-based node_modules
directories are not created. Instead, Yarn manages the dependency resolution in-memory or via zip files, bypassing the need for these directories. But this new architecture poses a question: how do you refer to installed packages without a disk-based file path?
In Yarn PnP, you can typically import dependencies in your JavaScript files as you would normally, but for cases like CSS, it can be a bit tricky.
Here are some suggestions for importing reset-css
in a Sass/SCSS file when using Yarn PnP:
Method 1: resolutions
in package.json
-
Add
reset-css
to yourpackage.json
if you haven't already.{ "dependencies": { "reset-css": "^x.y.z" } }
-
Run
yarn install
. -
Use Yarn's resolutions field to create a symlink that you can reference in your Sass file.
{ "dependencies": { "reset-css": "^x.y.z" }, "resolutions": { "**/reset-css": "portal:./path/to/symlink/reset-css" } }
-
In your Sass/SCSS file:
@import "./path/to/symlink/reset-css/reset.css";
Method 2: sass
includePaths
Option
-
When you are running
sass
or using a build tool that usessass
(like Webpack'ssass-loader
), you can specifyincludePaths
to include the Yarn cache directory.For example, in Webpack config:
{ test: /\.scss$/, use: [{ loader: 'sass-loader', options: { sassOptions: { includePaths: [path.resolve(__dirname, '.yarn/cache')], }, }, }] }
-
Then, you should be able to import
reset-css
in your.scss
file like this:@import "reset-css/reset.css";
Method 3: Copy During Build
You could copy the reset.css
into your source directory during a build step. The idea is to use build tooling to locate the file within the Yarn cache and copy it into your project so that it can be easily referenced.
- Identify the package location in the Yarn cache.
- Use build tools like Webpack or a script to copy the
reset.css
into your project directory. - Reference it like any other local stylesheet.
Choose the method that best suits your project structure and build process.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论