Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Requiring SASS in Angular 2 components with webpack #219

Closed
mark-norgate opened this issue Jan 12, 2016 · 12 comments
Closed

Requiring SASS in Angular 2 components with webpack #219

mark-norgate opened this issue Jan 12, 2016 · 12 comments

Comments

@mark-norgate
Copy link

I am trying to require my SASS in my components, like this:

@Component({
    selector: 'signup',
    directives: [RouterOutlet, RouterLink],
    styles: [require('../sass/signup.scss')],
    template: require('./signup.component.html')
})

but my browser is complaining: EXCEPTION: TypeError: s.replace is not a function

I previously had the require outside of the component definition and it worked fine; but now I have another component I have to put the styles inside the components.

I have the correct loaders going on for SASS:

{
    test: /\.scss$/,
    loaders: ['style', 'css?sourceMap', 'sass?sourceMap']
}

What am I doing wrong?

@davidarnarsson
Copy link

Have you tried calling .toString()? As in, styles: [ require('../sass/signup.scss').toString() ] ?

I did something similar for stylus, using the css-loader.

@abdulhaq-e
Copy link
Contributor

Change the loaders to:

{
    test: /\.scss$/,
    loaders: ['raw-loader', 'sass-loader?sourceMap']
}

@mark-norgate
Copy link
Author

Thank you @davidarnarsson , that works. And @abdulhaq-e I tried implementing your suggested change but it ruined my layout unfortunately. The problem now remains that both styles are being loaded on both pages.

The main problem is the background colour, which is different for both components. I have this:

html, body {
    height:100%;
    background-color: #eaeaea;
    display: flex;
    align-items: center;
    justify-content: center;
    display: -webkit-flex;
    -webkit-align-items: center;
    -webkit-justify-content: center;
}

but the background-color for each component is the same, even though I am specifying a different colour in each scss file. This is more of an issue with Angular 2 so I will go elsewhere for the answer.

Unless you know.

Thanks for your help. Mark

@manavsehgal
Copy link

Opened #199 consolidating UI/style related issues.

@PatrickJS
Copy link
Owner

closing for #199

@ilio
Copy link

ilio commented Apr 27, 2016

try

require('../sass/signup.scss');

@Component({
       selector: 'signup',
       directives: [RouterOutlet, RouterLink],
       template: require('./signup.component.html')
})

@bitterloa
Copy link

YASS! @davidarnarsson, using require('./file.scss').toString() helped. Thanks

@smithad15
Copy link

If you don't want to have to write .toString() everywhere, you could use this loader instead: https://www.npmjs.com/package/css-to-string-loader

@lpalli
Copy link

lpalli commented Jun 7, 2016

Anyone was able to run the test after adding scss files?

@alvipeo
Copy link

alvipeo commented Jun 7, 2016

I gave up. I use Bootstrap 3 now, so everything works.

@lpalli
Copy link

lpalli commented Jun 8, 2016

I founded the solution to run the test (karma) after adding scss files, I added the loader also in the config/webpack.test.js file, not only in the config/webpack.common.js file.

@alvipeo
Copy link

alvipeo commented Jun 8, 2016

Can we have a separate branch for Bootstrap 4? I think it might be useful...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants