rollup-plugin-img
Advanced tools
Comparing version 1.0.5 to 1.0.6
'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var fs = require('fs'); | ||
var fs__default = _interopDefault(fs); | ||
var path = require('path'); | ||
var rollupPluginutils = require('rollup-pluginutils'); | ||
var crypto = _interopDefault(require('crypto')); | ||
function createCommonjsModule(fn, module) { | ||
return module = { exports: {} }, fn(module, module.exports), module.exports; | ||
} | ||
var index$1 = createCommonjsModule(function (module) { | ||
'use strict'; | ||
var isStream = module.exports = function (stream) { | ||
return stream !== null && typeof stream === 'object' && typeof stream.pipe === 'function'; | ||
}; | ||
isStream.writable = function (stream) { | ||
return isStream(stream) && stream.writable !== false && typeof stream._write === 'function' && typeof stream._writableState === 'object'; | ||
}; | ||
isStream.readable = function (stream) { | ||
return isStream(stream) && stream.readable !== false && typeof stream._read === 'function' && typeof stream._readableState === 'object'; | ||
}; | ||
isStream.duplex = function (stream) { | ||
return isStream.writable(stream) && isStream.readable(stream); | ||
}; | ||
isStream.transform = function (stream) { | ||
return isStream.duplex(stream) && typeof stream._transform === 'function' && typeof stream._transformState === 'object'; | ||
}; | ||
}); | ||
var hasha = function (input, opts) { | ||
opts = opts || {}; | ||
var outputEncoding = opts.encoding || 'hex'; | ||
if (outputEncoding === 'buffer') { | ||
outputEncoding = undefined; | ||
} | ||
var hash = crypto.createHash(opts.algorithm || 'sha512'); | ||
var update = function (buf) { | ||
var inputEncoding = typeof buf === 'string' ? 'utf8' : undefined; | ||
hash.update(buf, inputEncoding); | ||
}; | ||
if (Array.isArray(input)) { | ||
input.forEach(update); | ||
} else { | ||
update(input); | ||
} | ||
return hash.digest(outputEncoding); | ||
}; | ||
hasha.stream = function (opts) { | ||
opts = opts || {}; | ||
var outputEncoding = opts.encoding || 'hex'; | ||
if (outputEncoding === 'buffer') { | ||
outputEncoding = undefined; | ||
} | ||
var stream = crypto.createHash(opts.algorithm || 'sha512'); | ||
stream.setEncoding(outputEncoding); | ||
return stream; | ||
}; | ||
hasha.fromStream = function (stream, opts) { | ||
if (!index$1(stream)) { | ||
return Promise.reject(new TypeError('Expected a stream')); | ||
} | ||
opts = opts || {}; | ||
return new Promise(function (resolve, reject) { | ||
stream | ||
.on('error', reject) | ||
.pipe(hasha.stream(opts)) | ||
.on('error', reject) | ||
.on('finish', function () { | ||
resolve(this.read()); | ||
}); | ||
}); | ||
}; | ||
hasha.fromFile = function (fp, opts) { return hasha.fromStream(fs__default.createReadStream(fp), opts); }; | ||
hasha.fromFileSync = function (fp, opts) { return hasha(fs__default.readFileSync(fp), opts); }; | ||
var index = hasha; | ||
var mimeMap = { | ||
@@ -41,3 +136,10 @@ '.jpg': 'image/jpeg', | ||
} | ||
var outputFile = output + "/" + (path.basename(id)); | ||
var name = path.basename(id); | ||
if (opt.hash) { | ||
var code = fs.readFileSync(id).toString(); | ||
var hash = index(code, { algorithm: 'md5' }); | ||
name = (path.basename(id, ext)) + "-" + hash + ext; | ||
} | ||
var outputFile = output + "/" + name; | ||
var baseIndex = outputFile.indexOf('/'); | ||
@@ -44,0 +146,0 @@ baseIndex = baseIndex !== -1 ? baseIndex + 1 : 0; |
@@ -1,5 +0,97 @@ | ||
import { createReadStream, createWriteStream, existsSync, mkdirSync, readFileSync, statSync } from 'fs'; | ||
import fs, { createReadStream, createWriteStream, existsSync, mkdirSync, readFileSync, statSync } from 'fs'; | ||
import { basename, extname, relative } from 'path'; | ||
import { createFilter } from 'rollup-pluginutils'; | ||
import crypto from 'crypto'; | ||
function createCommonjsModule(fn, module) { | ||
return module = { exports: {} }, fn(module, module.exports), module.exports; | ||
} | ||
var index$1 = createCommonjsModule(function (module) { | ||
'use strict'; | ||
var isStream = module.exports = function (stream) { | ||
return stream !== null && typeof stream === 'object' && typeof stream.pipe === 'function'; | ||
}; | ||
isStream.writable = function (stream) { | ||
return isStream(stream) && stream.writable !== false && typeof stream._write === 'function' && typeof stream._writableState === 'object'; | ||
}; | ||
isStream.readable = function (stream) { | ||
return isStream(stream) && stream.readable !== false && typeof stream._read === 'function' && typeof stream._readableState === 'object'; | ||
}; | ||
isStream.duplex = function (stream) { | ||
return isStream.writable(stream) && isStream.readable(stream); | ||
}; | ||
isStream.transform = function (stream) { | ||
return isStream.duplex(stream) && typeof stream._transform === 'function' && typeof stream._transformState === 'object'; | ||
}; | ||
}); | ||
var hasha = function (input, opts) { | ||
opts = opts || {}; | ||
var outputEncoding = opts.encoding || 'hex'; | ||
if (outputEncoding === 'buffer') { | ||
outputEncoding = undefined; | ||
} | ||
var hash = crypto.createHash(opts.algorithm || 'sha512'); | ||
var update = function (buf) { | ||
var inputEncoding = typeof buf === 'string' ? 'utf8' : undefined; | ||
hash.update(buf, inputEncoding); | ||
}; | ||
if (Array.isArray(input)) { | ||
input.forEach(update); | ||
} else { | ||
update(input); | ||
} | ||
return hash.digest(outputEncoding); | ||
}; | ||
hasha.stream = function (opts) { | ||
opts = opts || {}; | ||
var outputEncoding = opts.encoding || 'hex'; | ||
if (outputEncoding === 'buffer') { | ||
outputEncoding = undefined; | ||
} | ||
var stream = crypto.createHash(opts.algorithm || 'sha512'); | ||
stream.setEncoding(outputEncoding); | ||
return stream; | ||
}; | ||
hasha.fromStream = function (stream, opts) { | ||
if (!index$1(stream)) { | ||
return Promise.reject(new TypeError('Expected a stream')); | ||
} | ||
opts = opts || {}; | ||
return new Promise(function (resolve, reject) { | ||
stream | ||
.on('error', reject) | ||
.pipe(hasha.stream(opts)) | ||
.on('error', reject) | ||
.on('finish', function () { | ||
resolve(this.read()); | ||
}); | ||
}); | ||
}; | ||
hasha.fromFile = function (fp, opts) { return hasha.fromStream(fs.createReadStream(fp), opts); }; | ||
hasha.fromFileSync = function (fp, opts) { return hasha(fs.readFileSync(fp), opts); }; | ||
var index = hasha; | ||
var mimeMap = { | ||
@@ -39,3 +131,10 @@ '.jpg': 'image/jpeg', | ||
} | ||
var outputFile = output + "/" + (basename(id)); | ||
var name = basename(id); | ||
if (opt.hash) { | ||
var code = readFileSync(id).toString(); | ||
var hash = index(code, { algorithm: 'md5' }); | ||
name = (basename(id, ext)) + "-" + hash + ext; | ||
} | ||
var outputFile = output + "/" + name; | ||
var baseIndex = outputFile.indexOf('/'); | ||
@@ -42,0 +141,0 @@ baseIndex = baseIndex !== -1 ? baseIndex + 1 : 0; |
{ | ||
"name": "rollup-plugin-img", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "import image files with rollup", | ||
@@ -11,3 +11,7 @@ "main": "dist/index.cjs.js", | ||
"license": "MIT", | ||
"files": [ "src", "dist", "README.md" ], | ||
"files": [ | ||
"src", | ||
"dist", | ||
"README.md" | ||
], | ||
"keywords": [ | ||
@@ -25,4 +29,7 @@ "rollup", | ||
"eslint": "^3.19.0", | ||
"hasha": "^3.0.0", | ||
"rollup": "^0.41.6", | ||
"rollup-plugin-buble": "^0.15.0" | ||
"rollup-plugin-buble": "^0.15.0", | ||
"rollup-plugin-commonjs": "^8.0.2", | ||
"rollup-plugin-node-resolve": "^3.0.0" | ||
}, | ||
@@ -29,0 +36,0 @@ "dependencies": { |
@@ -48,7 +48,6 @@ # rollup-plugin-img | ||
- exclude & include: like other rollup plugins. [Details](https://github.com/rollup/rollup/wiki/Plugins) | ||
- output: the dest path of output image files. | ||
- extensions: a regular expression for the extensions of image files. | ||
- limit: the limit(byte) of the file size. A file will be transformed into base64 string when it doesn't | ||
exceeded the limit, otherwise, it will be copyed to the dest path. | ||
- exclude & include: Optional. like other rollup plugins. [Details](https://github.com/rollup/rollup/wiki/Plugins) | ||
- output: Required. the dest path of output image files. The first directory of dest will be handled as the base output directory(where the html file will be, usually). | ||
- extensions: Optional. a regular expression for the extensions of image files. | ||
- limit: Optional. the limit(byte) of the file size. A file will be transformed into base64 string when it doesn't exceeded the limit, otherwise, it will be copyed to the dest path. | ||
@@ -55,0 +54,0 @@ demo: |
import { statSync, readFileSync, createReadStream, createWriteStream, existsSync, mkdirSync } from 'fs'; | ||
import { extname, basename, relative } from 'path'; | ||
import { createFilter } from 'rollup-pluginutils'; | ||
import hasha from 'hasha'; | ||
@@ -37,3 +38,10 @@ const mimeMap = { | ||
} | ||
const outputFile = `${output}/${basename(id)}`; | ||
let name = basename(id); | ||
if (opt.hash) { | ||
const code = readFileSync(id).toString(); | ||
const hash = hasha(code, { algorithm: 'md5' }); | ||
name = `${basename(id, ext)}-${hash}${ext}`; | ||
} | ||
const outputFile = `${output}/${name}`; | ||
let baseIndex = outputFile.indexOf('/'); | ||
@@ -40,0 +48,0 @@ baseIndex = baseIndex !== -1 ? baseIndex + 1 : 0; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13843
283
6
70