Angular CLI 6 Custom Webpack SCSS loader
Asked Answered
H

1

7

I am trying to write a custom webpack module to override or extend the current SCSS build in angular 6 with cli 6, with the intention of being able to pass in a 'brand' and match it to any overrides e.g "somemodule/'brand'/filename.override.scss" and replace the file in parent folder "somemodule/filename.scss" but I am making no progress.

Set up: @angular/cli": "6.2.2", with @angular-builders/custom-webpack": "^2.4.1"

I have updated my projects build to reflect that I am using custom webpack, and the location of of override/extention

     "build": {
      "builder": "@angular-builders/custom-webpack:browser",
      "options": {
        "customWebpackConfig": {
          "path": "./extra-webpack.config.js"
        },

My extra-webpack.config.js looks like this, just a basic hello world style I have seen over many sites

module.exports = {
    module:{
        rules: [{
            test: /\.scss$/,
            use: [{
                loader: "style-loader"
            }, {
                loader: "css-loader"
            }, {
                loader: "sass-loader",
                options: {
                    includePaths: ["src"]
                }
            }]
        }]
    }
};

however running my build with this

ng run websitename:build

throws this error

    Module build failed (from ./node_modules/sass- 

loader/lib/loader.js):

^                                                                                        
  Invalid CSS after "": expected 1 selector or at- 
 rule, was "var content = requi"    

From my understand this can be caused when trying to redefine the scss rule, however I can not see any other references to this rule to remove. I have also attempted some methods for completely override the old rule but no luck.

Solutions and guides i have been mainly focused on https://dev.to/meltedspark/customizing-angular-cli-6-buildan-alternative-to-ng-eject-1oc4 https://medium.com/a-beginners-guide-for-webpack-2/webpack-loaders-css-and-sass-2cc0079b5b3a https://www.npmjs.com/package/@angular-builders/custom-webpack#custom-webpack-config-object https://github.com/webpack-contrib/sass-loader/blob/master/test/bootstrapSass/webpack.config.js https://github.com/webpack-contrib/sass-loader/issues/536 https://github.com/meltedspark/angular-builders

Hitandrun answered 21/9, 2018 at 0:29 Comment(5)
I have a same problems, i didn't found solutionAdventist
I wasn't able to solve this in the end either sorry, I ended up creating separate projects for each brand within the same project.Hitandrun
For some rason in a doc npmjs.com/package/@angular-builders/… i can read mergeStrategies: webpack config merge strategies, can be append | prepend | replace per webpack config entry. Defaults to append. Maybe we can try get original config webpack and modify to load hot cssAdventist
What is your intention behind creating a SCSS loader? You only want to replace a SCSS file with another SCSS file that holds specific CSS for this brand? If so, I might know a solution.Laundes
Hey - did anybody have any luck with this?Lipstick
C
3

I had the same problem, I have tried mergeStrategies but didn't work.I have changed my custom webpack into a function, that solved the issue, I was able extend sass options.

module.exports = function(config) {
    const rules = config.module.rules || [];
    rules.forEach(rule => {
        if (String(rule.test) === String(/\.scss$|\.sass$/)) {
        const loaders = rule.use;
        const transformedLoaders = loaders.map(l => {
            if (l.loader === 'sass-loader') {
                // here you can override options or replace or add.
            } 
            return l;
        });
        rule.use = transformedLoaders;
        }
    });
    return config;
};

Chronological answered 28/5, 2019 at 20:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.