Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sass mixin still uses ~ (tilde) in $directory which is unsupported by newer bundlers #798

Open
michaelfaith opened this issue Sep 1, 2023 · 3 comments
Labels
bug Something isn't working contributors welcome! Open to outside contributors sass Sass related issue

Comments

@michaelfaith
Copy link

michaelfaith commented Sep 1, 2023

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.json to 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.

image

Version

5.0.2

OS

Windows 11

Browser

Chrome

Additional context

Recommend removing the ~ from

$directory: if(
$directory,
$directory,
'~@fontsource#{if($isVariable, '-variable', '')}/#{map.get($metadata, id)}/files'
);

@michaelfaith michaelfaith added the bug Something isn't working label Sep 1, 2023
@jwr1 jwr1 added the sass Sass related issue label Sep 2, 2023
@ayuhito
Copy link
Member

ayuhito commented Sep 3, 2023

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 $directory Sass 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 $esbuildDirectory variable with the non-tilde path that can be passed into $directory when importing the Sass mixins? We could then include a notice in the Angular guide to remark on the difference.

@michaelfaith
Copy link
Author

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.

@ayuhito ayuhito added the contributors welcome! Open to outside contributors label Oct 3, 2023
@RBilly
Copy link
Contributor

RBilly commented Apr 9, 2024

Alternatively I fixed this issue by doing the following in the vite config file:

resolve: {
    alias: [
      {
        // Workaround to emulate behavior of ~ prefix in Wepback that refers to a relative path in node_modules folder
        find: /~(.+)/,
        replacement: `${process.cwd()}/node_modules/$1`
      }
    ]
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working contributors welcome! Open to outside contributors sass Sass related issue
Projects
None yet
Development

No branches or pull requests

4 participants