Skip to content

Commit a2be415

Browse files
author
勤拭
committed
hashfix
1 parent 9efb7b8 commit a2be415

File tree

8 files changed

+93
-18
lines changed

8 files changed

+93
-18
lines changed

HISTORY.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
0.2.3 - 2017/01/12
2+
----------------------
3+
feat: support repath inline assets src
4+
15
0.2.2 - 2016/8/26
26
----------------------
37
feat: support .htm files

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ The plugin accepts the following options:
4848
- regx: must be instance of RegExp
4949
- replace: must be a function return new path of html
5050
- ignore: pass through to glob
51+
- hashFix: fix atool-build assets hash repath
5152
- xFixAssets: do not fix assets paths in html but fix hash
52-
- hash: fix assets with hash paths in html
53+
- hash: fix assets with hash paths in html
5354
- forceRelative: absolute path in html would regard as relative, USED FOR publicPath
5455

5556
### License

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "html-repath-webpack-plugin",
3-
"version": "0.2.2",
3+
"version": "0.2.3",
44
"description": "manage html output path by this plugin",
55
"main": "index.js",
66
"scripts": {

src/hashReplace.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { filsMap, fixAssetsInHtml } from './util';
2+
function hashReplace(compiler) {
3+
compiler.plugin('compilation', (compilation) => {
4+
const assets = compilation.assets;
5+
let map = {};
6+
compilation.plugin('html-webpack-plugin-after-html-processing', (htmlData, callback) => {
7+
const htmlPluginData = htmlData;
8+
map = filsMap(assets);
9+
htmlPluginData.html = fixAssetsInHtml(htmlPluginData.html, '', map, true, false);
10+
callback(null, htmlPluginData);
11+
});
12+
});
13+
}
14+
15+
export default hashReplace;

src/index.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { join, normalize, parse, relative, dirname } from 'path';
1+
import { join, normalize, relative, dirname } from 'path';
22
import isRelative from 'is-relative';
33
import glob from 'glob';
44
import { readFileSync } from 'fs';
55

6-
import { fixAssetsInHtml, isValidExpression, isValidReplace } from './util';
6+
import { fixAssetsInHtml, isValidExpression, isValidReplace, filesMap } from './util';
7+
import hashReplace from './hashReplace';
78

89
export default class HtmlRepath {
910

@@ -14,6 +15,7 @@ export default class HtmlRepath {
1415
ignore: '',
1516
xFixAssets: false,
1617
hash: false,
18+
hashFix: false,
1719
forceRelative: false,
1820
};
1921

@@ -22,12 +24,16 @@ export default class HtmlRepath {
2224
}
2325

2426
apply(compiler) {
27+
const opts = this.options;
28+
const { hashFix } = opts;
29+
if (hashFix) {
30+
hashReplace(compiler);
31+
return;
32+
}
2533
const context = normalize(compiler.context);
2634
let outputPath = normalize(compiler.options.output.path);
2735
outputPath = isRelative(outputPath) ? join(context, outputPath) : outputPath;
2836
compiler.plugin('emit', (compilation, callback) => {
29-
const opts = this.options;
30-
3137
if (!isValidExpression(opts.regx) || !isValidReplace(opts.replace)) {
3238
return;
3339
}
@@ -39,17 +45,7 @@ export default class HtmlRepath {
3945
const assets = compilation.assets;
4046
let map = {};
4147
if (opts.hash) {
42-
map = Object.keys(assets).reduce((prev, item) => {
43-
const _prev = prev;
44-
const pathInfo = parse(item);
45-
const spInfo = pathInfo.name.split('-');
46-
const extname = pathInfo.ext;
47-
const hash = spInfo[1];
48-
const name = spInfo[0];
49-
_prev[name + extname] = hash;
50-
51-
return _prev;
52-
}, {});
48+
map = filesMap(assets);
5349
}
5450
const htmlFiles = glob.sync('**/*.+(html|htm)', globOpts);
5551
htmlFiles.forEach((htmlFileName) => {

src/util.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,22 @@ export function isValidReplace(replace) {
5656

5757
return isValid;
5858
}
59+
60+
export function filesMap(assets) {
61+
let map = {};
62+
if (!assets) {
63+
return map;
64+
}
65+
map = Object.keys(assets).reduce((prev, item) => {
66+
const _prev = prev;
67+
const pathInfo = parse(item);
68+
const spInfo = pathInfo.name.split('-');
69+
const extname = pathInfo.ext;
70+
const hash = spInfo[1];
71+
const name = spInfo[0];
72+
_prev[name + extname] = hash;
73+
74+
return _prev;
75+
}, {});
76+
return map;
77+
}

test/expect/with-hashFix.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<html>
2+
<head>
3+
<meta charset="utf-8">
4+
<script src="common.js"></script>
5+
<link rel="stylesheet" type="text/css" href="common.css">
6+
<script src="index.js"></script>
7+
<link rel="stylesheet" type="text/css" href="index.css">
8+
</head>
9+
<body>
10+
<h1 class="gear-1">gear-1-test</h1>
11+
</body>
12+
</html>

test/index-test.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { readFileSync } from 'fs';
44
import rimraf from 'rimraf';
55
import compile from './utils/compile';
66
import HtmlRepathPlugin from '../src';
7-
import { isValidExpression, isValidReplace } from '../src/util';
7+
import { isValidExpression, isValidReplace, filesMap } from '../src/util';
88

99
const fixDir = join(__dirname, 'fixtures');
1010
const expDir = join(__dirname, 'expect');
@@ -124,6 +124,22 @@ describe('HtmlRepathPlugin', () => {
124124
});
125125
});
126126

127+
it('with params: hashFix', done => {
128+
// 需要补充更详细的用例
129+
compile(
130+
new HtmlRepathPlugin({
131+
hashFix: true,
132+
hash: true,
133+
}), true, () => {
134+
const now = readFileSync(join(tmpDir, 'x/x/x/index.html'), 'utf-8');
135+
const path = join(expDir, 'with-hashFix.html');
136+
const exp = readFileSync(path, 'utf-8');
137+
expect(now).to.equal(exp);
138+
done();
139+
});
140+
done();
141+
});
142+
127143
it('util with invalid regx', done => {
128144
expect(isValidExpression).withArgs('*,djla$/').to.throwError();
129145
done();
@@ -133,4 +149,16 @@ describe('HtmlRepathPlugin', () => {
133149
expect(isValidReplace).withArgs('*,djla$/').to.throwError();
134150
done();
135151
});
152+
153+
it('util with filesMap', done => {
154+
const arg = {
155+
'test-abc.js': {},
156+
'check-abc.css': {},
157+
'aaa.html': '',
158+
};
159+
expect(filesMap('')).to.eql({});
160+
const result = expect(filesMap(arg));
161+
result.to.have.property('test.js', 'abc');
162+
done();
163+
});
136164
});

0 commit comments

Comments
 (0)