fontsource: Sass mixin still uses ~ (tilde) in $directory which is unsupported by newer bundlers
Describe the bug
The use of ~ in the css url statement for the $directory variable default is causing errors in Angular’s newer esbuild-based build system. My understanding is webpack is moving away from this convention as well. Angular’s non-esbuild build system supports with or without ~. So it would seem to me that removing the ~ from the $directory default would allow for maximum capability.
Steps to Reproduce
- Create new angular app:
ng new test-app - Install fontsource package
yarn add @fontsource/roboto - Include the mixin in
styles.scss:
@use '@fontsource/roboto/scss/mixins' as roboto;
@include roboto.faces();
- Switch
angular.jsonto esbuild builder Change:
...
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
...
to
...
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser-esbuild",
...
- Run
ng build
Expected behavior
Should build successfully. But instead I receive the following error:
X [ERROR] Could not resolve "~@fontsource/roboto/files/roboto-latin-400-normal.woff" [plugin angular-css-resource]
../../dist/components/theme.css:69:509:
69 │ ...ff2"),url("~@fontsource/roboto/files/roboto-...
╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can remove the tilde and use a relative path to reference it, which should remove this error.
Version
5.0.2
OS
Windows 11
Browser
Chrome
Additional context
Recommend removing the ~ from https://github.com/fontsource/fontsource/blob/0f4d2c779601f496dac118299a818a7d1069726b/packages/cli/src/sass/mixins.ts#L43-L47
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 10
- Comments: 15 (7 by maintainers)
That would certainly help as things transition away from tildes. At some point, though, it seems like non-tilde will be the norm. So it may be worth considering reversing the defaults at some point when it makes sense. But yeah, an interim solution might help.
The new
@fontsource-utils/scssmixins part of the Fontsourcev5.2.0release drops the~prefix in favour no tilde at all as the new default. Thanks for everyone’s patience!The new package should also make it much easier for us to push breaking changes related to SCSS without having to bump the main non-SCSS related font packages with a major release. Hopefully, making these types of issues much quicker to resolve in the future.
You can learn more about the new package in the documentation.
Alternatively I fixed this issue by doing the following in the vite config file:
I have fixed this issue by providing
$directoryhowever I am running into issue in my angular test.Build fix:
After this fix I am now facing hen I run
ng testThis looks like somehow the $directory workaround had no effect from test runtime/workflow.
I think this idea may unintentionally affect many builds with older bundlers and potentially be breaking for some of them. I’m a little uncomfortable making such a change without knowing how many users this could impact.
The current workaround would be to use the
$directorySass mixin parameter with the correct path, but maybe we could make things easier for users by including additional preset variables defining the path? e.g. we include an$esbuildDirectoryvariable with the non-tilde path that can be passed into$directorywhen importing the Sass mixins? We could then include a notice in the Angular guide to remark on the difference.