如何使用 Yarn PnP 来包含 CSS 文件

huangapple go评论50阅读模式
英文:

How to include css files using yarn pnp

问题

我创建了一个新项目并使用 yarn 安装了 reset-csssass 包。
现在我想在我的主样式表中引入已安装包中的 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

  1. 如果尚未添加reset-css,请将其添加到您的package.json中。

    {
      "dependencies": {
        "reset-css": "^x.y.z"
      }
    }
    
  2. 运行 yarn install

  3. 使用Yarn的resolutions字段创建一个可以在Sass文件中引用的符号链接。

    {
      "dependencies": {
        "reset-css": "^x.y.z"
      },
      "resolutions": {
        "**/reset-css": "portal:./path/to/symlink/reset-css"
      }
    }
    
  4. 在您的Sass/SCSS文件中:

    @import "./path/to/symlink/reset-css/reset.css";
    

方法2:使用sassincludePaths选项

  1. 当您运行sass或使用使用sass的构建工具(如Webpack的sass-loader)时,可以指定includePaths以包含Yarn缓存目录。

    例如,在Webpack配置中:

    {
      test: /\.scss$/,
      use: [{
        loader: 'sass-loader',
        options: {
          sassOptions: {
            includePaths: [path.resolve(__dirname, '.yarn/cache')],
          },
        },
      }]
    }
    
  2. 然后,您应该能够像这样在.scss文件中导入reset-css

    @import "reset-css/reset.css";
    

方法3:在构建过程中复制

您可以在构建步骤期间将reset.css复制到您的源目录中。这个想法是使用构建工具来在Yarn缓存中找到文件并将其复制到您的项目中,以便可以轻松引用它。

  1. 确定Yarn缓存中的包位置。
  2. 使用像Webpack或一个脚本之类的构建工具将reset.css复制到您的项目目录中。
  3. 像任何其他本地样式表一样引用它。

选择最适合您项目结构和构建流程的方法。

英文:

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

  1. Add reset-css to your package.json if you haven't already.

    {
      "dependencies": {
        "reset-css": "^x.y.z"
      }
    }
    
  2. Run yarn install.

  3. 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"
      }
    }
    
  4. In your Sass/SCSS file:

    @import "./path/to/symlink/reset-css/reset.css";
    

Method 2: sass includePaths Option

  1. When you are running sass or using a build tool that uses sass (like Webpack's sass-loader), you can specify includePaths 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')],
          },
        },
      }]
    }
    
  2. 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.

  1. Identify the package location in the Yarn cache.
  2. Use build tools like Webpack or a script to copy the reset.css into your project directory.
  3. Reference it like any other local stylesheet.

Choose the method that best suits your project structure and build process.

huangapple
  • 本文由 发表于 2023年6月6日 17:31:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76413260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定