Skip to content

Commit c4a49b1

Browse files
committed
feat: initial commit
0 parents  commit c4a49b1

File tree

12 files changed

+7875
-0
lines changed

12 files changed

+7875
-0
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 2
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
*.log
3+
.idea
4+
node_modules
5+
coverage
6+
.nyc_output

.remarkignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test/snapshots/**/*.md

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
language: node_js
2+
node_js:
3+
- '8'
4+
after_success:
5+
npm run coverage

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Nick Baugh <[email protected]> (http://niftylettuce.com/)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# manifest-rev
2+
3+
[![build status](https://semaphoreci.com/api/v1/ladjs/manifest-rev/branches/master/shields_badge.svg)](https://semaphoreci.com/ladjs/manifest-rev)
4+
[![code coverage](https://img.shields.io/codecov/c/github/ladjs/manifest-rev.svg)](https://codecov.io/gh/ladjs/manifest-rev)
5+
[![code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/sindresorhus/xo)
6+
[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg)](https://github.com/prettier/prettier)
7+
[![made with lass](https://img.shields.io/badge/made_with-lass-95CC28.svg)](https://github.com/lassjs/lass)
8+
[![license](https://img.shields.io/github/license/ladjs/manifest-rev.svg)](<>)
9+
10+
> Dynamically load assets into your views, emails, etc. from your `rev-manifest.json` manifest revision file (e.g. `<script src="{{ manifest('foo.js'); }}"></script>` would return `<script src="/foo-0775041dd4.js"></script>` when rendered).
11+
12+
13+
## Table of Contents
14+
15+
* [Install](#install)
16+
* [Usage](#usage)
17+
* [API](#api)
18+
* [Contributors](#contributors)
19+
* [License](#license)
20+
21+
22+
## Install
23+
24+
[npm][]:
25+
26+
```sh
27+
npm install manifest-rev
28+
```
29+
30+
[yarn][]:
31+
32+
```sh
33+
yarn add manifest-rev
34+
```
35+
36+
37+
## Usage
38+
39+
```js
40+
const path = require('path');
41+
42+
const Koa = require('koa');
43+
const manifestRev = require('manifest-rev');
44+
45+
const app = new Koa();
46+
47+
app.use((ctx, next) => {
48+
ctx.state.manifest = manifestRev({
49+
manifest: path.join(__dirname, 'build', 'rev-manifest.json'),
50+
prepend: '/'
51+
});
52+
return next();
53+
});
54+
55+
// ...
56+
```
57+
58+
2. Call the `manifest(str)` helper function in your views when you need to include assets (requires a templating engine).
59+
60+
> [pug][]:
61+
62+
```pug
63+
html
64+
head
65+
title Foo
66+
body
67+
h1 Foo
68+
script(src=manifest('foo.js'))
69+
```
70+
71+
> [ejs][]
72+
73+
```ejs
74+
<html>
75+
<head>
76+
<title>Foo</title>
77+
</head>
78+
<body>
79+
<h1>Foo</h1>
80+
<script src="<%= manifest('foo.js'); %>"></script>
81+
</body>
82+
</html>
83+
```
84+
85+
> [nunjucks][] (via [koa-nunjucks-promise][]):
86+
87+
```html
88+
<html>
89+
<head>
90+
<title>Foo</title>
91+
</head>
92+
<body>
93+
<h1>Foo</h1>
94+
<script src="{{ manifest('foo.js'); }}"></script>
95+
</body>
96+
</html>
97+
```
98+
99+
100+
## API
101+
102+
* `manifestRev(options)` - accepts a required `options` argument for setup. Returns middleware for use in `app.use` statement (which in turn binds to `ctx.state` a helper function called `manifest`). Here are the properties accepts in the `options` argument.
103+
104+
* `manifest` (**required**) - path to a valid `rev-manifest.json` file (e.g. as built by [gulp-rev][] or [gulp-rev-all][])
105+
* `prepend` (optional) - string to prepend before file paths rendered after lookup (e.g. if you type `{{ manifest('foo.js'); }}` in your view, and you have passed `prepend: '/dist/'` in your setup, then your tag would render as `<script src="/dist/foo-0775041dd4.js"></script>` (defaults to `/`)
106+
107+
* `manifest(str)` - the helper function returned when `manifestRev` is invoked in your app. Returns the string found from a lookup in your `rev-manifest.json` file for the `str` argument passed (e.g. if you type `{{ manifest('foo.js'); }}` in your view, then it returns for the value of the `foo.js` property as defined in your `manifest` file, such as `foo-0775041dd4.js`). If the found is not found, then the input `str` argument is returned.
108+
109+
110+
## Contributors
111+
112+
| Name | Website |
113+
| -------------- | -------------------------- |
114+
| **Nick Baugh** | <http://niftylettuce.com/> |
115+
116+
117+
## License
118+
119+
[MIT](LICENSE) © [Nick Baugh](http://niftylettuce.com/)
120+
121+
122+
##
123+
124+
[npm]: https://www.npmjs.com/
125+
126+
[yarn]: https://yarnpkg.com/
127+
128+
[koa-nunjucks-promise]: https://github.com/hanai/koa-nunjucks-promise
129+
130+
[gulp-rev-all]: https://github.com/smysnk/gulp-rev-all
131+
132+
[gulp-rev]: https://github.com/sindresorhus/gulp-rev
133+
134+
[nunjucks]: https://mozilla.github.io/nunjucks/
135+
136+
[pug]: https://github.com/pugjs/pug
137+
138+
[ejs]: http://ejs.co/

index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const PROD = process.env.NODE_ENV === 'production';
2+
3+
module.exports = function(opts) {
4+
if (typeof opts !== 'object') {
5+
throw new TypeError('`options` argument required');
6+
}
7+
8+
if (typeof opts.manifest !== 'string') {
9+
throw new TypeError('`manifest` property is required');
10+
}
11+
12+
if (typeof opts.prepend !== 'undefined' && typeof opts.prepend !== 'string') {
13+
throw new TypeError('`prepend` property defined, but it was not a string');
14+
}
15+
16+
opts.logger = opts.logger || console;
17+
opts.prepend = opts.prepend || '/';
18+
19+
let manifest = {};
20+
21+
try {
22+
manifest = require(opts.manifest);
23+
} catch (err) {
24+
opts.logger.error(err);
25+
}
26+
27+
return str => {
28+
let output = opts.prepend + str;
29+
try {
30+
if (!PROD) manifest = require(opts.manifest);
31+
output = opts.prepend + (manifest[str] || str);
32+
} catch (err) {
33+
opts.logger.error(err);
34+
}
35+
36+
return output;
37+
};
38+
};

package.json

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
{
2+
"name": "manifest-rev",
3+
"description": "Dynamically load assets into your views, emails, etc. from your `rev-manifest.json` manifest revision file",
4+
"version": "0.0.0",
5+
"author": "Nick Baugh <[email protected]> (http://niftylettuce.com/)",
6+
"bugs": {
7+
"url": "https://github.com/ladjs/manifest-rev/issues",
8+
"email": "[email protected]"
9+
},
10+
"commitlint": {
11+
"extends": [
12+
"@commitlint/config-conventional"
13+
]
14+
},
15+
"contributors": [
16+
"Nick Baugh <[email protected]> (http://niftylettuce.com/)"
17+
],
18+
"dependencies": {},
19+
"devDependencies": {
20+
"@commitlint/cli": "^8.2.0",
21+
"@commitlint/config-conventional": "^8.2.0",
22+
"ava": "^2.4.0",
23+
"codecov": "^3.5.0",
24+
"cross-env": "^5.2.1",
25+
"eslint": "^6.4.0",
26+
"eslint-config-xo-lass": "^1.0.3",
27+
"fixpack": "^2.3.1",
28+
"husky": "^3.0.5",
29+
"lint-staged": "^9.2.5",
30+
"nyc": "^14.1.1",
31+
"remark-cli": "^7.0.0",
32+
"remark-preset-github": "^0.0.16",
33+
"xo": "^0.24.0"
34+
},
35+
"engines": {
36+
"node": ">=8.x"
37+
},
38+
"homepage": "https://github.com/ladjs/manifest-rev",
39+
"husky": {
40+
"hooks": {
41+
"pre-commit": "lint-staged && npm test",
42+
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
43+
}
44+
},
45+
"keywords": [
46+
"asset",
47+
"bundle",
48+
"concat",
49+
"css",
50+
"dynamic",
51+
"ejs",
52+
"gulp",
53+
"gulp-rev",
54+
"gulp-rev-all",
55+
"jade",
56+
"js",
57+
"koa",
58+
"lass",
59+
"loader",
60+
"manifest",
61+
"manifest-rev",
62+
"minify",
63+
"nunjucks",
64+
"optimization",
65+
"optimize",
66+
"optipng",
67+
"pipeline",
68+
"render",
69+
"rev",
70+
"revision",
71+
"script",
72+
"uglify",
73+
"views"
74+
],
75+
"license": "MIT",
76+
"lint-staged": {
77+
"*.js": [
78+
"xo --fix",
79+
"git add"
80+
],
81+
"*.md": [
82+
"remark . -qfo",
83+
"git add"
84+
],
85+
"package.json": [
86+
"fixpack",
87+
"git add"
88+
]
89+
},
90+
"main": "index.js",
91+
"nyc": {
92+
"check-coverage": true,
93+
"lines": 100,
94+
"functions": 100,
95+
"branches": 90,
96+
"reporter": [
97+
"lcov",
98+
"html",
99+
"text"
100+
]
101+
},
102+
"prettier": {
103+
"singleQuote": true,
104+
"bracketSpacing": true,
105+
"trailingComma": "none"
106+
},
107+
"remarkConfig": {
108+
"plugins": [
109+
"preset-github"
110+
]
111+
},
112+
"repository": {
113+
"type": "git",
114+
"url": "https://github.com/ladjs/manifest-rev"
115+
},
116+
"scripts": {
117+
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov",
118+
"lint": "xo && remark . -qfo",
119+
"test": "npm run lint && npm run test-coverage",
120+
"test-coverage": "cross-env NODE_ENV=test nyc ava"
121+
},
122+
"xo": {
123+
"prettier": true,
124+
"space": true,
125+
"extends": [
126+
"xo-lass"
127+
]
128+
}
129+
}

test/fixtures/rev-manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)