postcss-hash
Advanced tools
Comparing version
112
index.js
@@ -5,3 +5,2 @@ "use strict"; | ||
const { dirname, basename } = require("path"); | ||
const postcss = require("postcss"); | ||
const MapGenerator = require("postcss/lib/map-generator"); | ||
@@ -11,3 +10,3 @@ const utils = require("./utils"); | ||
module.exports = postcss.plugin("postcss-hash", opts => { | ||
module.exports = opts => { | ||
opts = Object.assign( | ||
@@ -24,57 +23,74 @@ { | ||
return function(root, result) { | ||
// replace filename | ||
const originalName = result.opts.to; | ||
result.opts.to = utils.rename(originalName, root.toString(), opts); | ||
return { | ||
postcssPlugin: "postcss-hash", | ||
OnceExit(root, { result, stringify }) { | ||
// replace filename | ||
const originalName = result.opts.to; | ||
result.opts.to = utils.rename(originalName, root.toString(), opts); | ||
// In order to get content addressable hash names, we need to generate | ||
// and hash the map file first, which gives us the ability to hash that, | ||
// then we can do a full map.generate() which will apply the sourceMappingURL | ||
// to the CSS file, allowing us to do a full hash of the CSS including | ||
// thes sourceMappingURL comment. | ||
if (opts.includeMap) { | ||
// Extract the stringifier | ||
let str = postcss.stringify; | ||
if (result.opts.syntax) str = result.opts.syntax.stringify; | ||
if (result.opts.stringifier) str = result.opts.stringifier; | ||
if (str.stringify) str = str.stringify; | ||
// In order to get content addressable hash names, we need to generate | ||
// and hash the map file first, which gives us the ability to hash that, | ||
// then we can do a full map.generate() which will apply the sourceMappingURL | ||
// to the CSS file, allowing us to do a full hash of the CSS including | ||
// thes sourceMappingURL comment. | ||
if (opts.includeMap) { | ||
// Extract the stringifier | ||
let str = stringify; | ||
if (result.opts.syntax) str = result.opts.syntax.stringify; | ||
if (result.opts.stringifier) str = result.opts.stringifier; | ||
if (str.stringify) str = str.stringify; | ||
// Generate the sourceMap contents | ||
const map = new MapGenerator(str, root, result.opts); | ||
map.generateString(); | ||
// Generate the sourceMap contents | ||
const map = new MapGenerator(str, root, result.opts); | ||
map.generateString(); | ||
const hash = utils.rename(originalName, map.map.toString(), opts); | ||
const hash = utils.rename( | ||
originalName, | ||
map.map.toString(), | ||
opts | ||
); | ||
// If the sourcemap annotation option is set, then we can name the sourcemap | ||
// based on the contents of its map, so change the option to be a string. | ||
if (result.opts.map) { | ||
result.opts.map.annotation = basename(`${hash}.map`); | ||
} | ||
// If the sourcemap annotation option is set, then we can name the sourcemap | ||
// based on the contents of its map, so change the option to be a string. | ||
if (result.opts.map) { | ||
result.opts.map.annotation = basename(`${hash}.map`); | ||
} | ||
// need to call map.generate() which applies the sourceMappingURL comment | ||
// to the CSS and returns it as res[0] | ||
const res = map.generate() | ||
// need to call map.generate() which applies the sourceMappingURL comment | ||
// to the CSS and returns it as res[0] | ||
const res = map.generate(); | ||
result.opts.to = utils.rename(originalName, res[0], opts); | ||
} else { | ||
result.opts.to = utils.rename(originalName, root.toString(), opts); | ||
} | ||
result.opts.to = utils.rename(originalName, res[0], opts); | ||
} else { | ||
result.opts.to = utils.rename( | ||
originalName, | ||
root.toString(), | ||
opts | ||
); | ||
} | ||
// create/update manifest.json | ||
const newData = utils.data(originalName, result.opts.to); | ||
// create/update manifest.json | ||
const newData = utils.data(originalName, result.opts.to); | ||
// You're probably thinking "Why not make all of the following async?!" | ||
// Well, using the async versions causes race conditions when this plugin | ||
// is called multiple times. Try switching to async versions and running the tests | ||
// and you'll see they fail | ||
mkdirp.sync(dirname(opts.manifest)) | ||
let oldData = {} | ||
try { | ||
oldData = JSON.parse(readFileSync(opts.manifest, "utf-8")); | ||
} catch (e) { | ||
oldData = {} | ||
// You're probably thinking "Why not make all of the following async?!" | ||
// Well, using the async versions causes race conditions when this plugin | ||
// is called multiple times. Try switching to async versions and running the tests | ||
// and you'll see they fail | ||
mkdirp.sync(dirname(opts.manifest)); | ||
let oldData = {}; | ||
try { | ||
oldData = JSON.parse(readFileSync(opts.manifest, "utf-8")); | ||
} catch (e) { | ||
oldData = {}; | ||
} | ||
const data = JSON.stringify( | ||
Object.assign(oldData, newData), | ||
null, | ||
2 | ||
); | ||
writeFileSync(opts.manifest, data, "utf-8"); | ||
} | ||
const data = JSON.stringify(Object.assign(oldData, newData), null, 2); | ||
writeFileSync(opts.manifest, data, "utf-8"); | ||
}; | ||
}); | ||
}; | ||
module.exports.postcss = true; |
{ | ||
"name": "postcss-hash", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "PostCSS plugin to replace output file names with HASH algorithms (md5, sha256, sha512, etc) and string length of your choice - for cache busting", | ||
@@ -37,4 +37,3 @@ "keywords": [ | ||
"dependencies": { | ||
"mkdirp": "^0.5.1", | ||
"postcss": "^7.0.2" | ||
"mkdirp": "^0.5.1" | ||
}, | ||
@@ -44,2 +43,3 @@ "devDependencies": { | ||
"jest": "^23.6.0", | ||
"postcss": "^8.3.0", | ||
"prettier": "^1.14.2" | ||
@@ -46,0 +46,0 @@ }, |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
10920
7.73%1
-50%7
16.67%124
13.76%4
33.33%1
Infinity%- Removed
- Removed
- Removed
- Removed