includes-loader
A webpack loader for any text file, and support nested includes with flexible options
Installation
npm install includes-loader --save-dev
Usage
var confStr = require('includes!./file.conf')'
// confStr is 'foo\nbar\nconf'
Details as follow
directory
├── file.conf
└── foo
├── part.conf
└── bar
└── part.conf
./file.conf
#include "./foo/part"
conf
./foo/part.conf
foo
#include "./bar/part"
./foo/bar/part.conf
bar
Configuration
includes options
default options
includes: {
pattern: {
re: /#include\s+?"(.+?)";*?/,
index: 1
},
extensions: [] // the extension will be same to the including file
}
pattern
An object that should be used to specify how to match the include statement patten.re is the RegExp to match the include statement patten.index is the index of filepath in the matched results
// can be set to a function with filepath argument
pattern: function (filepath) {
var pattern
if (/\.html$/.test(filepath)) {
pattern = {
re: /<!--#\s*?include\s+?virtual=("|')(.+?)\1\s*?-->/,
index: 2
}
}
return pattern
}
extensions
An array of extensions that should be used to resolve includes, If you include file with their own extension (e.g. #include './somefile.ext'), you must add an empty string in the array.
// can be set to a function with filepath argument
extensions: function (filepath) {
var extensions
if (/\.html$/.test(filepath)) {
extensions = ['', '.html', '.shtml', '.htm']
} else if (/\.glsl$/.test(filepath)) {
extensions = ['', '.glsl', '.vert', '.frag']
}
return extensions
}
expamle webpack.config
module.exports = {
module: {
module: {
loaders: [{
test: /\.html$/,
loader: 'html!includes'
}, {
test: /\.glsl$/,
loader: 'includes'
}]
}
},
includes: {
extensions: function (filepath) {
var extensions
if (/\.html$/.test(filepath)) {
extensions = ['', '.html', '.shtml', '.htm']
} else if (/\.glsl$/.test(filepath)) {
extensions = ['', '.glsl', '.vert', '.frag']
}
return extensions
},
pattern: function (filepath) {
var pattern
// only custom includes pattern for html
if (/\.html$/.test(filepath)) {
pattern = {
re: /<!--#\s*?include\s+?virtual=("|')(.+?)\1\s*?-->/,
index: 2
}
}
return pattern
}
}
};