Skip to content

Instantly share code, notes, and snippets.

@valtism
Created January 25, 2022 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valtism/f5eff35edb827f0dccf39f8e9347e6ce to your computer and use it in GitHub Desktop.
Save valtism/f5eff35edb827f0dccf39f8e9347e6ce to your computer and use it in GitHub Desktop.
vite-plugin-sri patch
diff --git a/node_modules/@small-tech/vite-plugin-sri/index.js b/node_modules/@small-tech/vite-plugin-sri/index.js
index 841f438..e85b205 100644
--- a/node_modules/@small-tech/vite-plugin-sri/index.js
+++ b/node_modules/@small-tech/vite-plugin-sri/index.js
@@ -18,51 +18,56 @@
// License: ISC.
//
////////////////////////////////////////////////////////////////////////////////
+'use strict';
-import { createHash } from 'crypto'
-import cheerio from 'cheerio'
-import fetch from 'node-fetch'
+const crypto = require('crypto');
+const cheerio = require('cheerio');
+const fetch = require('node-fetch');
-export default function sri () {
+////////////////////////////////////////////////////////////////////////////////
+
+function sri () {
return {
name: 'vite-plugin-sri',
enforce: 'post',
apply: 'build',
async transformIndexHtml(html, context) {
- const bundle = context.bundle
+ const bundle = context.bundle;
const calculateIntegrityHashes = async (element) => {
- let source
- let attributeName = element.attribs.src ? 'src' : 'href'
- const resourcePath = element.attribs[attributeName]
+ let source;
+ let attributeName = element.attribs.src ? 'src' : 'href';
+ const resourcePath = element.attribs[attributeName];
if (resourcePath.startsWith('http')) {
// Load remote source from URL.
- source = await (await fetch(resourcePath)).buffer()
+ source = await (await fetch(resourcePath)).buffer();
} else {
// Load local source from bundle.
- const resourcePathWithoutLeadingSlash = element.attribs[attributeName].slice(1)
- const bundleItem = bundle[resourcePathWithoutLeadingSlash]
- source = bundleItem.code || bundleItem.source
+ const resourcePathWithoutLeadingSlash = element.attribs[attributeName].slice(1);
+ const bundleItem = bundle[resourcePathWithoutLeadingSlash];
+ source = bundleItem.code || bundleItem.source;
}
- element.attribs.integrity = `sha384-${createHash('sha384').update(source).digest().toString('base64')}`
- }
+ element.attribs.integrity = `sha384-${crypto.createHash('sha384').update(source).digest().toString('base64')}`;
+ };
- const $ = cheerio.load(html)
+ const $ = cheerio.load(html);
$.prototype.asyncForEach = async function (callback) {
for (let index = 0; index < this.length; index++) {
await callback(this[index], index, this);
}
- }
+ };
// Implement SRI for scripts and stylesheets.
- const scripts = $('script').filter('[src]')
- const stylesheets = $('link[rel=stylesheet]').filter('[href]')
+ const scripts = $('script').filter('[src]');
+ const stylesheets = $('link[rel=stylesheet]').filter('[href]');
- await scripts.asyncForEach(calculateIntegrityHashes)
- await stylesheets.asyncForEach(calculateIntegrityHashes)
+ await scripts.asyncForEach(calculateIntegrityHashes);
+ await stylesheets.asyncForEach(calculateIntegrityHashes);
return $.html()
}
}
}
+
+module.exports = sri;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment