@bfoese/ngx-preload-fonts
Version:
Angular post-build tool to inject font preload links for fingerprinted fonts into the index file.
78 lines (77 loc) • 5.04 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = __importDefault(require("chalk"));
const commander_1 = require("commander");
const figlet = __importStar(require("figlet"));
const fs = __importStar(require("fs"));
const file_util_1 = require("./file.util");
const log = console.log;
const handleError = (message) => {
log(chalk_1.default.red(message));
process.exit(1);
};
console.log(figlet.textSync('ngx-preload-fonts', { horizontalLayout: 'full' }), '\n\n');
const injectionMarker = '<!-- inject:preload-fonts --><!-- endinject -->';
const regexInjectionMarker = /(<!-- inject:preload-fonts -->)([\s\S]*?)(<!-- endinject -->)/gm;
commander_1.program
.version('1.0.0')
.option('-d, --dist <dist>', 'Path to build output directory. Should be identical with `outputPath` property from `angular.json`')
.option('-f, --file [file]', `Your index file name. If not provided, this will default to ${chalk_1.default.yellow.bold('index.html')}. Should be identical with 'index' property from 'angular.json'. Make sure, that this file contains the injection marker, which will be replaced with the generated preload links: ${chalk_1.default.yellow.bold(injectionMarker)}`)
.option('-i, --include [include]', 'Optional comma separated list of font names to include for the preload link generation. Other font names will be ignored. Provide the names without file extension, e.g. "helvetica" instead of "helvetica.woff2"')
.option('-e, --exclude [exclude]', 'Optional comma separated list of font names to exclude from the preload link generation. Provide the names without file extension, e.g. "helvetica" instead of "helvetica.woff2"')
.parse(process.argv);
if (!process.argv.slice(2).length) {
commander_1.program.outputHelp();
}
if (!commander_1.program.dist) {
handleError('Please specify the build output directory of your application');
commander_1.program.outputHelp();
}
commander_1.program.dist = file_util_1.FileUtil.normalizePath(commander_1.program.dist);
commander_1.program.file = commander_1.program.file ?? 'index.html';
const appBuildDirs = file_util_1.FileUtil.getAppBuildDirs(commander_1.program.dist, commander_1.program.file);
if (!appBuildDirs || appBuildDirs.length === 0) {
handleError(`Could not find ${chalk_1.default.underline(commander_1.program.file)} in ${chalk_1.default.underline(commander_1.program.dist)} or on of its subdirectories. Did you defined the right build output path and is your build finished?`);
}
for (const appBuildDir of appBuildDirs) {
const files = fs.readdirSync(appBuildDir);
const fonts = file_util_1.FileUtil.filterFonts(files, commander_1.program.include ? commander_1.program.include.split(',') : undefined, commander_1.program.exclude ? commander_1.program.exclude.split(',') : undefined);
if (fonts && fonts.size > 0) {
const preloadFontLinks = Array.from(fonts.keys())
.map((font) => `<link rel="preload" as="font" href="${font}" type="font/${fonts.get(font)}" crossorigin="anonymous">`)
.join('\n');
const indexFilePath = `${appBuildDir}/${commander_1.program.file}`;
const indexFileContent = fs.readFileSync(indexFilePath, 'utf8');
const match = indexFileContent.match(regexInjectionMarker);
if (!match || match.length === 0) {
log(chalk_1.default.red(`Inserted ${chalk_1.default.yellow.underline.bold('0')} preload font links into ${chalk_1.default.yellow.underline(indexFilePath)}: Could not detect the injection marker ${chalk_1.default.yellow(injectionMarker)}. Did you forget to include the marker or was is already replaced by a previous run?`));
}
else {
fs.writeFileSync(indexFilePath, indexFileContent.replace(regexInjectionMarker, preloadFontLinks));
log(chalk_1.default.green(`Inserted ${chalk_1.default.yellow.underline.bold(fonts.size)} preload font links into ${chalk_1.default.yellow.underline(indexFilePath)}`));
}
}
}