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

packing-template

v1.2.4

Published

这是 Packing 前端开发工具用来处理网页模版的工具包。本包提供两个方法:

Downloads

21

Readme

packing-template

这是 Packing 前端开发工具用来处理网页模版的工具包。本包提供两个方法:

  • middleware
  • plugin

这两个方法的参数可以通过下面两种方式传入:

  • 在函数调用处通过 options 传入。这种方式传入的参数对所有 entry points 都有效。
  • 通过与 ${entry}.js 同目录的 ${entry}.settings.js 配置文件传入。这种方式传入的参数仅对单一 entry point 有效。
import path from 'path';

export default {
  template: path.resolve(__dirname, 'template.html'),
  title: 'Page A',
  keywords: 'A AA',
  description: 'A simple text',
  attrs: ['img:src', 'link:href', 'script:src'],
  city: 'Beijing'
};

middleware

这是一个 express 中间件,提供开发环境下网页自动生成、脚本注入的功能。

用法

import Express from 'express';
import { middleware } from 'packing-template';

const app = new Express();

middleware(app, appConfig, {
    template: path.resolve(__dirname, 'template.html'),
    // inject: 'head',
    // favicon: 'xxx.png'
    // charset: 'gb2312'
});

参数

app

express 实例。

appConfig

packing 配置。

options

可选参数:

template

类型: String

默认值: node_modules/packing-template/templates/default.html

模版文件的绝对路径。

inject

类型: String

默认值: body

可选值: - body: 插入在 标签前面 - head:: 插入在 标签前面

js脚本注入到 HTML 代码的位置。

是否在网页中插入 favico meta标签。当取值不是 false 时,该值表示 favicon 的路径。

chunks

类型: [String] | null

默认值: null

需要在页面中插入的 chunk 列表。

excludeChunks

类型: [String] | null

默认值: null

不需要在页面中插入的 chunk 列表。

chunksSortMode

类型: String

默认值: commonChunksFirst

可选值: - id: 按 chunk id 正序 - none: 按 entries 中的顺序 - reverse: 按 entries 中的顺序反转 - manual: 按 chunks 中指定的顺序 - commonChunksFirst: 公共包优先

chunk 包在页面中插入的顺序。

favicon

类型: Boolean | String

默认值: false

是否在网页中插入 favico meta标签。当取值不是 false 时,该值表示 favicon 的路径。

title

类型: String

默认值: untitled

网页标题。

charset

类型: String

默认值: utf-8

网页使用的字符集。

keywords

类型: Boolean | String

默认值: utf-8

是否在网页中插入 keywords meta标签。当取值不是 false 时,该值表示 keywords 的值。

description

类型: Boolean | String

默认值: utf-8

是否在网页中插入 description meta标签。当取值不是 false 时,该值表示 description 的值。

plugin

这是一个 webpack 插件,它在发布时会根据 entry points 文件生成对应的网页文件。

用法

import Express from 'express';
import { middleware } from 'packing-template';

const app = new Express();

middleware(app, appConfig, {
    template: path.resolve(__dirname, 'template.html'),
    // inject: 'head',
    // favicon: 'xxx.png'
    // charset: 'gb2312'
});

参数

app

express 实例。

appConfig

packing 配置。

options

可选参数:

template

类型: String

默认值: node_modules/packing-template/templates/default.html

模版文件的绝对路径。

inject

类型: String

默认值: body

可选值: - body: 插入在 标签前面 - head: 插入在 标签前面

js脚本注入到 HTML 代码的位置。

是否在网页中插入 favico meta标签。当取值不是 false 时,该值表示 favicon 的路径。

chunks

类型: [String] | null

默认值: null

需要在页面中插入的 chunk 列表。

excludeChunks

类型: [String] | null

默认值: null

不需要在页面中插入的 chunk 列表。

chunksSortMode

类型: String

默认值: commonChunksFirst

可选值: - id: 按 chunk id 正序 - none: 按 entries 中的顺序 - reverse: 按 entries 中的顺序反转 - manual: 按 chunks 中指定的顺序 - commonChunksFirst: 公共包优先

chunk 包在页面中插入的顺序。

favicon

类型: Boolean | String

默认值: false

是否在网页中插入 favico meta标签。当取值不是 false 时,该值表示 favicon 的路径。

title

类型: String

默认值: untitled

网页标题。

charset

类型: String

默认值: utf-8

网页使用的字符集。

keywords

类型: Boolean | String

默认值: utf-8

是否在网页中插入 keywords meta标签。当取值不是 false 时,该值表示 keywords 的值。

description

类型: Boolean | String

默认值: utf-8

是否在网页中插入 description meta标签。当取值不是 false 时,该值表示 description 的值。

attrs

类型: Array

默认值: [img:src, link:href]

网页文件中需要在编译时替换为 _hash 的标签属性。格式为 tag:attribute 。如果想对所有标签的某个属性替换,请使用 * 代替 tag,如所有标签的 src 属性都需要替换,则使用 *:src

pug 模版支持的一些约定

pug 模版灵活的语法,网页可以被拆分成很多没有规律的代码片段,这种复杂的结构导致了自动注入代码变得很困难,为了简化自动注入代码的逻辑,现对 pug 做以下约定:

  • 模版分为两类
    • layout 布局母模版
    • pages 页面入口模版,可以是通用模版,也可以是与 entry points 一一对应的模版(为了兼容 packing v3 以下版本)
  • layout 母模版必须包含以下 block
    • meta: 自动注入的 favicokeywordsdescription 会 append 在该代码末尾
    • title: 自动注入的 title 会替换默认网页标题
    • style: 自动注入的 CSS 样式表会 append 在该代码末尾
    • script: 自动注入的 javascript 脚本会 append 在该代码末尾
  • 使用 extends 方式从母模版页面继承入口页面