Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 7 point images to cdn

Tags:

angular

How can I change all <Img src='./assets/abc.svg'> to <Img src='cdn.example.com/abc.svg'>? Basically I want to transform the image sources.

I am aware of --deployUrl and baseHref options but those things seem to not transform imgs' src attribute. Please see this as well: https://github.com/angular/angular-cli/issues/6666

like image 247
Rajat Saxena Avatar asked Oct 27 '22 08:10

Rajat Saxena


1 Answers

I don't know how to do it with command line arguments, I don't know if it exists already. But you can do it programatically using pipes.

import { environment } from './environments/environment';

@Pipe({
    name: 'env',
})
export class EnvPipe implements PipeTransform {

    transform(url: any): object {
        return environment.production ? /*do transformation here*/ : url;
    }
}

and in you template

<Img src={{'./assets/abc.svg' | env}}>

Drawback is to add this pipe in all your urls (with risk of being forgeted)

like image 69
bubbles Avatar answered Dec 21 '22 14:12

bubbles