Grunt Inline Import

A Grunt plugin that inlines file imports.
Getting Started
This plugin requires Grunt >= 0.4.0
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-inline-import --save-dev
Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:
grunt.loadNpmTasks("grunt-inline-import");
Usage
The inlining process is destructive. Affected files will be changed permanently. Create a
backup first!
To inline your file imports, you need to specify the path to the JavaScript
file that should be modified. Additionally, you need to define the
extensions of the relevant import statements.
text.txt
hello world
index.js
import stuff from "garage";
import text from "./text.txt";
Gruntfile.js
inlineImport: {
options: {
extensions: {
".txt": "utf8"
}
},
task: {
src: "index.js"
}
}
index.js (inlined)
import stuff from "garage";
const text = "hello world";
Glob
You may use glob patterns to inline a bunch of files at once.
inlineImport: {
options: {
extensions: {
".html": "utf8",
".css": "utf8"
}
},
task: {
src: "src/**/tpl.js"
}
}
Options
You may provide special glob options for the glob mechanism.
For more information, check out the options of the inline-import tool.
inlineImport: {
options: {
extensions: {
".html": "utf8",
".png": "base64"
},
encoding: "utf8",
useVar: true,
glob: { ... },
},
task: {
options: {
extensions: {
".glsl": "utf8"
}
},
src: "index.js"
}
}
Creating a Backup
In order to create a backup of specific files, you'll need tools for copying and deleting files. The following example uses the basic grunt
plugins grunt-contrib-copy and grunt-contrib-clean.
Gruntfile.js (copy setup)
copy: {
backup: {
expand: true,
cwd: "src",
src: "**/tpl.js",
dest: "backup",
filter: "isFile"
},
restore: {
expand: true,
cwd: "backup",
src: "**",
dest: "src",
filter: "isFile"
}
}
Gruntfile.js (clean setup)
clean: {
backup: ["backup"]
}
Gruntfile.js (tasks)
grunt.registerTask("backup", ["restore", "copy:backup"]);
grunt.registerTask("restore", ["copy:restore", "clean:backup"]);
grunt.registerTask("prepublish", ["backup", "inlineImport"]);
grunt.registerTask("postpublish", ["restore"]);
Contributing
Maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.