diff --git a/HISTORY.md b/HISTORY.md index af99e37..95f87f0 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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 diff --git a/README.md b/README.md index 49e6961..1711252 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index bb534d0..8de55ed 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/hashReplace.js b/src/hashReplace.js new file mode 100644 index 0000000..26a8bb7 --- /dev/null +++ b/src/hashReplace.js @@ -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; diff --git a/src/index.js b/src/index.js index 1be14ab..0a54754 100644 --- a/src/index.js +++ b/src/index.js @@ -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 { @@ -14,6 +15,7 @@ export default class HtmlRepath { ignore: '', xFixAssets: false, hash: false, + hashFix: false, forceRelative: false, }; @@ -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; } @@ -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) => { diff --git a/src/util.js b/src/util.js index 4ad4843..7cb00a4 100644 --- a/src/util.js +++ b/src/util.js @@ -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; +} diff --git a/test/expect/with-hashFix.html b/test/expect/with-hashFix.html new file mode 100644 index 0000000..4ad85a7 --- /dev/null +++ b/test/expect/with-hashFix.html @@ -0,0 +1,12 @@ + + + + + + + + + +

gear-1-test

+ + diff --git a/test/index-test.js b/test/index-test.js index f90d5b0..4ee8295 100644 --- a/test/index-test.js +++ b/test/index-test.js @@ -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'); @@ -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(); @@ -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(); + }); });