Adds an include
function which places the given file into a string at compile-time.
$ npm install babel-plugin-include
.babelrc
{
"plugins": ["include"]
}
$ babel --plugins include script.js
require('babel').transform('code', {
plugins: ['include']
});
Given text.txt
with the contents:
Hello, World!
the following JS:
let file = include("text.txt");
will be compiled to:
let file = "Hello, World!";
- The file is included relative to the JS file the
include
is in unless aroot
is specified in the plugin options, in which case, theroot
is used. (See below for info onroot
) - The default encoding is utf8 however that can be changed
- Special characters/unprintables are automatically escaped
- The
include
function takes a single string as argument. Any following arguments are ignored.
babel-plugin-include
allows you to change various settings by providing an options object by using the following instead:
{
plugins: [
['include', { options }]
]
}
where { options }
is the options object. The following options are available:
The root option specifies the root in which files are included from. e.g.:
{
plugins: [
['include', {
'root': 'proj/src'
}]
]
}
The encoding option specifies which encoding to use when including files. Default is utf8
{
plugins: [
['include', {
'encoding': 'ucs2'
}]
]
}
The normalize newline option specifies whether newlines should be normalized or not. This converts \r\n
to \n
and removes and trailing newlines. Disable this for binary files or other applicable locations.
{
plugins: [
['include', {
'encoding': 'ucs2'
}]
]
}