inline-resource-plugin
A webpack plugin to embed css/js resource in the html with inline-source module.
Install
$ npm install inline-resource-plugin --save-dev
example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<link href="inline.css" inline>
<script src="inline.js" inline></script>
</head>
<body>
<div class="container">
<h1>hello world!</h1>
</div>
</body>
</html>
function Person() {
}
Person.prototype.sayHello = function () {
var word = 'hello';
console.log(word);
};
.container {
border: 1px solid #000000;
}
Output:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>test</title>
<style>.container{border:1px solid #000}</style>
<script>function Person(){}Person.prototype.sayHello=function(){var o="hello";console.log(o)};</script>
</head>
<body>
<div class="container">
<h1>hello world!</h1>
</div>
</body>
</html>
Usage
Available options
include:
compile
: If the file that you want to embed need to be compiled(such as ES6 or require), you can pass 'true'.(default false
)compress
: enable/disable compression.(default true
)rootpath
: path used for resolving inlineable paths.template
: the path of your template file.This option is used for finding out the files which need to embed into the template.(required)test
: the file which you want to execute embed task.If you have multiple templates,you'd better use regx to specify the template that you want to execute inline task.filename
: If you decide to use the other plugins such as HtmlWebpackPlugin to generate template file,you can ignore this option.Or you can pass the path and we will generate template file by ourselves.
note: test
and filename
option at least one is needed(Refer to the example below).
const HtmlWebpackPlugin = require('html-webpack-plugin');
const InlineResourcePlugin = require('inline-resource-plugin');
module.exports = {
entry: {
hello: './src/hello'
},
output: {
path: './build',
publicPath: '/inline-resource-plugin/example/build/',
filename: '[name].js'
},
plugins: [
new HtmlWebpackPlugin({
filename: 'hello_result.html',
template: './src/hello.html',
inject: 'body'
}),
new InlineResourcePlugin({
compile: true,
compress: true,
rootpath: './src',
template: './src/hello.html',
test: /^hello_result\.html$/
}),
new InlineResourcePlugin({
compile: false,
compress: true,
rootpath: './src',
template: './src/world.html',
filename: 'world_result.html'
})
]
};
note: You can find this demo in the example directory.
Events
Available event include:
inline-resource-plugin-html-after-emit
: This event represent that the compile and embed task has been completed.You may need it when you use this plugin with hot module replacement feature.
example:
compiler.plugin('inline-resource-plugin-html-after-emit', function (data, callback) {
hotMiddleware.publish({action: 'reload'});
callback();
});
License
MIT License