Skip to content

Commit dbea2e4

Browse files
committed
Initial release
0 parents  commit dbea2e4

File tree

7 files changed

+760
-0
lines changed

7 files changed

+760
-0
lines changed

.gitignore

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

.npmignore

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

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) [2016] [Sébastien Nikolaou]
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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# laravel-elixir-sri
2+
3+
[![npm version](http://img.shields.io/npm/v/laravel-elixir-sri.svg)](https://npmjs.org/package/laravel-elixir-sri) [![npm license](http://img.shields.io/npm/l/laravel-elixir-sri.svg)](https://npmjs.org/package/laravel-elixir-sri)
4+
5+
Generate [Subresource Integrity (SRI)](https://www.w3.org/TR/SRI/) hashes in your Laravel Elixir asset pipeline.
6+
7+
## Install
8+
9+
```
10+
npm install laravel-elixir-sri --save-dev
11+
```
12+
13+
or
14+
15+
```
16+
yarn add laravel-elixir-sri --dev
17+
```
18+
19+
## Usage within Laravel Elixir
20+
21+
### Example Gulpfile:
22+
23+
```javascript
24+
var elixir = require('laravel-elixir');
25+
26+
require('laravel-elixir-sri');
27+
28+
elixir(function (mix) {
29+
mix.sri([
30+
'css/app.css',
31+
'js/app.js'
32+
]);
33+
});
34+
```
35+
36+
This will generate hashes for the given files stored in your **public folder** and save them to a `/public/sri.json` file.
37+
38+
### Changing the output folder:
39+
40+
You can specify a different output folder in the second argument:
41+
42+
```javascript
43+
// Save sri.json to /public/assets
44+
mix.sri('css/app.css', '/public/assets');
45+
```
46+
47+
### Parameters:
48+
49+
If you wish to customize more options, you can pass them as an object in the third argument:
50+
51+
```javascript
52+
// Use sha512 algorithm
53+
mix.sri('js/app.js', null, {
54+
algorithms: ['sha512']
55+
});
56+
```
57+
You can find all the available parameters on `gulp-sri`'s [documentation](https://www.npmjs.com/package/gulp-sri#parameters).
58+
59+
## Usage within Laravel views
60+
61+
First, you need to require the [laravel-sri]('https://github.com/sebdesign/laravel-sri') package in your `composer.json`.
62+
63+
To reference the generated hashes from the `sri.json` in your views, you may use the `integrity` helper function with the name of the file you are using in your `elixir` or `asset` function.
64+
65+
As a fallback, if the given file is not found in the `sri.json`, **it will generate the appropriate hashes on the fly** for your convenience. Read more on the [laravel-sri]('https://github.com/sebdesign/laravel-sri') repository.
66+
67+
```php
68+
{{-- Use with elixir() function --}}
69+
<link rel="stylesheet" href="{{ elixir('css/app.css') }}" integrity="{{ integrity('css/app.css') }}" crossorigin="anonymous">
70+
71+
{{-- Use with asset() function --}}
72+
<script src="{{ asset('js/app.js') }}" integrity="{{ integrity('js/app.js') }}" crossorigin="anonymous"></script>
73+
```

index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
var Elixir = require('laravel-elixir');
2+
var sri = require('gulp-sri');
3+
4+
Elixir.extend('sri', function (src, output, options) {
5+
var filename = options && options.fileName;
6+
7+
new Elixir.Task('sri', function () {
8+
this.recordStep('Generating hashes');
9+
10+
return (
11+
gulp.src(this.src.path, { base: Elixir.config.publicPath })
12+
.pipe(sri(options))
13+
.pipe(gulp.dest(this.output.baseDir))
14+
.pipe(new Elixir.Notification('SRI complete!'))
15+
);
16+
}, getPaths(src, output, filename));
17+
});
18+
19+
/**
20+
* Prep the Gulp src and output paths.
21+
*
22+
* @param {string|array} src
23+
* @param {string|null} output
24+
* @param {string|null} filename
25+
* @return {object}
26+
*/
27+
function getPaths(src, output, filename) {
28+
src = Array.isArray(src) ? src : [src];
29+
output = output || Elixir.config.publicPath;
30+
filename = filename || 'sri.json';
31+
32+
return new Elixir.GulpPaths()
33+
.src(src, Elixir.config.publicPath)
34+
.output(output, filename);
35+
}

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "laravel-elixir-sri",
3+
"version": "1.0.0",
4+
"description": "SRI hash generator for Laravel Elixir",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/sebdesign/laravel-elixir-sri.git"
12+
},
13+
"keywords": [
14+
"SRI",
15+
"security",
16+
"integrity",
17+
"laravel",
18+
"elixir",
19+
"gulp"
20+
],
21+
"author": "Sébastien Nikolaou <info@sebdesign.eu> (http://sebdesign.eu)",
22+
"license": "MIT",
23+
"bugs": {
24+
"url": "https://github.com/sebdesign/laravel-elixir-sri/issues"
25+
},
26+
"homepage": "https://github.com/sebdesign/laravel-elixir-sri",
27+
"dependencies": {
28+
"gulp-sri": "^0.3.0"
29+
}
30+
}

0 commit comments

Comments
 (0)