@@ -3,88 +3,63 @@ import path from "path";
33import { PostHTML } from "posthtml" ;
44import hasha from "hasha" ;
55
6- const DEFAULT_PATH = "" ;
76const DEFAULT_HASH_LENGTH = 20 ;
8- const DEFAULT_OPTIONS : IOptions = {
9- path : DEFAULT_PATH ,
10- hashLength : DEFAULT_HASH_LENGTH ,
11- css : true ,
12- js : true ,
13- } ;
14-
157const REGEX_HASH = new RegExp ( / \[ h a s h .* ] / g) ;
168
17- export function replaceHash ( str : string , buffer : Buffer | string ) {
9+ export function replaceHash ( str : string , buffer : Buffer ) {
1810 const match = str . match ( REGEX_HASH ) ;
19-
20- if ( match == null ) {
21- return str ;
22- }
23-
24- const [ _ , len ] = match [ 0 ] . replace ( / \[ | ] / g, "" ) . split ( ":" ) ;
11+ const [ _ , len ] = match ! [ 0 ] . replace ( / \[ | ] / g, "" ) . split ( ":" ) ;
2512
2613 return str . replace (
2714 REGEX_HASH ,
2815 hasha ( buffer ) . slice ( 0 , Number ( len ) || DEFAULT_HASH_LENGTH )
2916 ) ;
3017}
3118
32- function plugin ( options = DEFAULT_OPTIONS ) {
33- return function posthtmlHash ( tree : PostHTML . Node ) {
34- const hashLength = options . hashLength || DEFAULT_HASH_LENGTH ;
35- const css = options . css !== undefined ? options . css : DEFAULT_OPTIONS . css ;
36- const js = options . js !== undefined ? options . js : DEFAULT_OPTIONS . js ;
37- const nonEmptyString = new RegExp ( / \S + / ) ;
38- const matchers = ( [
39- css && {
40- tag : "link" ,
41- attrs : { rel : "stylesheet" , href : nonEmptyString } ,
42- } ,
43- js && { tag : "script" , attrs : { src : nonEmptyString } } ,
44- ] . filter ( Boolean ) as unknown ) as IPostHTMLHashMatcher [ ] ;
45-
46- tree . match ( matchers , ( node ) => {
47- const attrs = node . attrs ! ;
48- let fileName = "" ;
49-
50- if ( attrs . href ) {
51- fileName = attrs . href ;
52- } else if ( attrs . src ) {
53- fileName = attrs . src ;
54- }
55-
56- const pathToFile = options . path || "" ;
57- const file = path . join ( process . cwd ( ) , pathToFile , fileName ) ;
58-
59- if ( fs . existsSync ( file ) ) {
60- const buffer = fs . readFileSync ( file ) ;
61- const hashedFileName = replaceHash ( fileName , buffer ) ;
62- const hashedFile = path . join ( process . cwd ( ) , pathToFile , hashedFileName ) ;
19+ type NodeWithHashRegex = { attrs : { href ?: string ; src ?: string } } ;
6320
64- fs . renameSync ( file , hashedFile ) ;
21+ function plugin ( options ?: { path ?: string } ) {
22+ return function posthtmlHash ( tree : PostHTML . Node ) {
23+ tree . match (
24+ [ { attrs : { href : REGEX_HASH } } , { attrs : { src : REGEX_HASH } } ] ,
25+ ( node ) => {
26+ const _node = ( node as unknown ) as NodeWithHashRegex ;
27+ const { href, src } = _node . attrs ;
28+
29+ let fileName = "" ;
30+
31+ if ( href ) {
32+ fileName = href ;
33+ } else if ( src ) {
34+ fileName = src ;
35+ }
6536
66- if ( attrs . href ) {
67- node . attrs ! . href = hashedFileName ;
68- } else if ( attrs . src ) {
69- node . attrs ! . src = hashedFileName ;
37+ const pathToFile = options ?. path || "" ;
38+ const file = path . join ( process . cwd ( ) , pathToFile , fileName ) ;
39+
40+ if ( fs . existsSync ( file ) ) {
41+ const buffer = fs . readFileSync ( file ) ;
42+ const hashedFileName = replaceHash ( fileName , buffer ) ;
43+ const hashedFile = path . join (
44+ process . cwd ( ) ,
45+ pathToFile ,
46+ hashedFileName
47+ ) ;
48+
49+ fs . renameSync ( file , hashedFile ) ;
50+
51+ if ( href ) {
52+ _node . attrs . href = hashedFileName ;
53+ } else if ( src ) {
54+ _node . attrs . src = hashedFileName ;
55+ }
7056 }
71- }
7257
73- return node ;
74- } ) ;
58+ return node ;
59+ }
60+ ) ;
7561 } ;
7662}
7763
78- interface IOptions {
79- path ?: string ;
80- hashLength ?: number ;
81- css ?: boolean ;
82- js ?: boolean ;
83- }
84-
85- interface IPostHTMLHashMatcher {
86- tag : PostHTML . StringMatcher ;
87- attrs : PostHTML . AttrMatcher ;
88- }
89-
90- export { plugin , DEFAULT_HASH_LENGTH } ;
64+ export default plugin ;
65+ export { plugin as hash , plugin as posthtmlHash } ;
0 commit comments