reactjs - 在 React 项目中用与平台无关的 SASS 编译器替换 node-sass

标签 reactjs windows-subsystem-for-linux node-sass

我们是一个由大约 6 名开发人员组成的团队,具有异构设置(Windows 10、Ubuntu、macOS),并已开始使用 React 进行开发。

Linux/macOS 和 Windows 之间的脚本编写非常痛苦,因此我们决定使用 Bash for Windows 来运行我们的构建。一旦您正确设置了 Bash,这就会非常有效,并且与 VS Code 的集成也很棒。 但是有一个很大的问题:每当你想在 Bash 之外更新你的项目(使用 CMD 或 PowerShell)时,你实际上是在切换你的操作系统,然后你会从节点收到一个很好的错误-sass 说 your environment has changed

由于重建node-sass根本不起作用,我们发现解决这个问题的唯一方法是删除或重命名node_modules并再次运行npm install,但这非常消耗时间/资源。

我们已经看到它看起来像 there is a native js alternative但我们想看看是否有人经历过同样的事情并找到了合适的解决方案。

我们确实没有大量的 SASS 文件,因此我们不太关心性能(这是使用 libSass 在 C++ 中实现这一点的要点)。

注意:我们不能总是在 Windows 上使用 Bash,因为我们在一些运行 npm 构建的 Java/React 应用程序中进行了一些困惑的 Maven 集成,而这将始终在 Windows 中。

最佳答案

我们在应用程序中使用 Angular 6 前端并遇到了同样的问题。我们通过将所需的二进制文件 checkin 存储库并使用不同的 Maven 配置文件(每个环境/操作系统一个,对于我们的 CI 来说默认是 linux)来将 sass-binary-path 设置为其环境特定的二进制文件来解决这个问题。但是,只有当您拥有能够设置环境变量的构建系统时,它才有效。另一个警告是您必须手动更新二进制文件。

<properties>
    <sass.binary.path.base>${basedir}/node-sass-binaries/</sass.binary.path.base>
</properties>
...
<profile>
  <id>linux</id>
  <activation>
    <activeByDefault>true</activeByDefault>
  </activation>
  <properties>
    <sass.binary.path>${sass.binary.path.base}/linux-x64-64_binding.node</sass.binary.path>
  </properties>
</profile>
<profile>
  <id>windows</id>
  <properties>
    <sass.binary.path>${sass.binary.path.base}/win32-x64-64_binding.node</sass.binary.path>
  </properties>
</profile>
<profile>
  <id>mac</id>
  <properties>
    <sass.binary.path>${sass.binary.path.base}/darwin-x64-64_binding.node</sass.binary.path>
  </properties>
</profile>

关于reactjs - 在 React 项目中用与平台无关的 SASS 编译器替换 node-sass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45051141/

相关文章:

ios - React Native libc++abi.dylib : terminating with uncaught exception of type NSException

ubuntu - 无法在 Jetbrains GoLand 中打开终端

node.js - yarn 安装抛出错误 : gyp verb `which` failed Error: not found: python2

javascript - 使用 React 和 Apollo 进行 GraphQL 身份验证

javascript - 如何在 Redux 中更新状态后触发关闭回调?

javascript - 嵌套异步 - 最佳实践

linux - .zshrc 中最有效的 if 语句来检查 Linux 操作系统是否在 WSL 上运行?

ruby-on-rails - 为在 WSL 环境中运行的 Rails 项目配置 capybara 时,如何指定 chrome.exe 的路径?

node.js - npm install : Failed at the node-sass@3. 13.1 安装后脚本

css - Sass会自动生成一个css-style.css文件放在Sass文件夹下吗?