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

Can't figure out how to replace a default Angular plugin / inline fonts

Open metallidevils opened this issue 6 years ago • 8 comments

Is your feature request related to a problem? Please describe. I am struggling with figuring out how to inline font files that are referenced in css. I understand that by default, Angular CLI ships with a file-loader that tests for .woff/woff2/etc files.

How can I remove/replace this file-loader, and instead use a base64-inline loader to inline the font files? I assume that removing default loaders should be possible but I cannot find a straightforward way to do this. Thanks!

metallidevils avatar Sep 20 '19 01:09 metallidevils

Same problem, can't use custom loader to inline svg. I have .less file, and property for background that refers to node_modules like background: url(~symlink_to_node_modules/images/menu.svg) no-repeat center; I am tried to cutoff svg rule from base config that shipped from cli: { test: /\.(eot|svg|cur|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)$/, loader: 'file-loader', options: { name: '[name].[ext]' } } , and this don't work. My test rule doesn't match svg, but svg image somehow bundled. Is there any workaround?

novathore avatar Oct 21 '19 19:10 novathore

@metallidevils Hey.

How can I remove/replace this file-loader, and instead use a base64-inline loader to inline the font files?

// extra-webpack.config.js
module.exports = config => {
  config.module.rules = config.module.rules
    .filter(rule => rule.loader !== 'file-loader')
    .concat({
      test: /\.(eot|svg|cur|jpg|png|webp|gif|otf|ttf|woff|woff2|ani)$/,
      loader: 'base64-inline-loader',
      options: {
        name: '[name].[ext]'
      }
    });

  return config;
};

Checked it with a simple example:

import { Component } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';

import base64 from './image.jpg';

@Component({
  selector: 'app-root',
  template: `
    <img [src]="base64" />
  `
})
export class AppComponent {
  base64 = this.sanitizer.bypassSecurityTrustUrl(base64);

  constructor(private sanitizer: DomSanitizer) {}
}

arturovt avatar Nov 17 '19 19:11 arturovt

@arturovt thanks! This is going to load all these file types with base64-inline-loader. Although you did show the way for removing the default loader I think the author wanted to load just the font files (or in case of @novathore svgs) with custom loader.
Seems like in these cases the default loader should be replaced with two loaders (one is custom-loader for the specific extension and file-loader for the rest). Right?

just-jeb avatar Nov 17 '19 20:11 just-jeb

I think he will need to check the RegEx used by Angular and modify with its own (that doesn't contain font extensions) + also add the base64-inline-loader. Something like:

// pseudocode tho
rules.find(findFileLoader).test = /\.(svg|jpg|....without woff etc.)/
rules.push({ test: /fonts extensions regex/, loader: base64-inline-loader });

arturovt avatar Nov 17 '19 20:11 arturovt

Yeah that's probably better because it only affects the matching without assuming anything about other options (in case they change it in the future).

just-jeb avatar Nov 17 '19 20:11 just-jeb

If you don't know the original regex then you could do something like:

module.exports = config => {
  const fileLoader = config.rules.find(rule => rule.loader === 'file-loader');

  fileLoader.test = new RegExp(
    fileLoader.test.source
      .replace(/\|woff/, '')
      .replace(/\|woff2/, '')
      .replace(/\|ttf/, '')
  );

  config.rules.push({
    test: /\.(woff|woff2|ttf)$/,
    loader: 'base64-inline-loader',
  });

  return config;
};

Not very strong in regex tho :D

arturovt avatar Nov 18 '19 17:11 arturovt

@arturovt
import base64 from './image.jpg'; In this case, when your javascript/typescript file have directly dependency its ok. But, when i have dependency, in my css, like background: url(my/long/path/to/my/nice.svg), the test rule like /svg$/ doesnt work at all.

novathore avatar Nov 20 '19 09:11 novathore

@novathore Could you make a github repo with reproduction please? I do not wanna try to reproduce your behavior locally on my own...

arturovt avatar Nov 20 '19 09:11 arturovt

Closing as stale.

arturovt avatar Dec 22 '22 21:12 arturovt