Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular-cli: Configure sass-loader to include node_modules in includePath

angular-cli: 1.0.0-beta.15

node: 6.5.0

os: linux x64

I want to compile the scss file from bootstrap-material-design v4-dev branch by putting it in the apps[0].styles[] array of angular-cli.json but I get the following error:

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",
]

On bootstrap-material-design's documentation it says to put node_modules in the includePath of the sass-loader.

Is it possible with the current version of angular-cli? If yes, how?

like image 609
Alexander Ciesielski Avatar asked Feb 18 '26 23:02

Alexander Ciesielski


1 Answers

workaround

I've written a node script that is executed as npm post install. Files webpack-build-production.js and webpack-build-development.js located at node_modules/angular-cli/models/ should contain sassLoader entity and following script will append it automaticly.

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');
  }
});
like image 167
Vojtech Avatar answered Feb 20 '26 17:02

Vojtech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!