foundation-emails icon indicating copy to clipboard operation
foundation-emails copied to clipboard

@font-face definitions don't get embedded into HTML build

Open jsit opened this issue 8 years ago • 13 comments

$ foundation new --framework emails

$ npm run build

// src/assets/scss/app.scss

@import 'settings';
@import 'foundation';

@font-face {
  font-family: 'Franklin Gothic';
  src: url("https://example.com/Franklin-Gothic.woff") format("woff");
}

The @font-face definition doesn't get embedded in the HTML build, so an element with font-face:"Franklin Gothic"; won't be rendered in Franklin Gothic.

jsit avatar Mar 24 '16 17:03 jsit

I've gotten around this by wrapping my @font-face declarations inside @media all {} to force them to be embedded.

jsit avatar Mar 24 '16 20:03 jsit

Ok, that's good to know! Were you running npm start or npm run build? Just making sure the inliner didn't do this.

rafibomb avatar Mar 24 '16 21:03 rafibomb

npm run build. The @font-face definitions get into app.css just fine, but they don't make it into index.html.

jsit avatar Mar 25 '16 13:03 jsit

Hmm I wonder if it's the inliner removing them https://github.com/jonkemp/gulp-inline-css

There should be a wrapper to preserve some styles like media queries. Will have to look into this more. If you find that this was resolved by a newer version of the inliner, we can update it.

rafibomb avatar Mar 25 '16 16:03 rafibomb

The @media all workaround wasn't working for me so I found another and wanted to share incase anyone else was still having trouble with this. I tweaked the gulpfile to manually grab a file with all the @font-face declarations and toss it in with the inliner.

// Inlines CSS into HTML, adds media query CSS into the <style> tag of the email, and compresses the HTML
function inliner(css) {
  var css = fs.readFileSync(css).toString();
  // where `fontsFilePath` is relative path to a file with @font-face declarations
  var fonts = fs.readFileSync(fontsFilePath).toString();
  var mqCss = siphon(css);

  var pipe = lazypipe()
    .pipe($.inlineCss, {
      applyStyleTags: false
    })
    // include both `fonts` and `mqCss` in the style tags
    .pipe($.injectString.replace, '<!-- <style> -->', `<style>${fonts}${mqCss}</style>`)
    .pipe($.htmlmin, {
      collapseWhitespace: true,
      minifyCSS: true
    });

  return pipe();
}

aricallen avatar Apr 07 '16 16:04 aricallen

I have tried this several times, but continue to get the following error:

[14:49:36] 'inline' errored after 544 ms [14:49:36] TypeError: Cannot read property 'replace' of undefined at inliner (gulpfile.babel.js:119:11) at inline (gulpfile.babel.js:87:28) at bound (domain.js:254:14) at runBound (domain.js:267:12) at asyncRunner (/Users/trowley/Desktop/projects/email/campaigns/eye_safety/node_modules/async-done/index.js:36:18) at process._tickDomainCallback (node.js:381:11) [14:49:36] 'build' errored after 5.84 s [14:49:36] 'default' errored after 5.84 s

Perhaps I am placing the .css file with the @font-face declarations in the wrong place.

thomasjrowley avatar Jun 21 '16 19:06 thomasjrowley

I would maybe try console.log'ing the contents returned from the fs.readFileSync(fontsFilePath).toString(); to make sure that the file is being found correctly.

aricallen avatar Jun 24 '16 17:06 aricallen

@thomasjrowley : it was due to usage of $.injectString.replace instead of $.replace

kartikv11 avatar Jul 28 '16 08:07 kartikv11

Hello! May be somebody can help to understand what am i do wrong?

I got the Muller Bold font. Using the squirell servise i made otf files and put them on my server and add @font-face to fonts.scss file.

By using nethod of Thomas Rowley i changed gulpfile.

But in the code, that i got after "npm run buid", - i can't finde any @font-face including.

default

rapman avatar Feb 21 '17 15:02 rapman

@jsit - thanks for the tip about wrapping them in the media all declaration, that worked for me...only problem I am facing now is that my font files are local and I am not sure where to put them. When I put them in the src/assets/ directory I am unable to reference them because everything is being served from the dist/ directory. I moved the fonts into dist/assets directory, but everything I run "foundation build" they get removed from that directory. Any tips?

jtsurf1 avatar Mar 17 '17 13:03 jtsurf1

I don't know but I add my fonts as usual in the head as a

    <link href="https://fonts.googleapis.com/css?family=Muli|Titillium+Web" rel="stylesheet">

and after running npm run build it loaded the font.

Thanks, T04435.

T04435 avatar Apr 01 '17 12:04 T04435

Duplicate of https://github.com/zurb/foundation-emails/issues/543

DanielRuf avatar Mar 11 '18 21:03 DanielRuf

An example of what the whole process might look like with @aricallen's example, for those who are struggling.

  1. Create a file named 'fonts.scss' in the src/assets/scss directory.
  2. Then the config below will pick up that file - config below is in your gulpfile.babel.js file in the root directory of your project.
// Inlines CSS into HTML, adds media query CSS into the <style> tag of the email, and compresses the HTML
function inliner(css) {
  var css = fs.readFileSync(css).toString();
  // where `fontsFilePath` is relative path to a file with @font-face declarations
  var fonts = fs.readFileSync("src/assets/scss/fonts.scss").toString();
  var mqCss = siphon(css);

  var pipe = lazypipe()
    .pipe($.inlineCss, {
      applyStyleTags: false
    })
    // include both `fonts` and `mqCss` in the style tags
    .pipe($.injectString.replace, '<!-- <style> -->', `<style>${fonts}${mqCss}</style>`)
    .pipe($.htmlmin, {
      collapseWhitespace: true,
      minifyCSS: true
    });

  return pipe();
}
  1. Stop and restart the npm task.
  2. If you get any file/directory not found errors, just check that your fonts.scss location and the reference path in your gulp file.

Sgouws avatar Feb 28 '19 10:02 Sgouws