postcss-hash
Advanced tools
Comparing version
37
index.js
"use strict"; | ||
const { readFileSync, writeFileSync } = require("fs"); | ||
const { dirname } = require("path"); | ||
const { dirname, basename } = require("path"); | ||
const postcss = require("postcss"); | ||
const MapGenerator = require("postcss/lib/map-generator"); | ||
const utils = require("./utils"); | ||
@@ -15,2 +16,3 @@ const mkdirp = require("mkdirp"); | ||
manifest: "./manifest.json", | ||
includeMap: false, | ||
name: utils.defaultName | ||
@@ -26,2 +28,35 @@ }, | ||
// 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; | ||
// Generate the sourceMap contents | ||
const map = new MapGenerator(str, root, result.opts); | ||
map.generateString(); | ||
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`); | ||
} | ||
// 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); | ||
} | ||
// create/update manifest.json | ||
@@ -28,0 +63,0 @@ const newData = utils.data(originalName, result.opts.to); |
{ | ||
"name": "postcss-hash", | ||
"version": "1.0.2", | ||
"version": "2.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", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -59,3 +59,6 @@ # PostCSS Hash [![Build Status][ci-img]][ci] | ||
### includeMap `(boolean, default: false)` | ||
Setting `includeMap` to `true` will allow postcss-hash to hash the name of the sourcemap, as well hash the CSS _including_ the `sourceMappingURL` comment. You can set this option to true if you care about the hashed fingerprints matching the contents of the CSS file, and don't mind a performance hit of regenerating the CSS twice. | ||
### trim `(number, default: 10)` | ||
@@ -62,0 +65,0 @@ Hash's length. |
10136
25.04%109
36.25%94
3.3%