Skip to content

Commit d0d9142

Browse files
committed
refactor: initial commit
1 parent c511acb commit d0d9142

15 files changed

Lines changed: 434 additions & 3638 deletions

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
.DS_Store
22
/.vscode
3-
/coverage
43
/lib
54
/node_modules
65
yarn-debug.log*

.travis.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
language: node_js
22
node_js: 8
33
cache: yarn
4-
after_success: npx codecov
4+
scripts:
5+
- yarn test
6+
- yarn prepack

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10-
- Add optional files field to options parameter that matches specific file names
10+
<!--
11+
## [1.0.0](https://github.com/metonym/posthtml-hash/releases/tag/v1.0.0) - 2020-05-09
1112
12-
- Incorporate the `[hash:number]` format to specify the hash length
13+
- Replace `jest` with Node.js native `assert` (removes ~250k sub-dependencies) -->
1314

1415
## [0.3.0](https://github.com/metonym/posthtml-hash/releases/tag/v0.3.0) - 2020-05-04
1516

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
[![NPM][npm]][npm-url]
44

5-
`posthtml-hash` is a [PostHTML](https://github.com/posthtml/posthtml) plugin for hashing static CSS/JS assets to enable caching. [hasha](https://www.npmjs.com/package/hasha) is used to generate hashes.
5+
`posthtml-hash` is a [PostHTML](https://github.com/posthtml/posthtml) plugin for hashing static CSS/JS assets to enable caching.
66

77
```diff
88
<html>
99
<head>
10-
- <link rel="stylesheet" href="stylesheet.css" />
10+
- <link rel="stylesheet" href="stylesheet.[hash].css" />
1111
+ <link rel="stylesheet" href="stylesheet.9a6cf95c41e87b9dc102.css" />
1212
</head>
1313
<body>
14-
- <script src="main.js"></script>
14+
- <script src="main.[hash].js"></script>
1515
+ <script src="main.b0dcc67ffc1fd562f212.js"></script>
1616
</body>
1717
</html>
@@ -22,7 +22,7 @@
2222
```bash
2323
yarn add -D posthtml-hash
2424
# OR
25-
npm i posthtml-hash
25+
npm i -D posthtml-hash
2626
```
2727

2828
## Usage

docs/v0.3.0.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# posthtml-hash <img align="right" width="220" height="200" title="PostHTML logo" src="http://posthtml.github.io/posthtml/logo.svg">
2+
3+
> Documentation for v0.3.0.
4+
5+
[![NPM][npm]][npm-url]
6+
7+
`posthtml-hash` is a [PostHTML](https://github.com/posthtml/posthtml) plugin for hashing static CSS/JS assets to enable caching. [hasha](https://www.npmjs.com/package/hasha) is used to generate hashes.
8+
9+
```diff
10+
<html>
11+
<head>
12+
- <link rel="stylesheet" href="stylesheet.css" />
13+
+ <link rel="stylesheet" href="stylesheet.9a6cf95c41e87b9dc102.css" />
14+
</head>
15+
<body>
16+
- <script src="main.js"></script>
17+
+ <script src="main.b0dcc67ffc1fd562f212.js"></script>
18+
</body>
19+
</html>
20+
```
21+
22+
## Install
23+
24+
```bash
25+
yarn add -D posthtml-hash
26+
# OR
27+
npm i posthtml-hash
28+
```
29+
30+
## Usage
31+
32+
```js
33+
const fs = require("fs");
34+
const posthtml = require("posthtml");
35+
const { hash } = require("posthtml-hash");
36+
37+
const html = fs.readFileSync("./index.html");
38+
39+
posthtml()
40+
.use(hash())
41+
.process(html)
42+
.then((result) => fs.writeFileSync("./index.html", result.html));
43+
```
44+
45+
## Options
46+
47+
This plugin assumes that the file to process is in the same directory as the posthtml script. If not, specify the relative path to the html file in `options.path`:
48+
49+
```js
50+
const fs = require("fs");
51+
const posthtml = require("posthtml");
52+
const { hash } = require("posthtml-hash");
53+
54+
const html = fs.readFileSync("./public/index.html");
55+
56+
posthtml()
57+
.use(
58+
hash({
59+
/**
60+
* Relative path to processed HTML file
61+
*/
62+
path: "public", // default: ""
63+
64+
/**
65+
* Length of hash
66+
*/
67+
hashLength: 10, // default: 20
68+
69+
/**
70+
* Hash CSS files
71+
*/
72+
css: true, // default: true
73+
74+
/**
75+
* Hash JS files
76+
*/
77+
js: true, // default: true
78+
})
79+
)
80+
.process(html)
81+
.then((result) => fs.writeFileSync("./index.html", result.html));
82+
```
83+
84+
## [Examples](examples)
85+
86+
## Contributing
87+
88+
See the [PostHTML Guidelines](https://github.com/posthtml/posthtml/tree/master/docs).
89+
90+
## [Changelog](CHANGELOG.md)
91+
92+
## License
93+
94+
[MIT](LICENSE)
95+
96+
[npm]: https://img.shields.io/npm/v/posthtml-hash.svg?color=blue
97+
[npm-url]: https://npmjs.com/package/posthtml-hash

package.json

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,19 @@
77
"main": "lib/index.js",
88
"types": "lib/index.d.ts",
99
"scripts": {
10-
"develop": "tsc -w",
11-
"test": "jest --coverage",
12-
"test:tdd": "jest --watch",
10+
"test": "tsnd src/tests",
11+
"test:tdd": "tsnd --respawn src/tests",
1312
"prepack": "tsc"
1413
},
1514
"dependencies": {
1615
"hasha": "^5.0.0",
1716
"posthtml": "^0.12.0"
1817
},
1918
"devDependencies": {
20-
"@types/jest": "^25.2.1",
21-
"jest": "^25.5.4",
22-
"ts-jest": "^25.4.0",
19+
"@types/node": "^13.13.5",
20+
"ts-node-dev": "^1.0.0-pre.44",
2321
"typescript": "^3.6.3"
2422
},
25-
"jest": {
26-
"preset": "ts-jest",
27-
"watchPathIgnorePatterns": [
28-
"<rootDir>/src/tests/__fixtures__"
29-
]
30-
},
3123
"repository": {
3224
"type": "git",
3325
"url": "https://github.com/metonym/posthtml-hash.git"

src/global.d.ts

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

src/plugin.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from "fs";
22
import path from "path";
33
import { PostHTML } from "posthtml";
4-
import { createHash, hashFileName, processFile } from "./utils";
4+
import hasha from "hasha";
55

66
const DEFAULT_PATH = "";
77
const DEFAULT_HASH_LENGTH = 20;
@@ -12,6 +12,23 @@ const DEFAULT_OPTIONS: IOptions = {
1212
js: true,
1313
};
1414

15+
const REGEX_HASH = new RegExp(/\[hash.*]/g);
16+
17+
export function replaceHash(str: string, buffer: Buffer | string) {
18+
const match = str.match(REGEX_HASH);
19+
20+
if (match == null) {
21+
return str;
22+
}
23+
24+
const [_, len] = match[0].replace(/\[|]/g, "").split(":");
25+
26+
return str.replace(
27+
REGEX_HASH,
28+
hasha(buffer).slice(0, Number(len) || DEFAULT_HASH_LENGTH)
29+
);
30+
}
31+
1532
function plugin(options = DEFAULT_OPTIONS) {
1633
return function posthtmlHash(tree: PostHTML.Node) {
1734
const hashLength = options.hashLength || DEFAULT_HASH_LENGTH;
@@ -39,10 +56,9 @@ function plugin(options = DEFAULT_OPTIONS) {
3956
const pathToFile = options.path || "";
4057
const file = path.join(process.cwd(), pathToFile, fileName);
4158

42-
processFile(file, () => {
59+
if (fs.existsSync(file)) {
4360
const buffer = fs.readFileSync(file);
44-
const hash = createHash(buffer, hashLength);
45-
const hashedFileName = hashFileName(fileName, hash);
61+
const hashedFileName = replaceHash(fileName, buffer);
4662
const hashedFile = path.join(process.cwd(), pathToFile, hashedFileName);
4763

4864
fs.renameSync(file, hashedFile);
@@ -52,7 +68,7 @@ function plugin(options = DEFAULT_OPTIONS) {
5268
} else if (attrs.src) {
5369
node.attrs!.src = hashedFileName;
5470
}
55-
});
71+
}
5672

5773
return node;
5874
});

src/tests/__snapshots__/plugin.spec.ts.snap

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

src/tests/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "./plugin.test";

0 commit comments

Comments
 (0)