Skip to content

MarkAPhillips/grunt-inject-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

grunt-inject-js v.0.1.10

npm version

Grunt task that allows for multiple js files to be injected into a file. Inspired by grunt-inject

Getting Started

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-inject-js --save-dev

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

grunt.loadNpmTasks('grunt-inject-js');

The injectjs task

Overview

Run this task with the grunt injectjs command.

Task targets, files and options may be specified according to the Grunt Configuring tasks guide.

The task is to allow for the injection of multiple javascript scripts into a document at defined places in the document. The use case arose when having to inject different third party analytics code at different locations.

grunt.initConfig({
   injectjs: {
       dev:
        {
          files:{
            'test/output/index.html': 'test/fixtures/index.html'
          },
          scriptsrc: 'test/fixtures/*.js'
        }
      }
});

Required properties

files

Type: Grunt file configuration

The src HTML files must have the following comment(s) which are replaced by the injected JavaScript:

<!-- inject:[jsfile] -->

where [jsfile] is the JavaScript file name to be injected. The file extension .js should be omitted.

The follow configuration would inject three files into the html document at the specified locations:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Grunt Inject JS Test file</title>
    <!-- inject:google -->
    <!-- inject:woopra -->
</head>
<body>
<h1>Test File</h1>
<p>
    Content of test file
</p>
<!-- inject:pardot -->
</body>
</html>

Optional properties

scriptsrc

Type: String || Array || file glob

The path of the script(s) to be injected into the page.

It is recommended to have a directory that includes all the JavaScript files to be injected. A warning is raised if the directory contains non-JavaScript files.

This property is ignored if clear is set to true.

clear

Type: Boolean

If this property is set to true then the scriptsrc is ignored and all tags following the pattern

<!-- inject:[A-Za-z]* --> are removed from the file.

This property was added as in some environments no Javascript files need to be injected and all inject placeholders removed from the file(s).

Release History

* 2016-07-14   v0.1.10  

Update Readme text.

* 2016-07-14   v0.1.9  

Update Grunt peer dependencies and Grunt versions

* 2015-01-16   v0.1.8   Beta Version

Update Readme text.

* 2015-01-16   v0.1.7   Beta Version

Add clear option to task and update tests and Readme text.

* 2015-01-14   v0.1.6   Beta Version

Use grunt standardised verbose messaging.

* 2015-01-13   v0.1.5   Beta Version

Final updates for Readme text.

* 2015-01-13   v0.1.4   Beta Version

Update Readme text and changed notifications.

* 2015-01-13   v0.1.3   Beta Version

Code refactor and improved user notifications.

* 2015-01-13   v0.1.2   Beta Version

Improved error handling when no files exist.

* 2015-01-12   v0.1.1   Beta Version

Renamed task name from inject_js to injectjs and associated code changes.

* 2015-01-12   v0.1.0   Beta Version

Initial release.

About

Grunt Task that allows for multiple js files to injected into a file

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published