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-include-replace-s2

v1.3.5

Published

!!!this is ad hoc!!! Grunt task to include files, replace variables and remove if blocks. Allows for parameterised includes. bug fixed

Downloads

4

Readme

grunt-include-replace-s2

Grunt task to include files, replace variables and remove if blocks.

NOTE!

This is not thoroughly tested or efficient. It was created for a small use-case.

Take care when using it

===

Allows for parameterised file includes:

hello.html

<!DOCTYPE html>
<h1>Hello World!</h1>
@@include('/path/to/include/message.html', {"name": "Joe Bloggs", "premium": false})

message.html

<p>Hello @@name!</p>
@@_IF_:premium
<div>Premium member!</div>
@@_ENDIF_

Result:

<!DOCTYPE html>
<h1>Hello World!</h1>
<p>Hello Joe Bloggs!</p>

Getting Started

This plugin requires Grunt ~0.4.1

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-include-replace --save-dev

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

grunt.loadNpmTasks('grunt-include-replace');

The "includereplace" task

Overview

The task allows you to preprocess your project file contents by replacing placeholder "variables" and including content from other files. In addition to "global" variables that are replaced in all files you specify, the task introduces the concept of "local" variables, which are passed as parameters to included files.

WARNING: The task does not check for recursive includes.

In your project's Gruntfile, add a section named includereplace to the data object passed into grunt.initConfig().

grunt.initConfig({
  includereplace: {
    your_target: {
      options: {
        // Task-specific options go here.
      }
      // Files to perform replacements and includes with
      src: '*.html',
      // Destination directory to copy files to
      dest: 'dist/'
    }
  }
})

Options

options.globals

Type: Object Default value: {}

Global variables availabe for replacement in all files.

options.prefix

Type: String Default value: @@

Variable/include directive prefix. Careful when changing as it is added to the regular expression used for finding variables to be replaced.

options.suffix

Type: String Default value: ``

Variable/include directive suffix. Careful when changing as it is added to the regular expression used for finding variables to be replaced.

options.includesDir

Type: String Default value: Relative to including file

Directory where includes will be resolved.

options.docroot

Type: String Default value: .

@@docroot is a magic local variable that contains the relative path from the file that uses it to the path specified.

options.processIncludeContents

Type: Function Default value: undefined

A function called for every included file prior to processing by grunt-include-replace. It is passed the include file contents and local variables as parameters and should return the (possibly altered) file contents.

Usage Examples

Default Options

includereplace: {
  dist: {
    options: {
      globals: {
        var1: 'one',
        var2: 'two',
        var3: 'three'
      },
    },
    src: '*.html',
    dest: 'dist/'
  }
}
Files array format
includereplace: {
  dist: {
    options: {
      globals: {
        var1: 'one',
        var2: 'two',
        var3: 'three'
      },
    },
    files: [
      {src: 'js/**/*.js', dest: 'dist/', expand: true, cwd: 'src/'},
      {src: '*.css', dest: 'dist/css/', expand: true, cwd: 'src/css'}
    ]
  }
}
Files object format
includereplace: {
  dist: {
    options: {
      globals: {
        var1: 'one',
        var2: 'two',
        var3: 'three'
      },
    },
    files: {
      'dist/js/': ['**/*.js', '!dist/**/*.js'],
      'dist/css/': ['**/*.css', '!dist/**/*.css']
    }
  }
}

Custom Options

The following example allows include statements to appear as comments in HTML files by altering the prefix and suffix. Also all includes are resolved relative to the directory inc/ (relative to your Gruntfile) rather than relative to including file.

includereplace: {
  dist: {
    options: {
      prefix: '<!-- @@',
      suffix: ' -->',
      includesDir: 'inc/'
    },
    src: '*.html',
    dest: 'dist/'
  }
}

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

  • 2014-04-02   v1.3.4   Added ad hoc code
  • 2014-02-20   v1.3.0   Added functionality to remove IF blocks
  • 2013-12-30   v1.2.0   Rename like grunt-contrib-copy by specifying dest filename (for single files)
  • 2013-06-19   v1.1.0   Added magic local variable @@docroot: relative path from the file that uses it to the path specified
  • 2013-06-19   v1.0.0   Refactored files processing code to use Grunt files API properly
  • 2013-05-03   v0.4.0   Support for cwd directive
  • 2013-04-26   v0.3.0   Added new option includesDir - if set all includes resolved relative to that directory
  • 2013-04-19   v0.2.0   Added option processIncludeContents - a function that allows you to alter included file contents
  • 2013-02-18   v0.1.0   Grunt 0.4.x support