Skip to content

Commit ca52905

Browse files
committed
prepare to publish
1 parent 3185f34 commit ca52905

3 files changed

Lines changed: 120 additions & 9 deletions

File tree

README.md

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,94 @@
1+
<a href="https://www.npmjs.com/package/inline-source-webpack-plugin"><img src="https://img.shields.io/npm/v/inline-source-webpack-plugin.svg" alt="Version"></a>
2+
13
# inline-source-webpack-plugin
2-
A plugin for webpack4.x
4+
A webpack plugin to embed css/js resource in the html with inline-source module(webpack 4.x and HtmlWebpackPlugin is needed).
5+
> This plugin doesn't support webpack v1.x,v2.x and v3.x. If you need this feature, you can try [this](https://github.com/KyLeoHC/inline-resource-plugin).
6+
7+
## Install
8+
```bash
9+
$ npm install -S inline-source-webpack-plugin
10+
```
11+
12+
## example
13+
```html
14+
<!-- ./build/hello.html -->
15+
<!DOCTYPE html>
16+
<html lang="en">
17+
<head>
18+
<meta charset="UTF-8">
19+
<title>test</title>
20+
<link href="inline.css" inline>
21+
<script src="inline.js" inline></script>
22+
</head>
23+
<body>
24+
<div class="container">
25+
<h1>hello world!</h1>
26+
</div>
27+
</body>
28+
</html>
29+
```
30+
31+
```js
32+
/* ./src/inline.js */
33+
function Person() {
34+
}
35+
36+
Person.prototype.sayHello = function () {
37+
var word = 'hello';
38+
console.log(word);
39+
};
40+
```
41+
42+
```css
43+
/* ./src/inline.css */
44+
.container {
45+
border: 1px solid #000;
46+
}
47+
```
48+
49+
Output:
50+
```html
51+
<!-- ./build/hello.html -->
52+
<!DOCTYPE html>
53+
<html lang="en">
54+
<head>
55+
<meta charset="UTF-8">
56+
<title>test</title>
57+
<style>.container{border:1px solid #000}</style>
58+
<script>function Person(){}Person.prototype.sayHello=function(){var o="hello";console.log(o)};</script>
59+
</head>
60+
<body>
61+
<div class="container">
62+
<h1>hello world!</h1>
63+
</div>
64+
</body>
65+
</html>
66+
```
67+
68+
## Usage
69+
Available `options` include(refer to [this](https://github.com/popeindustries/inline-source#usage) for more options):
70+
- `compress`: enable/disable compression.(default `false`)
71+
- `rootpath`: path used for resolving inlineable paths.
72+
73+
```javascript
74+
// webpack.config.js example
75+
const HtmlWebpackPlugin = require('html-webpack-plugin');
76+
const InlineSourceWebpackPlugin = require('inline-source-webpack-plugin');
77+
78+
module.exports = {
79+
...
80+
plugins: [
81+
new HtmlWebpackPlugin({
82+
...
83+
}),
84+
new InlineSourceWebpackPlugin({
85+
compress: true,
86+
rootpath: './src'
87+
})
88+
]
89+
};
90+
```
91+
92+
## License
93+
94+
[MIT License](https://github.com/KyLeoHC/inline-source-webpack-plugin/blob/master/LICENSE)

index.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,45 @@
11
const {inlineSource} = require('inline-source');
22

3-
class InlineResourceWebpackPlugin {
3+
class InlineSourceWebpackPlugin {
44
constructor(options = {}) {
5-
this.options = options;
6-
this.name = 'InlineResourceWebpackPlugin';
5+
this.name = 'InlineSourceWebpackPlugin';
6+
this.options = Object.assign({
7+
compress: false
8+
}, options);
79
}
810

911
apply(compiler) {
1012
compiler.hooks.compilation.tap(this.name, compilation => {
13+
// if htmlWebpackPlugin is not exist, just do nothing
1114
if (compilation.hooks.htmlWebpackPluginAfterHtmlProcessing) {
1215
compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync(
1316
this.name,
1417
(data, cb) => {
15-
inlineSource(data.html, this.options)
18+
const options = Object.assign({}, this.options);
19+
options.handlers = (source, context) => {
20+
if (context && context.sources && context.sources.length) {
21+
// watch inline target
22+
context.sources.forEach(item => {
23+
if (compilation.fileDependencies.add) {
24+
compilation.fileDependencies.add(item.filepath);
25+
} else {
26+
// Before Webpack 4 - fileDepenencies was an array
27+
compilation.fileDependencies.push(item.filepath);
28+
}
29+
});
30+
}
31+
if (this.options.handlers) {
32+
return this.options.handlers(source, context);
33+
}
34+
return Promise.resolve();
35+
};
36+
inlineSource(data.html, options)
1637
.then(html => {
1738
data.html = html;
1839
cb(null, data);
1940
})
2041
.catch(error => {
21-
cb(null, data);
22-
throw error;
42+
compilation.errors.push(error);
2343
});
2444
}
2545
);
@@ -28,4 +48,4 @@ class InlineResourceWebpackPlugin {
2848
}
2949
}
3050

31-
module.exports = InlineResourceWebpackPlugin;
51+
module.exports = InlineSourceWebpackPlugin;

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"description": "A plugin for webpack4.x",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
87
},
98
"repository": {
109
"type": "git",

0 commit comments

Comments
 (0)