A webpack plugin to embed css/js resource in the html with inline-source module.
⚠️ This plugin only support webpack 1.x, 2.x and 3.x.For webpack 4.x or any webpack version, Use inline-source-webpack-plugin instead.
$ npm install inline-resource-plugin --save-dev
<!-- ./build/hello.html -->
<!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>
/* ./src/inline.js */
function Person() {
}
Person.prototype.sayHello = function () {
var word = 'hello';
console.log(word);
};
/* ./src/inline.css */
.container {
border: 1px solid #000000;
}
Output:
<!-- ./build/hello.html -->
<!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>
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'.(defaultfalse
)compress
: enable/disable compression.(defaulttrue
)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).
// webpack.config.js example
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({ // this InlineResourcePlugin is work with HtmlWebpackPlugin(As it has supplied 'test' option)
compile: true,
compress: true,
rootpath: './src',
template: './src/hello.html', // Just keep the same with the 'template' option of HtmlWebpackPlugin
test: /^hello_result\.html$/ // A regular expression that match the 'filename' option of HtmlWebpackPlugin
}),
new InlineResourcePlugin({ // this InlineResourcePlugin is work alone(As it has supplied 'filename' option)
compile: false,
compress: true,
rootpath: './src',
template: './src/world.html',
filename: 'world_result.html' // If you use 'filename' option, you don't need to supply 'test' option
})
]
};
note: You can find this demo in the example directory.
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();
});