angular-cli:配置 sass-loader 以在 includePath 中包含 node_modules

标签 angular angular-cli

Angular -cli: 1.0.0-beta.15

节点:6.5.0

操作系统:Linux x64

我想从 bootstrap-material-design v4-dev 分支编译 scss 文件,方法是将它放在 angular-cli.json 的 apps[0].styles[] 数组中 但出现以下错误:

ERROR in ./~/css-loader!./~/postcss-loader!./~/sass-loader!./~/bootstrap-material-design/scss/bootstrap-material-design.scss                                               
Module build failed:                                                                                                                                                       
@import "bootstrap/scss/variables"; // from bootstrap node_module                                                                                                          
^                                                                                                                                                                          
      File to import not found or unreadable: bootstrap/scss/variables                                                                                                     
Parent style sheet: /home/ciesielskico/Documents/Git/reports/node_modules/bootstrap-material-design/scss/_variables.scss                
      in /home/ciesielskico/Documents/Git/reports/node_modules/bootstrap-material-design/scss/_variables.scss (line 45, column 1)       
 @ ./~/bootstrap-material-design/scss/bootstrap-material-design.scss 4:14-148                                                                                              
 @ multi styles

Angular -cli.json

"styles": [
                "../node_modules/bootstrap/scss/bootstrap-flex.scss",
                "../node_modules/bootstrap-material-design/scss/bootstrap-material-design.scss",
]

在 bootstrap-material-design 的文档中,它说将 node_modules 放在 sass-loader 的 includePath 中。

当前版本的 angular-cli 是否可行? 如果是,如何?

最佳答案

解决方法

我已经编写了一个作为 npm 安装后执行的节点脚本。位于 node_modules/angular-cli/models/ 的文件 webpack-build-production.jswebpack-build-development.js 应该包含 < strong>sassLoader 实体和以下脚本将自动附加它。

var fs = require('fs');

const FILES = [
  "node_modules/angular-cli/models/webpack-build-production.js",
  "node_modules/angular-cli/models/webpack-build-development.js"
];

const ADDITIONAL_LINES = "\
        ,sassLoader: {\n\
            includePaths: [path.resolve(\"node_modules/bootstrap-sass/assets/stylesheets\")]\n\
        }\n\
    };\n";


FILES.forEach(function (file) {
  var text = fs.readFileSync(file, "utf8");
  var alreadyEdited = false;
  var content = "";

  text.split(/\r?\n/).forEach(function (line) {
    if (line.indexOf('sassLoader') !== -1) {
      alreadyEdited = true;
    }

    if (line.indexOf(' };') !== -1) {
      content += ADDITIONAL_LINES;
    } else {
      content += line + "\n";
    }
  });

  if (!alreadyEdited) {
    console.log('File is being adjusted by fix script. ', file);
    fs.writeFileSync(file, content, 'utf8');
  }
});

关于angular-cli:配置 sass-loader 以在 includePath 中包含 node_modules,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39635971/

相关文章:

css - 如何覆盖 Ng-Bootstrap 样式中的样式并将 html 结构编辑为 Typeahead

javascript - 使用 Prime NG 和 Angular CLI 时样式 Assets 的正确顺序是什么

Angular 2 在带有 Angular 路由器的 NPM 模块中延迟加载 NgModule

angular - 我的 Angular CLI 版本比要求的要高,但无论如何我都会收到错误消息

angular - 空合并运算符 Angular 2

javascript - Angular 2 ng构建

angular - 将环境变量传递给 Angular 库

node.js - ng 不被识别为内部或外部命令。 Jenkins + 角度 CLI

尽管 `runGuardsAndResolvers` 设置为 'always',但 Angular 解析器未更新或重新获取数据?

angular - 管道运算符时如何返回 `forkJoin` 可观察对象