1

I need to load an external css file in environment files. I have two environments: dev and prod. It works well in mode dev because the css file is in local, but not in mode prod.

environment.dev.ts

require("style-loader!./../assets/style.css");

environment.prod.ts

require("style-loader!http://abc/style.css");

Then i had this error:

Can't resolve 'http://abc/sprite.css'

How could i make it work with url ?

Thanks !

UPDATE

As the suggestion of Milad, i try to put a link with the dynamic href:

<link [attr.href]="getCss()" type="text/css">

function getCss(): string {
    return this.mode === 'dev' ?
        'assets/style.css':
        'http://abc/sprite.css';
}

And i have the following error:

unsafe value used in a resource URL context

8
  • What are you trying to do here ? You usualy don't have to load from an url as webpack is a bundler, it will include the path in the bundle and hte stylesheets gets added to the html it as a relative url... Commented Aug 22, 2017 at 9:23
  • 1
    Also, abc/style.css returns 404.. Commented Aug 22, 2017 at 9:25
  • @GiliYaniv yes, it's not the real link, i just want to show it is an external link Commented Aug 22, 2017 at 9:30
  • @n00dl3 the problem is in dev mode, i want to use, let's say path-1; in prod mode i want another path. How could i do that ? Commented Aug 22, 2017 at 9:32
  • 1
    OK, the real question is "does this css file exists at build time at this url ?" If it does exist, you can just use the resolve-url-loader (it sounds like a weird idea, but maybe that's legit). If it doesn't, what are you trying to do ? Commented Aug 22, 2017 at 9:38

1 Answer 1

1

You can't have http calls in your webpack's loader configuration because technically that loader does not exist in your production environment.

so

  require("style-loader!http://abc/style.css");

What you're saying is :

Hey webpack, when you're building my app, call this url and get the css and include it in the bundle.

This shouldn't work because Webpack shouldn't make an external call to get your bundle pieces.

Try downloading the css and putting it in your code .

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. I tried your solution and updated my question with a new error: "unsafe value used in a resource URL context"
I fixed this bug by using DomSanitizer. Thank you anyway !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.