|
| 1 | +# manifest-rev |
| 2 | + |
| 3 | +[](https://semaphoreci.com/ladjs/manifest-rev) |
| 4 | +[](https://codecov.io/gh/ladjs/manifest-rev) |
| 5 | +[](https://github.com/sindresorhus/xo) |
| 6 | +[](https://github.com/prettier/prettier) |
| 7 | +[](https://github.com/lassjs/lass) |
| 8 | +[](<>) |
| 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/ |
0 commit comments