image-webp-loader
Advanced tools
Comparing version 0.0.1 to 0.0.2
56
index.js
@@ -5,27 +5,39 @@ Object.defineProperty(exports, "__esModule", { | ||
exports.raw = true | ||
exports.default = loader | ||
var loaderUtils = require('loader-utils') | ||
var schemaUtils = require('schema-utils') | ||
var _loaderUtils2 = _interopRequireDefault(loaderUtils); | ||
const fs = require('fs') | ||
const {resolve} = require('path') | ||
const loaderUtils = require('loader-utils') | ||
const _loaderUtils = loaderUtils['__esModule'] ? loaderUtils : {default: loaderUtils} | ||
const imagemin = require('imagemin') | ||
const imageminWebp = require('imagemin-webp') | ||
function _interopRequireDefault(obj) { | ||
return obj && obj.__esModule ? obj : {default: obj}; | ||
} | ||
function loader(content) { | ||
var options = loaderUtils.getOptions(this) || {} | ||
// 获取hash | ||
console.log(loaderUtils.getHashDigest(content) + '-----------------') | ||
var url = _loaderUtils2.default.interpolateName(this, options.name, { | ||
content, | ||
regExp: options.regExp | ||
exports.default = function (content) { | ||
const options = loaderUtils.getOptions(this) || {} | ||
const hasHash = /\[hash.*?\]/.test(options.name) ? 1 : 0 | ||
const url = _loaderUtils.default.interpolateName(this, options.name, {content, regExp: options.regExp}) | ||
this.emitFile(url, content) | ||
this.targetFilePath = url | ||
const webpPath = options.name ? /[\//]/.test(options.name) ? url.match(/.*[\//]/)[0] : './' : './' | ||
imagemin([this.resourcePath], resolve(options.outputPath, webpPath), { | ||
use: [imageminWebp({quality: 90})] | ||
}).then(back => { | ||
if (hasHash) { | ||
let webpPath = back[0].path | ||
const hash = url.match(/\.[^.]+(?=\.(png|jpe?g))/i)[0].substr(1) | ||
let newPath = webpPath.replace(/\.webp$/g, "." + hash) + '.webp' | ||
fs.rename(webpPath, newPath, error => error && console.log(error)) | ||
} | ||
}) | ||
this.emitFile(url, content) | ||
return `module.exports = __webpack_public_path__ + ${JSON.stringify(url)}` | ||
let publicPath = url | ||
if (options.publicPath) { | ||
if (/\/$/.test(options.publicPath)) { | ||
publicPath = options.publicPath + publicPath | ||
} else { | ||
publicPath = options.publicPath + '/' + publicPath | ||
} | ||
} | ||
if (options.requestType) { | ||
publicPath = publicPath.replace(/\.(webp|png|jpe?g)/, '.' + options.requestType) | ||
} | ||
return `module.exports = __webpack_public_path__ + ${JSON.stringify(publicPath)};` | ||
} |
{ | ||
"name": "image-webp-loader", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "With webpack-loader to convert ordinary picture format to webp pictures, you can produce original pictures and webp pictures together and keep the same hash", | ||
"main": "index.js", | ||
"scripts": { | ||
"dev": "webpack --mode development" | ||
"test": "rimraf test/dist&&webpack --config test/webpack.config.js --mode development" | ||
}, | ||
@@ -13,4 +13,9 @@ "repository": { | ||
}, | ||
"author": "liujianhua", | ||
"author": "jianhualiu", | ||
"license": "MIT", | ||
"keywords": [ | ||
"webp", | ||
"webpack", | ||
"loader" | ||
], | ||
"bugs": { | ||
@@ -21,2 +26,4 @@ "url": "https://github.com/grajs/image-webp-loader/issues" | ||
"devDependencies": { | ||
"imagemin": "^5.3.1", | ||
"imagemin-webp": "^4.1.0", | ||
"webpack": "^4.7.0", | ||
@@ -23,0 +30,0 @@ "webpack-cli": "^2.1.3" |
Sorry, the diff of this file is not supported yet
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
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
14528
1
5
4
9
39
1