How do install fonts using npm? - npm

I want to install fonts using npm, for example, Open Sans or Roboto.
If I search for Open Sans on npm and filter for packages with over 1000 downloads per month I find a whole list. I am not sure which source to choose here, some are not well maintained and none of them are from the original source of the font, in this case, google.
npm-font-open-sans
typeface-open-sans
open-sans-all
open-sans-fontface
opensans-npm-webfont
I noticed that fonts are often used through a direct link to fonts.googleapis. I would prefer to have a local copy of the font to be able to develop offline. Is there a common way to install fonts through npm? Or is there another automated font download tool that I'm not aware of?

I use typefaces yarn add typeface-roboto
and then just do a require("typeface-roboto") / import "./typeface-roboto" or whatever font you choose.
I hope this is the answer you're looking for?
Use fontsource, typefaces is deprecated now.
yarn add #fontsource/open-sans // npm install #fontsource/open-sans
Then within your app entry file or site component, import it in. For example in Gatsby, you could choose to import it into a layout template (layout.js), page component (index.js), or gatsby-browser.js.
import "#fontsource/open-sans" // Defaults to weight 400 with all styles included.
Fontsource allows you to select weights and even individual styles, allowing you to cut down on payload sizes to the last byte! Utilizing the CSS unicode-range selector, all language subsets are accounted for.
import "#fontsource/open-sans/500.css"; // All styles included.
import "#fontsource/open-sans/900-normal.css"; // Select either normal or italic.
Alternatively, the same solutions could be imported via SCSS!
#import "~#fontsource/open-sans/index.css";
#import "~#fontsource/open-sans/300-italic.css";

Fontsource
The typefaces project is now deprecated and its successor is fontsource.
Usage
There's no much difference at the point of using it.
Install:
yarn add #fontsource/open-sans // npm install #fontsource/open-sans
Import:
import "#fontsource/open-sans"
Reference:
body {
font-family: "Open Sans";
}

Related

SAP Spartacus - Sparta-Theme requesting Open Sans by default - not DSGVO compliant

since January 2022 there is a court decision in germany which states that using Google Fonts over their web api is no longer data protection compliant.
Spartacus requests the Open Sans font initially by default. The font url is defined in the /_variables.scss of the sparta theme.
Unfortunately I wasn´t able to find a way to overwrite this variable or prevent the storefront from requesting the font in any way.
There should be any option to not load this font initially. Not only because this is an DSGVO topic but also to get rid of unnecessary requests at initial load.
Does anyone have an idea how to solve this or is there any recommendation from the Spartacus Team?
With that said above in the comments section,
you have 3 options:
1 - wait for the backport to the maintenance branch(es), and upgrade accordingly
2 - create your own custom package from the Spartacus source code, and update the libraries that imports the entire #import '~#spartacus/styles/scss/app';. You can can change it to the same one found in this pull request
3 - NOT ideal, but in your shell app, you can remove the stylesheets that causes those problems from the "styles" array property in angular.json, and/or remove the import to the styles on the respective package, and copy the styles from source to the stylesheet themselves.
The affected libraries are cart, order, and epd-visualization.

Rollup - Preserve modules + css

Can't find any resources online, but i'm trying to optimize our in-house component library, which i'm trying to make more tree shaker friendly.
How with rollup can i leave .css files in the output along with maintain their import in the file.
I.E
Foo.js (inside import "./foo.css")
Foo.css
Output.
Foo.js (inside import "./foo.css" remains) created into module
Foo.css
This seems as straight forward as possible and iv'e found similar threads asking for this but zero responses. https://github.com/egoist/rollup-plugin-postcss/issues/204
Allowing this will basically mean when people who consume my project will only get critical css automatically.
I.E Import { Foo } from "xyz/foo" will automatically import the accompanying css file.
Unfortunately I couldn't find a solution with Rollup for what you're looking for. However, if you're open to using Webpack there's a plugin that would make this possible called MiniCssExtractPlugin. It creates CSS files per JS file and would achieve the structure you're wanting.

how often do bootstrap variables change

Switching from "bootstrap": "~4.0.0-beta.2" to "bootstrap": "^4.0.0" added new variables in the _variables.scss. Boostrap classnames only change when there is a major version upgrade (AFAIK), but does the same apply to _variables.scss too?
It would change how we share the _variables.scss accross our application.
Does variable names change between releases?
The variable names shouldn't change as Bootstrap 4 is now in "real" release (out of beta).
The variables are there for a good reason: Your CSS shouldn't break when you upgrade. (not counting major versions of course).
Of course new variables can be added, as those shouldn't break your CSS.
If you are in any way unsure before upgrading, you can always check the changelogs. Those should tell about any breaking changes.
Using variables in SCSS
When you have set up your build tools, and created your SCSS file (let's say custom.scss), you can import Bootstrap and override any variable you want (you'll find them in _variables.scss in the Bootstrap source code):
//Your variable overrides
//Let us change the primary color which is set in the primary variable
$primary: #ce40c5;
//In this case we are importing everything
//Here we are using NodeJS with NPM, so your files should be here
#import "node_modules/bootstrap/scss/bootstrap";
Some things to note:
If you aren't using NodeJS, then just make sure that the import path directs to the bootstrap.scss file (don't put .scss in the import)
If you have checked the source code of Bootstrap 4, you may have seen !default after every variable. Don't include that in your override. It just tells Sass that the variable can be replaced
There's a bunch of good information about this in the docs, so feel free to check there if you are missing anything (or ask).

Styling of Mirage 2 using themes from bootswatch.com

Based from the Readme files of Mirage 2, it says here:
If you want to base your theme on an existing Bootstrap theme (like the ones at bootswatch.com) you can do so by using the standard Bootstrap color scheme and replacing the import of Bootstrap in bootstrap_color_scheme/_main.scss:
#import "../vendor/bootstrap-sass-official/vendor/assets/stylesheets/bootstrap";
with an import of just its _variables.sccs file (those variables need to be defined, because they are used in _dspace-bootstrap-tweaks.scss):
#import "../vendor/bootstrap-sass-official/vendor/assets/stylesheets/bootstrap/_variables";
Then import the the css file(s) of your Bootstrap theme of choice below it. Depending on the theme you may also need to update the twbs-font-path function right above that import statement.
What I did:
Ok, so I activated the mirage2_bootstrap_color_scheme maven profile. Copied the bootstrap_color_scheme/_main.scss from dspace-xmlui-mirage2/src/main/webapp/styles/ into dspace/modules/xmlui-mirage2/src/main/webapp/themes/Mirage2/styles.
I changed the text in _main.scss that says #import "../vendor/bootstrap-sass-official/vendor/assets/stylesheets/bootstrap"; into #import "../vendor/bootstrap-sass-official/vendor/assets/stylesheets/bootstrap/_variables";, and then I copied the bootstrap.min.css that I downloaded from bootswatch.com and put it in the same directory with _main.scss and then I referenced it in _main.scss with an #import "bootstrap.min.css"; statement.
I'm having this error when running mvn package:
error styles/main.scss (Line 12: File to import not found or unreadable: variables.`
I also tried putting the _variables.scss under the bootstrap_color_scheme directory and used the #import "variables"; but I got the same not found or unreadable error.
Can someone guide me on how to correctly use the themes from bootswatch.com to apply to Mirage 2? An example and a simple walk through of using a sample theme from bootswatch.com would be great.
Thanks in advance.
I deleted my other answer because I was mistaken.
Here is an example of a working bootswatch theme:
https://github.com/antoine-atmire/DSpace/tree/mirage2%2Bbootswatch/dspace/modules/xmlui-mirage2/src/main/webapp/themes/Mirage2/styles
The /styles/bootstrap_color_scheme/_main.scss file gets copied to /styles/main.scss during the build process, so the #import statements should be relative to /styles/main.scss.
Bootstrap-sass has updated its paths, and the Mirage 2 docs are not yet updated.
_variables.scss is now stored here: "../vendor/bootstrap-sass-official/assets/stylesheets/bootstrap/_variables.scss"
Depending on which version you're using, it can even be in another place.
A general recommendation is to look at the vendor folder in:
dspace/modules/xmlui/target/xmlui-*/themes/Mirage2/vendor/
Look for the path of the variables file there and use that path.

Inlining Bootstrap SASS images with Compass

A bit about environment: we do UI build automation with Grunt, we do use Twitter Bower for managing third-party dependencies, as we don't want to keep third-party code in our repository, we use Compass for CSS extension.
Currently making a compressed version of vendor assets into single CSS file and encountered a problem, that Compass doesn't transform somehow images to inline ones while build. We want all images to be inlined into resulting CSS file with Data URL (as long as we support browsers newer than IE9 =).
Master SCSS file including Bootstrap SASS looks like
// styles/main.scss
$iconSpritePath: '../components/bootstrap-sass/img/glyphicons-halflings.png';
$iconWhiteSpritePath: '../components/bootstrap-sass/img/glyphicons-halflings-white.png';
//..
#import "../components/bootstrap-sass/lib/bootstrap";
Compass command looks like
compass compile --css-dir target/compass/styles \
--sass-dir app/styles --images-dir app/images --output-style expanded
Resulting output is like
// target/compass/styles/main.css
/* line 18, ../../../app/components/bootstrap-sass/lib/_sprites.scss */
[class^="icon-"],
[class*=" icon-"] {
display: inline-block;
...
/* WANT THIS IMAGE INLINED */
background-image: url("../components/bootstrap-sass/img/glyphicons-halflings.png");
...
}
So, main desire is to get all url() expressions to hold base64 encoded images inline. As an alternative, we can switch to LESS, if it provides this ability more easily. Actually, a good thing, because we'd eliminate dependency on Ruby/Compass and we'd be able to install everything with NPM
try this
inline-image($image, $mime-type)
http://compass-style.org/reference/compass/helpers/inline-data/#inline-image
http://blog.derekperez.com/post/755676493/smarter-sprites-inline-image-function-with-sass
Multiple Background Images using Sass / Compass
Just changed the variable for base path in bootstrap SASS main file. it helped.