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

grunt-writetags

v0.3.0

Published

Writes tags for JS and/or CSS files to a file

Downloads

2

Readme

grunt-writetags

Writes tags for JS and/or CSS files to a file

Getting Started

This plugin requires Grunt ~0.4.5

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-writetags --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-writetags');

The “writetags” task

Overview

This task takes an array of JavaScript and/or CSS file paths and writes <script src="..."></script> and <link href="..." rel="stylesheet" /> tags to a file whose path can be configured.

It is somewhat similar to grunt-script-link-tags, but differs in two ways:

  • It does not use a template where a certain part of the template file is replaced with the tags, but (over)writes the file.
  • Optionally, it can use absolute instead of relative paths.

Both are features I need in my personal workflow, which roughly looks like this:

  • In dev environment, do not concat or minify/uglify assets, i.e.: just have the tags in a file (or two files, if CSS and JS tags are needed separately).
  • In dist/prod environment, concat and minify/uglify assets plus “hashify” the filenames in order to optimize caching behaviour.
  • In either environment, treat the generated file(s) as partials (partial templates) which are used by a templating engine
  • In either environment, get <script> or <style> tags with absolute virtual paths, so that they can be easily used in a project with possibly deeply nested URL structures.

For concatenation and minification, I like grunt-contrib-uglify and grunt-hashres, but if you prefer something else: no problem.

Using the task

In your project's Gruntfile, add a section named writetags to the object passed grunt.initConfig() (or use an equivalent call with grunt.config()):

grunt.initConfig({
    writetags: {
        options: {
            'prefix': '/foo'
        },
        dev:  {
            paths:  ['inc/scripts1.js', 'inc/scripts2.js', 'inc/dev-scripts.js', 'inc/styles.css'],
            dest:   'templates/assets-dev.html'
        },
        dist:  {
            paths:  ['inc/scripts1.js', 'inc/scripts2.js', 'inc/styles.css'],
            dest:   'templates/assets-dist.html'
        }
    }
});

writetags is a multi-task, and in this example, there are two sub-tasks “dev” and “dist” with basically the same files (which, in real life, one would probably not define literally, but via Grunt templates or a JS array), except “dev” adds a file not included in “dist”. Of course, usually this would be combined with the typical other asset handling steps, especially concatenation and minification (see info above for tasks I like to use with this one).

Options

subtask.paths

Type: Array

An array of filesystem paths, relative to the directory containing Gruntfile.js

subtask.dest

Type: String

An filesystem paths, relative to the directory containing Gruntfile.js

subtask.scriptTemplate

Type: String

A template for the <script> tag(s) to be written, expected to contain a placeholder {{path}} for the path.

Default value, if not specified: <script src="{{path}}"></script>\n

For instance, can be used to add an attribute such as async.

subtask.styleTemplate

Type: String

A template for the CSS <link> tag(s) to be written, expected to contain a placeholder {{path}} for the path.

Default value, if not specified: <link rel="stylesheet" href="{{path}}" />\n

options.prefix

Type: String Default value: [empty string]

If not empty, this prefix is prepended to the filesystem path specified by options.dest in the src and href attribute values of the <script> and <link> tags. For instance, setting this option to “/” will guarantee that paths to the JavaScript and CSS files are absolute.

The prefix can either be defined in the options (global for all subtasks) and/or defined for single subtask(s). If both are the defined, the subtask-specific prefix wins.

options.replace

Type: Object Default value: null

If defined, this is an object whose keys (each expected to be a string) are replaced with the corresponding values (also expected to be strings) in the resulting paths. This setting can be used for arbitrary tweaking of the output paths.

Replacements can either be defined in the options (global for all subtasks) and/or defined for single subtask(s). If both are the defined, the subtask-specific replacements win.

Tests

There are no tests. This task is so simple (currently, roughly 50 lines of code) that I don’t see the necessity.

Release History

  • 10/15/2016 - 0.3.0: Added scriptTemplate and styleTemplate options, removed errors in Readme.
  • 12/16/2015 - 0.2.0: Added replace, improve Readme
  • 11/19/2015 - 0.1.0: First release. Very simple, but provides what I need.