npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

resource-hint-generator

v0.2.0

Published

NPM package for generating prefetch, preload and other HTML resource hint

Downloads

11

Readme

resource-hint-generator PRs Welcome

prefetch, preconnectdns-prefetch等资源优先级提示自动化生成工具。

配套讲解文章《1.4秒到0.4秒-2行代码让JS加载耗时减少67%》:https://juejin.cn/post/7274889579076108348

特性

  1. 自动遍历构建产物,生成注入脚本,运行后注入prefetch资源优先级提示link标签
  2. 指定域名,生成注入脚本,运行后注入preconnectdns-prefetch资源优先级提示link标签

演示:

在线运行DEMO:https://stackblitz.com/edit/github-ckfcpg?file=README.md,dist%2Fresource-hint.js

fe295039-983b-45a6-91b3-67d9131a8316

用法

0. 安装

npm install resource-hint-generator --save-dev

1. 配置

module.exports = {
  distPath: `./dist`,
  includeFileTestFunc: (fileName) => {
    return /(a.*js)|(b.*css)|(first.*js)/g.test(fileName);
  },
  publicPath: 'https://config.com/static/', 
  preconnectDomains: ['https://config.com'],
};

2. 运行

1. 在package.json中新增运行命令generate-resource-hint

2. 并追加运行命令到构建命令(build)后

例如:

// package.json
"scripts": {
    "generate-resource-hint": "resource-hint-generator",
    "build": "cross-env NODE_ENV=production webpack && npm run generate-resource-hint",
}

选项

配置项 | 简介 | 类型 | 默认值 -- | -- | -- | -- distPath | 打包产物路径,也是生成的resource-hint.js输出路径 | String | './dist' includeFileTestFunc | 指定一个函数,返回布尔值表示,遍历distPath找到的的fileName,是否会被作为<link rel="prefetch">href属性值 | Function | (fileName: string) => false includeFileNames | 指定一个数组,数组中的字符串元素,会被作为<link rel="prefetch">href属性值 | String[] | [] projectRootPath | 项目根目录路径 | String | '' resourceHintFileName | 生成的注入脚本名称 | String | 'resource-hint.js' crossOriginValue | <link>标签crossorigin的属性值,默认值undefined表示无crossorigin属性 | String | undefined | undefined publicPath | 用于和includeFileTestFuncincludeFileNames匹配到的文件名,拼接出<link rel="prefetch">标签的href属性值 | String | '' preconnectDomains | 指定一个数组,数组中的每个字符串元素,都将产生2个href属性值为当前字符串的<link rel="preconnect">标签和<link rel="dns-prefetch">标签 | String[] | [] configFileName | 本地配置文件名称 | String | 'resource-hint-generator-config.js'

欢迎 Issue && PR