1
- import { join , normalize , parse , relative , dirname } from 'path' ;
1
+ import { join , normalize , relative , dirname } from 'path' ;
2
2
import isRelative from 'is-relative' ;
3
3
import glob from 'glob' ;
4
4
import { readFileSync } from 'fs' ;
5
5
6
- import { fixAssetsInHtml , isValidExpression , isValidReplace } from './util' ;
6
+ import { fixAssetsInHtml , isValidExpression , isValidReplace , filesMap } from './util' ;
7
+ import hashReplace from './hashReplace' ;
7
8
8
9
export default class HtmlRepath {
9
10
@@ -14,6 +15,7 @@ export default class HtmlRepath {
14
15
ignore : '' ,
15
16
xFixAssets : false ,
16
17
hash : false ,
18
+ hashFix : false ,
17
19
forceRelative : false ,
18
20
} ;
19
21
@@ -22,12 +24,16 @@ export default class HtmlRepath {
22
24
}
23
25
24
26
apply ( compiler ) {
27
+ const opts = this . options ;
28
+ const { hashFix } = opts ;
29
+ if ( hashFix ) {
30
+ hashReplace ( compiler ) ;
31
+ return ;
32
+ }
25
33
const context = normalize ( compiler . context ) ;
26
34
let outputPath = normalize ( compiler . options . output . path ) ;
27
35
outputPath = isRelative ( outputPath ) ? join ( context , outputPath ) : outputPath ;
28
36
compiler . plugin ( 'emit' , ( compilation , callback ) => {
29
- const opts = this . options ;
30
-
31
37
if ( ! isValidExpression ( opts . regx ) || ! isValidReplace ( opts . replace ) ) {
32
38
return ;
33
39
}
@@ -39,17 +45,7 @@ export default class HtmlRepath {
39
45
const assets = compilation . assets ;
40
46
let map = { } ;
41
47
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 ) ;
53
49
}
54
50
const htmlFiles = glob . sync ( '**/*.+(html|htm)' , globOpts ) ;
55
51
htmlFiles . forEach ( ( htmlFileName ) => {
0 commit comments