angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Angular CLI is modifying the paths to fonts set in @font-face to point to the root directory in a production build

Open gitdisrupt opened this issue 9 years ago • 53 comments

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.

gitdisrupt avatar Jun 07 '17 07:06 gitdisrupt

@ogousa can you please provide a solution?

ErVishalM avatar Oct 04 '19 04:10 ErVishalM

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 :)

ogousa avatar Oct 04 '19 13:10 ogousa

Thank you @ogousa. Is there any reported issue so that I can follow that issue directly.

ErVishalM avatar Oct 04 '19 18:10 ErVishalM

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')

Sathyaraj06 avatar Oct 09 '19 14:10 Sathyaraj06

on angular 8.0.2 adding

"production": {
              "deployUrl": "./path/to/prod/files/",

in angular.json solved the problem for me

UsarodnaKuze avatar Oct 11 '19 05:10 UsarodnaKuze

@UsarodnaKuze could you clarify this? thank you so much!

malua avatar Nov 07 '19 16:11 malua

@Sathyaraj06: If I take the dot away, it takes the wrong path.

src: url('/assets/fonts/Regular/OpenSans-Regular.woff');

then in production:

image

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.

Willyisback avatar Nov 15 '19 13:11 Willyisback

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!

ogousa avatar Nov 15 '19 19:11 ogousa

@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 :)

alxshll avatar Dec 05 '19 17:12 alxshll

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

niccolofanton avatar Dec 06 '19 09:12 niccolofanton

@alxshell The tilde will be replaced by the base-href during building. So it works for both development and production. :)

Willyisback avatar Dec 12 '19 12:12 Willyisback

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

denkerny avatar Jan 09 '20 11:01 denkerny

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.