Skip to content

Commit 1a8575a

Browse files
committed
test: add tests
1 parent d0d9142 commit 1a8575a

11 files changed

Lines changed: 97 additions & 86 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
/.vscode
33
/lib
44
/node_modules
5+
/src/tests/__fixtures__/processed
56
yarn-debug.log*
67
yarn-error.log*

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"license": "MIT",
55
"description": "PostHTML plugin for hashing static assets",
66
"author": "Eric Liu (https://github.com/metonym)",
7-
"main": "lib/index.js",
8-
"types": "lib/index.d.ts",
7+
"main": "lib/plugin.js",
8+
"types": "lib/plugin.d.ts",
99
"scripts": {
1010
"test": "tsnd src/tests",
1111
"test:tdd": "tsnd --respawn src/tests",

src/index.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/plugin.ts

Lines changed: 42 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -3,88 +3,63 @@ import path from "path";
33
import { PostHTML } from "posthtml";
44
import hasha from "hasha";
55

6-
const DEFAULT_PATH = "";
76
const 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-
157
const REGEX_HASH = new RegExp(/\[hash.*]/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 };
File renamed without changes.
File renamed without changes.

src/tests/__fixtures__/processed/bundle.min.9a6cf95c41.css

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/tests/__fixtures__/processed/bundle.min.9a6cf95c41e87b9dc102.css

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/tests/__fixtures__/processed/bundle.min.b0dcc67ffc.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/tests/__fixtures__/processed/bundle.min.b0dcc67ffc1fd562f212.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)