Skip to content

Commit 08b4c54

Browse files
committed
Initial commit
0 parents  commit 08b4c54

File tree

10 files changed

+160
-0
lines changed

10 files changed

+160
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
dist/

index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
var assert = require('assert');
3+
4+
function HtmlWebpackWrapHtmlPlugin(options) {
5+
assert.equal(options, undefined, "HtmlWebpackWrapHtmlPlugin not need options");
6+
}
7+
HtmlWebpackWrapHtmlPlugin.prototype.apply = function(compiler) {
8+
var self = this;
9+
compiler.plugin('compilation', function(compilation) {
10+
compilation.plugin('html-webpack-plugin-after-html-processing', function(htmlPluginData, callback) {
11+
var options = htmlPluginData.plugin.options;
12+
if (options.before) {
13+
htmlPluginData.html = options.before + "\n" + htmlPluginData.html;
14+
}
15+
if (options.after) {
16+
htmlPluginData.html += "\n" + options.after;
17+
}
18+
19+
callback();
20+
});
21+
});
22+
};
23+
module.exports = HtmlWebpackWrapHtmlPlugin;

package.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "html-webpack-wrap-html-plugin",
3+
"version": "0.0.1",
4+
"description": "simplifies wrap HTML files to serve your webpack bundles",
5+
"main": "index.js",
6+
"scripts": {
7+
"prepublish": "npm run test",
8+
"test": "jasmine"
9+
},
10+
"keywords": [
11+
"html-webpack-plugin",
12+
"webpack",
13+
"wrap",
14+
"html",
15+
"plugin"
16+
],
17+
"author": "zman <hxfdarling@hotmail.com> (https://github.com/hxfdarling)",
18+
"license": "MIT",
19+
"repository": {
20+
"type": "git",
21+
"url": "git+https://github.com/hxfdarling/html-webpack-wrap-html-plugin.git"
22+
},
23+
"bugs": {
24+
"url": "https://github.com/hxfdarling/html-webpack-wrap-html-plugin/issues"
25+
},
26+
"homepage": "https://github.com/hxfdarling/html-webpack-wrap-html-plugin#readme",
27+
"devDependencies": {
28+
"html-loader": "^0.4.4",
29+
"html-webpack-plugin": "^2.24.1",
30+
"jasmine": "^2.5.2",
31+
"webpack": "^1.14.0"
32+
}
33+
}

readme.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# HtmlWebpackWrapHtmlPlugin
2+
This is a webpack plugin that simplifies wrap HTML files to serve your webpack bundles.
3+
Enhances html-webpack-plugin functionality by add `{before:"<!--before-->",after:"<!--after-->"}` option to allow you to wrap html content.
4+
## install by npm
5+
---
6+
```npm
7+
npm i --save-dev HtmlWebpackPlugin HtmlWebpackWrapHtmlPlugin
8+
```
9+
10+
## Basic usage
11+
---
12+
Require the plugin in you webpack config:
13+
```js
14+
var HtmlWebpackPlugin = require('HtmlWebpackPlugin');
15+
var HtmlWebpackWrapHtmlPlugin = require('HtmlWebpackWrapHtmlPlugin');
16+
```
17+
Add the plugin in you webpack config as fllow:
18+
```js
19+
plugins:[
20+
new HtmlWebpackPlugin({
21+
filename:"index.html",
22+
template:"src/index.html",
23+
before:"<!--you html before text-->",
24+
after:"<!--you html after text-->"
25+
}),
26+
new HtmlWebpackWrapHtmlPlugin()
27+
]
28+
29+
```
30+
## License
31+
[MIT](http://couto.mit-license.org/)
32+

spec/fixtures/after.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!--after html-->

spec/fixtures/before.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!--before html-->

spec/fixtures/entry.js

Whitespace-only changes.

spec/fixtures/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>HtmlWebpackWrapHtmlPlugin</title>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
</head>
8+
<body>
9+
10+
</body>
11+
</html>

spec/index.spec.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* eslint-env jasmine */
2+
var path = require('path');
3+
var fs = require('fs');
4+
var webpack = require('webpack');
5+
var rm_rf = require('rimraf');
6+
var HtmlWebpackPlugin = require('html-webpack-plugin');
7+
var HtmlWebpackWrapHtmlPlugin = require('../');
8+
var OUTPUT_DIR = path.join(__dirname, '../dist');
9+
describe('HtmlWebpackWrapHtmlPlugin', function() {
10+
beforeEach(function(done) {
11+
rm_rf(OUTPUT_DIR, done);
12+
});
13+
var before = fs.readFileSync(path.join(__dirname, 'fixtures/before.txt')),
14+
after = fs.readFileSync(path.join(__dirname, 'fixtures/after.txt'));
15+
it('should insert before.txt before html and after.txt after html', function(done) {
16+
webpack({
17+
entry: {
18+
app: path.join(__dirname, 'fixtures', 'entry.js'),
19+
},
20+
output: {
21+
path: OUTPUT_DIR,
22+
filename: '[name].js'
23+
},
24+
module: {
25+
26+
},
27+
plugins: [
28+
new HtmlWebpackPlugin({
29+
before: before,
30+
after: after
31+
}),
32+
new HtmlWebpackWrapHtmlPlugin()
33+
]
34+
}, function(err) {
35+
expect(err).toBeFalsy();
36+
var htmlFile = path.resolve(__dirname, '../dist/index.html');
37+
fs.readFile(htmlFile, 'utf8', function(er, data) {
38+
expect(er).toBeFalsy();
39+
var html = data.toString();
40+
var beforeReg = new RegExp('^' + before),
41+
afterReg = new RegExp(after + '$');
42+
expect(beforeReg.exec(html).length).toBe(1);
43+
expect(afterReg.exec(html).length).toBe(1);
44+
done();
45+
});
46+
done();
47+
});
48+
});
49+
});

spec/support/jasmine.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"spec_dir": "spec",
3+
"spec_files": [
4+
"**/*[sS]pec.js"
5+
],
6+
"stopSpecOnExpectationFailure": false,
7+
"random": true
8+
}

0 commit comments

Comments
 (0)