简体   繁体   English

Webpack 4 sass-loader 导入文件,路径中没有 ~ 波浪号

[英]Webpack 4 sass-loader import file without ~ tilde in path

Is there a way to use third-party SASS with imports like @import module-name/path and still have sass-loader resolve the files to node_modules ?有没有办法将第三方 SASS 与@import module-name/path类的导入一起使用,并且仍然让 sass-loader 将文件解析为node_modules

If I put the ~ in like @import ~module-name/path this works and sass-loader looks into the node_modules and finds the files, but I am unable to modify the SASS files to add the ~ .如果我把~@import ~module-name/path这样工作,sass-loader 会查看node_modules并找到文件,但我无法修改 SASS 文件以添加~

I have tried a few things such as我尝试了一些事情,例如

 rules: [
  {
    test: /\.(sa|sc|c)ss$/,
    use: [
      MiniCssExtractPlugin.loader,
      "css-loader",
      {
        loader: "sass-loader",
        options: {
          includePaths: ["node_modules"]
        }
      }
    ]
  }
]

but the modules do not resolve.但模块没有解析。

Any ideas?有任何想法吗? I see lots of other folks with the same issue but only see adding ~ as a real fix (which I can't do).我看到很多其他人有同样的问题,但只看到添加~作为一个真正的修复(我做不到)。

After much trial and effort, I found this is not possible AFAIK, but you can write something to enumerate your includePaths and add that array to your config.经过多次尝试和努力,我发现这在 AFAIK 中是不可能的,但是您可以编写一些内容来枚举您的includePaths并将该数组添加到您的配置中。

The last part of each path should be a directory where an @import would be found.每个路径的最后一部分应该是一个可以找到@import的目录。

For @import scoped-module-name/path对于@import scoped-module-name/path

Have your includePaths with this:有你的包含includePaths

rules: [
  {
    test: /\.(sa|sc|c)ss$/,
    use: [
      MiniCssExtractPlugin.loader,
      "css-loader",
      {
        loader: "sass-loader",
        options: {
          includePaths: ["node_modules/@your_org"] // scoped-module-name exists here
        }
      }
    ]
  }
]

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

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