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

docpad-plugin-frontend

v3.0.0

Published

CSS and JS assets manager, based on Frontend Grunt.js task

Downloads

57

Readme

A DocPad plugin that manages CSS and JS assets. Combined with Grunt.js’ Frontend task, creates cache-reset links to static files.

The main reasons to use this plugin are:

  • Get compiled by Grunt.js resources and output links to them with last modified date prefix to effectively reset cache every time resource is re-compiled.
  • Organize resources into sets that can be selectively redefined in templates and documents.

Installation

Run npm install --save docpad-plugin-frontend command in your DocPad project root.

How it works

This plugin extends template data object with assets(type) method. When called inside layout template, returns sorted list of files for specified type. The type is simply a string identifier of the resource type, for example, css or js.

Whenever you call assets(type) method from template, it will collect resource list of specified type from current document and layout with respect of layout templates inheritance.

Simple example

In your layout template, let’s say default.html.eco, define list of CSS and JS resources in document’s meta data with css and js properties. You can use any name for resources: js, jsFooter, styles etc.

---
css: "/css/style.css"
js:  ["/js/fileA.js", "/js/fileB.js"]
---
<!doctype html>
<html>
<body>
    
</body>
</html>

In the same template, use assets() method to get list of resources (I’m using Eco templates, but you can use any other):

---
css: "/css/style.css"
js:  "/js/fileA.js,/js/fileB.js"
---
<!doctype html>
<html>
<head>
    <% for url in @assets('css'): %><link rel="stylesheet" href="<%= url %>" /><% end %>
    <% for url in @assets('js'): %><script src="<%= url %>"></script><% end %>
</head>
<body>
    
</body>
</html>

If you’re using Grunt.js’ Frontend template task, generated file urls may look like this:

<!doctype html>
<html>
<head>
    <link rel="stylesheet" href="/20121101001423/css/style.css" />
    <script src="/20121101001423/js/fileA.js"></script>
    <script src="/20121001001129/js/fileB.js"></script>
</head>
<body>
    
</body>
</html>

As you can see, generated urls contains numeric prefix which is last compilation date, taken from build catalog generated by Grunt.js Frontend task. Such prefixes can effectively reset cache from static resources every time this resource is recompiled.

Organizing resources

You can organize resources into sets that can be overridden by descendant templates or document. All you need is to add numeric suffix to resource type, like this: css2, js10 and so on.

For example, in default.html.eco template you can define the following resource sets in meta data:

---
js: "fileA.js"
js2: ["fileB.js", "fileC.js"]
js3: "fileD.js"
---

In page.html.eco template, which inherits default.html.eco, you can redefine resource set:

---
layout: default
js2: "foo.js"
---

You can also redefine resource sets in document that uses one of these templates:

---
layout: page
js3: "page.js"
---

The final resource list retrieved by assets('js') call for your document will look like this:

fileA.js
foo.js
page.js

All sets are sorted by numeric suffix.

Debug mode

If you’re using grunt-frontend task to build CSS and JS, you can use a special debug mode. In this mode, assets() method will return a list of source files instead minified ones.

To enable debug mode, you need to set frontendDebug config property to true. It’s best to use different DocPad environment for debugging:

docpadConfig = {
    …
    environments:
        debug:
            frontendDebug: true
}

To view your web-site in debug mode, pass debug environment option to DocPad:

docpad run --env=debug

DocPad configuration

In order to correctly handle cache-busted links with built-in DocPad server, you need to update your docpad.coffee config: add serverAfter event to docpadConfig.

docpadConfig = {
    events:
        # Extend server so it can respond to cache-reset assets
        serverAfter: ({server}) ->
            server.get /^\/\d+\/(c|j)\//, (req, res, next) ->
                req.url = req.url.replace /^\/\d+\//, '/'
                next()
}

module.exports = docpadConfig

A real-world example is available in Emmet Documentation web-site source.