Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hashfix #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
0.2.3 - 2017/01/12
----------------------
feat: support repath inline assets src

0.2.2 - 2016/8/26
----------------------
feat: support .htm files
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ The plugin accepts the following options:
- regx: must be instance of RegExp
- replace: must be a function return new path of html
- ignore: pass through to glob
- hashFix: fix atool-build assets hash repath
- xFixAssets: do not fix assets paths in html but fix hash
- hash: fix assets with hash paths in html
- hash: fix assets with hash paths in html
- forceRelative: absolute path in html would regard as relative, USED FOR publicPath

### License
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "html-repath-webpack-plugin",
"version": "0.2.2",
"version": "0.2.3",
"description": "manage html output path by this plugin",
"main": "index.js",
"scripts": {
Expand Down
15 changes: 15 additions & 0 deletions src/hashReplace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { filsMap, fixAssetsInHtml } from './util';
function hashReplace(compiler) {
compiler.plugin('compilation', (compilation) => {
const assets = compilation.assets;
let map = {};
compilation.plugin('html-webpack-plugin-after-html-processing', (htmlData, callback) => {
const htmlPluginData = htmlData;
map = filsMap(assets);
htmlPluginData.html = fixAssetsInHtml(htmlPluginData.html, '', map, true, false);
callback(null, htmlPluginData);
});
});
}

export default hashReplace;
26 changes: 11 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { join, normalize, parse, relative, dirname } from 'path';
import { join, normalize, relative, dirname } from 'path';
import isRelative from 'is-relative';
import glob from 'glob';
import { readFileSync } from 'fs';

import { fixAssetsInHtml, isValidExpression, isValidReplace } from './util';
import { fixAssetsInHtml, isValidExpression, isValidReplace, filesMap } from './util';
import hashReplace from './hashReplace';

export default class HtmlRepath {

Expand All @@ -14,6 +15,7 @@ export default class HtmlRepath {
ignore: '',
xFixAssets: false,
hash: false,
hashFix: false,
forceRelative: false,
};

Expand All @@ -22,12 +24,16 @@ export default class HtmlRepath {
}

apply(compiler) {
const opts = this.options;
const { hashFix } = opts;
if (hashFix) {
hashReplace(compiler);
return;
}
const context = normalize(compiler.context);
let outputPath = normalize(compiler.options.output.path);
outputPath = isRelative(outputPath) ? join(context, outputPath) : outputPath;
compiler.plugin('emit', (compilation, callback) => {
const opts = this.options;

if (!isValidExpression(opts.regx) || !isValidReplace(opts.replace)) {
return;
}
Expand All @@ -39,17 +45,7 @@ export default class HtmlRepath {
const assets = compilation.assets;
let map = {};
if (opts.hash) {
map = Object.keys(assets).reduce((prev, item) => {
const _prev = prev;
const pathInfo = parse(item);
const spInfo = pathInfo.name.split('-');
const extname = pathInfo.ext;
const hash = spInfo[1];
const name = spInfo[0];
_prev[name + extname] = hash;

return _prev;
}, {});
map = filesMap(assets);
}
const htmlFiles = glob.sync('**/*.+(html|htm)', globOpts);
htmlFiles.forEach((htmlFileName) => {
Expand Down
19 changes: 19 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,22 @@ export function isValidReplace(replace) {

return isValid;
}

export function filesMap(assets) {
let map = {};
if (!assets) {
return map;
}
map = Object.keys(assets).reduce((prev, item) => {
const _prev = prev;
const pathInfo = parse(item);
const spInfo = pathInfo.name.split('-');
const extname = pathInfo.ext;
const hash = spInfo[1];
const name = spInfo[0];
_prev[name + extname] = hash;

return _prev;
}, {});
return map;
}
12 changes: 12 additions & 0 deletions test/expect/with-hashFix.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>
<head>
<meta charset="utf-8">
<script src="common.js"></script>
<link rel="stylesheet" type="text/css" href="common.css">
<script src="index.js"></script>
<link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
<h1 class="gear-1">gear-1-test</h1>
</body>
</html>
30 changes: 29 additions & 1 deletion test/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { readFileSync } from 'fs';
import rimraf from 'rimraf';
import compile from './utils/compile';
import HtmlRepathPlugin from '../src';
import { isValidExpression, isValidReplace } from '../src/util';
import { isValidExpression, isValidReplace, filesMap } from '../src/util';

const fixDir = join(__dirname, 'fixtures');
const expDir = join(__dirname, 'expect');
Expand Down Expand Up @@ -124,6 +124,22 @@ describe('HtmlRepathPlugin', () => {
});
});

it('with params: hashFix', done => {
// 需要补充更详细的用例
compile(
new HtmlRepathPlugin({
hashFix: true,
hash: true,
}), true, () => {
const now = readFileSync(join(tmpDir, 'x/x/x/index.html'), 'utf-8');
const path = join(expDir, 'with-hashFix.html');
const exp = readFileSync(path, 'utf-8');
expect(now).to.equal(exp);
done();
});
done();
});

it('util with invalid regx', done => {
expect(isValidExpression).withArgs('*,djla$/').to.throwError();
done();
Expand All @@ -133,4 +149,16 @@ describe('HtmlRepathPlugin', () => {
expect(isValidReplace).withArgs('*,djla$/').to.throwError();
done();
});

it('util with filesMap', done => {
const arg = {
'test-abc.js': {},
'check-abc.css': {},
'aaa.html': '',
};
expect(filesMap('')).to.eql({});
const result = expect(filesMap(arg));
result.to.have.property('test.js', 'abc');
done();
});
});