laravel-elixir-sri

1.0.1 • Public • Published

laravel-elixir-sri

npm version npm license

Generate Subresource Integrity (SRI) hashes in your Laravel Elixir asset pipeline.

Install

npm install laravel-elixir-sri --save-dev

or

yarn add laravel-elixir-sri --dev

Usage within Laravel Elixir

Example Gulpfile:

var elixir = require('laravel-elixir');
 
require('laravel-elixir-sri');
 
elixir(function (mix) {
    mix.sri([
        'css/app.css',
        'js/app.js'
    ]);
});

This will generate hashes for the given files stored in your public folder and save them to a /public/sri.json file.

Changing the output folder:

You can specify a different output folder in the second argument:

// Save sri.json to /public/assets
mix.sri('css/app.css', '/public/assets');

Parameters:

If you wish to customize more options, you can pass them as an object in the third argument:

// Use sha512 algorithm
mix.sri('js/app.js', null, {
    algorithms: ['sha512']
});

You can find all the available parameters on gulp-sri's documentation.

Usage within Laravel views

First, you need to require the laravel-sri package in your composer.json.

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.

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 repository.

{{-- Use with elixir() function --}}
<link rel="stylesheet" href="{{ elixir('css/app.css') }}" integrity="{{ integrity('css/app.css') }}" crossorigin="anonymous">
 
{{-- Use with asset() function --}}
<script src="{{ asset('js/app.js') }}" integrity="{{ integrity('js/app.js') }}" crossorigin="anonymous"></script>

Credits

License

The MIT License (MIT). Please see License File for more information.

Dependencies (1)

Dev Dependencies (0)

    Package Sidebar

    Install

    npm i laravel-elixir-sri

    Weekly Downloads

    3

    Version

    1.0.1

    License

    MIT

    Last publish

    Collaborators

    • sebdesign