Angular CLI is modifying the paths to fonts set in @font-face to point to the root directory in a production build
Bug Report or Feature Request (mark with an x)
- [ x] bug report -> found similar issue which i believe to have been closed in error: #3143
Versions.
Output from: `ng --version`.
@angular/cli: 1.1.0
node: 8.0.0
os: darwin x64
@angular/animations: 4.1.3
@angular/common: 4.1.3
@angular/compiler: 4.1.3
@angular/core: 4.1.3
@angular/forms: 4.1.3
@angular/http: 4.1.3
@angular/platform-browser: 4.1.3
@angular/platform-browser-dynamic: 4.1.3
@angular/router: 4.1.3
@angular/cli: 1.1.0
@angular/compiler-cli: 4.1.3
@angular/language-service: 4.1.3
Repro steps.
ng new testapp --style scss
Then created new directory in ./src/assets/fonts/ which includes a set of custom font files like Gibson-Light-webfont.eot for example.
Then in my styles.scss file i reference those fonts like this:
@font-face {
font-family: 'gibson_package';
src: url('assets/fonts/Gibson-Light-webfont.eot');
...
Then i run ng build --prod
In my ./dist directory, the build process correctly copies my fonts to ./dist/assets/fonts/* as expected, but it also included duplicates of all of my fonts right in the root of my ./dist directory which i did not expect and the paths set in @font-face url's are changed to point to the root.
The log given by the failure.
No errors are output.
Desired functionality.
That the production build only copy my fonts to the ./dist/assets/fonts directory and not modify the paths in @font-face urls.
@ogousa can you please provide a solution?
With Angular 8 we have the same problem again :( To fix it you can add Node.js script which modifies assets url AFTER compilation.
For production build you can use this command:
npm run prod-build
The "prod-build" is defined in package.json:
"scripts": {
"prod-build": "ng build --prod --base-href /aaa/ && node ./scripts/web.js",
...
}
where aaa is the base url.
You have to add new script "web.js" to scripts folder (scripts folder is on the same level as src). Also you have to install "replace-in-file" package:
npm i replace-in-file
This is the content of web.js:
var replace = require('replace-in-file');
let fromList = [];
let toList = [];
// fix assets path for web production build
fromList.push(new RegExp('/assets/fonts', 'g'));
toList.push('./assets/fonts');
try {
let options = {
files: 'dist/aaa/styles.*.css',
from: fromList,
to: toList,
allowEmptyPaths: false
};
replace.sync(options);
}
catch (error) {
console.error('Error occurred:', error);
}
This code simply replaces all occurrence of absolute path /assets/fonts with relative path ./assets/fonts in resulting style file inside dist folder.
It should work until Angular team fix this issue :)
Thank you @ogousa. Is there any reported issue so that I can follow that issue directly.
don't use dots for relative path here, just use as url('/assets/abc.woff2'). It doesn't point to that file when pressing "ctrl + click", but it removes the duplicate files created in the root dist folder.
eg : ('../fonts/abc') to ('/fonts/abc') : ('./assets/abc.woff2') to ('/assets/abc.woff2')
on angular 8.0.2 adding
"production": {
"deployUrl": "./path/to/prod/files/",
in angular.json solved the problem for me
@UsarodnaKuze could you clarify this? thank you so much!
@Sathyaraj06: If I take the dot away, it takes the wrong path.
src: url('/assets/fonts/Regular/OpenSans-Regular.woff');
then in production:

For info, base-href is \app\ and I bind the font over angular.json:
{
"glob": "**/*",
"input": "./node_modules/npm-font-open-sans/fonts/",
"output": "./assets/fonts/"
}
Workaround for me is:
src: url('~/assets/fonts/Regular/OpenSans-Regular.woff');
Then it works.
Yes, it really works!
I have Angular 8.12 and project name aaa:
ng build --prod --base-href /aaa/
for fonts:
@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 400;
src: url('~/assets/fonts/roboto-v18-latin-ext_latin-regular.woff2') format('woff2');
}
for images:
background: url('~/assets/images/br-header.svg');
Everything works fine for external server and for localhost.
Thank you, Willyisback!
@Willyisback thank you very much. It works like a charm.
Would you mind to explain why the usage of the tilde is working here... I am really excited to understand it :)
Yes, it really works! I have Angular 8.12 and project name aaa:
ng build --prod --base-href /aaa/for fonts:
@font-face { font-family: 'Roboto'; font-style: normal; font-weight: 400; src: url('~/assets/fonts/roboto-v18-latin-ext_latin-regular.woff2') format('woff2'); }for images:
background: url('~/assets/images/br-header.svg');Everything works fine for external server and for localhost.
Thank you, Willyisback!
This works! thanks man
@alxshell The tilde will be replaced by the base-href during building. So it works for both development and production. :)
why does this is still closed? Dist folder has duplicate of assets and it has bigger size. Is there a way to prevent copy to root folder files are already exist here into specific folders (like fonts, img, etc.)
here is explanation:
-dist
--fonts
--- *.ttf
--img
--- cat.png
cat.png
*.ttf
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.