Insert code from one file into another.
Useful when you want to build a single file containing everything it needs for initial rendering. Such as on mobile, where the number of requests directly effects user experience. At the moment it only overwrites the destination file. So be careful to consider that in the build process.
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-insert --save-dev
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt.loadNpmTasks('grunt-insert');
In your project's Gruntfile, add a section named insert
to the data object passed into grunt.initConfig()
.
grunt.initConfig({
insert: {
options: {
// Task-specific options go here.
},
your_target: {
// Target-specific file lists and/or options go here.
},
},
});
Type: string
Default value: null
Specify a string to be removed from the source code.
In this example the javascript is contained in a separate file for development purposes. Then stuffed in the script tag of the html file.
grunt.initConfig({
insert: {
options: {},
main: {
src: "lib/js/stuff.js",
dest: "dist/html/index.html",
match: /\/\/Stuff it here/
},
},
});
Note the match parameter is actually a regular expression.
Then in the destination file, index.html, make sure you place the match string wherever you want source code to be placed. It could look something like this
<!doctype html>
<html>
<head>
<script>
//Stuff it here
</script>
</head>
<body>
Blah blah.
</body>
</html>
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.
(Nothing yet)