cache-hash
Advanced tools
Comparing version 1.0.0-beta.9 to 1.0.0-beta.10
@@ -7,3 +7,3 @@ /** | ||
import { optionsType } from './types'; | ||
declare const _default: (options: optionsType) => Promise<void>; | ||
declare const _default: (opts: optionsType) => Promise<void>; | ||
export = _default; |
@@ -30,3 +30,3 @@ "use strict"; | ||
const CWD = process.cwd(); | ||
const defualtOptions = { | ||
const defaultOptions = { | ||
target: CWD, | ||
@@ -36,3 +36,7 @@ output: CWD, | ||
versionKey: 'v', | ||
lazy: 'src', | ||
selectAll: [ | ||
['script', 'src'], | ||
['link', 'href'], | ||
['img', 'src'] | ||
], | ||
html: true, | ||
@@ -45,5 +49,6 @@ css: true, | ||
}; | ||
module.exports = function (options) { | ||
module.exports = function (opts) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const options = Object.assign(Object.assign({}, defaultOptions), opts); | ||
options.target = (0, path_1.isAbsolute)(options.target) ? options.target : (0, path_1.join)(CWD, options.target, '/'); | ||
@@ -54,3 +59,2 @@ options.output = (0, path_1.isAbsolute)(options.output) ? options.output : (0, path_1.join)(CWD, options.output, '/'); | ||
const start = process.hrtime(); | ||
options = Object.assign(defualtOptions, options); | ||
if (options.target !== options.output) | ||
@@ -57,0 +61,0 @@ (0, fs_extra_1.copySync)(options.target, options.output); |
@@ -17,22 +17,29 @@ "use strict"; | ||
return; | ||
const Selector = `script[src],link[href],img[${options.lazy || 'src'}]`; | ||
const Selector = []; | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
for (const [tag, attr] of options.selectAll) { | ||
Selector.push(`${tag}[${attr}]`); | ||
} | ||
for (const htmlFile of htmlFiles) { | ||
const content = (0, fs_1.readFileSync)(htmlFile).toString(); | ||
const dom = (0, htmlparser2_1.parseDocument)(content); | ||
const sources = (0, css_select_1.selectAll)(Selector, dom); | ||
const sources = (0, css_select_1.selectAll)(Selector.toString(), dom); | ||
for (const source of sources) { | ||
const attribs = { img: 'src', link: 'href', script: 'src' }; | ||
if (options.lazy) | ||
attribs.img = options.lazy; | ||
const name = source.name; | ||
let path = source.attribs[attribs[name]]; | ||
const pathParams = (0, searchParams_1.default)(path); | ||
path = path.replace(pathParams, ''); | ||
const sourcePath = (0, getAbsolutePath_1.default)(options.output, htmlFile, path); | ||
if ((0, fs_1.existsSync)(sourcePath) && (0, fs_1.statSync)(sourcePath).isFile()) { | ||
const data = (0, fs_1.readFileSync)(sourcePath); | ||
const sourceHash = (0, getHash_1.default)(data, options.size); | ||
const sourceHashPath = (0, setHash_1.default)(path + pathParams, options.versionKey, sourceHash); | ||
source.attribs[attribs[name]] = sourceHashPath; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
options | ||
.selectAll.filter(([tag, attr]) => tag === name && source.attribs[attr]) | ||
.forEach(([, attr]) => { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
let path = source.attribs[attr]; | ||
const pathParams = (0, searchParams_1.default)(path); | ||
path = path.replace(pathParams, ''); | ||
const sourcePath = (0, getAbsolutePath_1.default)(options.output, htmlFile, path); | ||
if ((0, fs_1.existsSync)(sourcePath) && (0, fs_1.statSync)(sourcePath).isFile()) { | ||
const data = (0, fs_1.readFileSync)(sourcePath); | ||
const sourceHash = (0, getHash_1.default)(data, options.size); | ||
const sourceHashPath = (0, setHash_1.default)(path + pathParams, options.versionKey, sourceHash); | ||
source.attribs[attr] = sourceHashPath; | ||
} | ||
}); | ||
} | ||
@@ -39,0 +46,0 @@ const result = (0, dom_serializer_1.default)(dom, { encodeEntities: 'utf8' }); |
@@ -9,3 +9,3 @@ export declare type KV = { | ||
versionKey?: string; | ||
lazy?: string; | ||
selectAll?: string[][]; | ||
html?: boolean; | ||
@@ -12,0 +12,0 @@ css?: boolean; |
{ | ||
"name": "cache-hash", | ||
"version": "1.0.0-beta.9", | ||
"version": "1.0.0-beta.10", | ||
"description": "Create hash for static resources to break http cache", | ||
@@ -5,0 +5,0 @@ "main": "dist/cache-hash.js", |
@@ -43,3 +43,3 @@ # Cache-hash | ||
--key <key> Version key (default: "v") | ||
--lazy <lazy> Image lazy loading (default: "src") | ||
--lazy <lazy> Select the specified dom element. Example: `'[["a","b"]]'` selects the img tag, and the img tag must contain the lazy attribute (default: [['script', 'src'], ['link', 'href'], ['img', 'src']]) | ||
--html <html> Generate hash for html referenced resources (default: true) | ||
@@ -70,3 +70,3 @@ --css <css> Generate hash for css referenced resources (default: true) | ||
// versionKey: 'v', | ||
// lazy: 'src', | ||
// selectAll: [ [ 'script', 'src' ], [ 'link', 'href' ], [ 'img', 'src' ] ], | ||
// html: true, | ||
@@ -73,0 +73,0 @@ // css: true, |
Sorry, the diff of this file is not supported yet
27486
484