UNPKG

gatsby-plugin-prefetch-google-fonts

Version:

A Gatsby plugin to download and prefetch [Google Fonts](https://fonts.google.com/). Can increase performance as opposed to loading webfonts from Google's external stylesheet.

99 lines (76 loc) 2.62 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); var _require = require("fs-extra"), pathExists = _require.pathExists, readFile = _require.readFile, outputFile = _require.outputFile, remove = _require.remove, copy = _require.copy; var Hash = require("object-hash"); var downloadCSS = require("./download-css"); var mergeCSS = require("./merge-css"); var downloadFonts = require("./download-fonts"); var path = require("path"); exports.onPreBootstrap = /*#__PURE__*/ function () { var _ref = (0, _asyncToGenerator2.default)( /*#__PURE__*/ _regenerator.default.mark(function _callee(config, options) { var cachedPath, hashPath, hash; return _regenerator.default.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: cachedPath = path.join("./.cache/google-fonts/", config.pathPrefix); hashPath = path.join(cachedPath, "hash.txt"); hash = Hash(options); _context.next = 5; return pathExists(hashPath); case 5: _context.t0 = !_context.sent; if (_context.t0) { _context.next = 12; break; } _context.next = 9; return readFile(hashPath, "utf8"); case 9: _context.t1 = _context.sent; _context.t2 = hash; _context.t0 = _context.t1 !== _context.t2; case 12: if (!_context.t0) { _context.next = 23; break; } _context.next = 15; return remove("./.cache/google-fonts"); case 15: _context.next = 17; return downloadCSS(options, config); case 17: _context.next = 19; return mergeCSS(config); case 19: _context.next = 21; return downloadFonts(config); case 21: _context.next = 23; return outputFile(hashPath, hash); case 23: _context.next = 25; return copy("".concat(cachedPath, "/fonts"), "./public/google-fonts"); case 25: case "end": return _context.stop(); } } }, _callee, this); })); return function (_x, _x2) { return _ref.apply(this, arguments); }; }();