Ai
1 Star 0 Fork 0

zrw_git/style-vw-vh-loader

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
index.js 2.46 KB
一键复制 编辑 原始数据 按行查看 历史
zrw_git 提交于 2025-06-04 09:28 +08:00 . 逻辑调整解决转换问题
const loaderUtils = require('loader-utils');
const _propListMatcher = require('./prop-list-matcher.js');
const defaultopts = {
enabled: true,
matchTemplate: false,
viewport: 1920,
unit: 'vw',
fixed: 5,
propList: ['*'],
minPixelValue: 1,
}
const template = /<template>([\s\S]+)<\/template>/gi
const ZPXRegExp = /\b(\d+(\.\d+)?)px\b/;
const PROP_PXRegExp = /\b([a-zA-Z-]+)\s*:\s*(\d+(\.\d+)?)px\b/;
module.exports = function (source) {
let opts = loaderUtils.getOptions(this) || {};
let config = Object.assign({}, defaultopts, opts);
let pxGlobalRegExp = new RegExp(ZPXRegExp.source, 'g');
let propPxGlobalRegExp = new RegExp(PROP_PXRegExp.source, 'g');
let satisfyPropList = (0, _propListMatcher.createPropListMatcher)(config.propList)
if (this.cacheable) {
this.cacheable();
}
if (!config.enabled) {
return source
}
let callback = (match, $1) => {
if (!$1 || $1 < config.minPixelValue) return match
let val = $1 / config.viewport * 100
val = parseFloat(val.toFixed(config.fixed))
return val === 0 ? val : val + config.unit
}
let propCallback = (match, $1, $2) => {
if (satisfyPropList($1)) {
if (!$2 || $2 < config.minPixelValue) return match
let val = $2 / config.viewport * 100;
val = parseFloat(val.toFixed(config.fixed))
return `${$1}: ${val === 0 ? val : val + config.unit}`
}
return match
}
if (config.matchTemplate) {
let _source = ''
if (template.test(source)) {
_source = source.match(template)[0]
}
let t_source = source
if (propPxGlobalRegExp.test(_source)) {
let $_source = _source.replace(propPxGlobalRegExp, propCallback)
t_source = t_source.replace(template, $_source)
}
if (pxGlobalRegExp.test(_source)) {
let $_source = _source.replace(pxGlobalRegExp, callback)
t_source = t_source.replace(template, $_source)
}
return t_source
} else if (propPxGlobalRegExp.test(source) || pxGlobalRegExp.test(source)) {
let t_source = source
if (propPxGlobalRegExp.test(source)) {
t_source = t_source.replace(propPxGlobalRegExp, propCallback)
}
if (pxGlobalRegExp.test(source)) {
t_source = t_source.replace(pxGlobalRegExp, callback)
}
return t_source
} else {
return source
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zrw_git/style-vw-vh-loader.git
git@gitee.com:zrw_git/style-vw-vh-loader.git
zrw_git
style-vw-vh-loader
style-vw-vh-loader
master

搜索帮助