babel-plugin-styled-components-px2vw
Version:
Babel plugin for convert px to vw units of styled-components
19 lines (18 loc) • 1.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.source = void 0;
var tslib_1 = require("tslib");
var types_1 = require("@babel/types");
var template_1 = tslib_1.__importDefault(require("@babel/template"));
exports.source = "\nfunction %%px2vw%%(%%input%%, ...args) {\n if (typeof %%input%% === 'function') return %%px2vw%%(%%input%%(...args), ...args);\n var value = typeof %%input%% === 'string' ? parseFloat(%%input%%) : typeof %%input%% === 'number' ? %%input%% : 0;\n var pixels = Number.isNaN(value) ? 0 : value;\n if (Math.abs(pixels) < %%minPixelValue%%) {\n return `${pixels}px`;\n }\n var unit = %%viewportUnit%%;\n var mul = Math.pow(10, %%unitPrecision%% + 1);\n return `${Math.round(Math.floor(pixels * 100 / %%viewportWidth%% * mul) / 10) * 10 / mul }${unit}`;\n}\n";
exports.default = (function (_px2vw, config) {
var template = template_1.default.statement(exports.source);
return template({
input: types_1.identifier('input'),
px2vw: _px2vw,
viewportWidth: types_1.numericLiteral(config.viewportWidth),
unitPrecision: types_1.numericLiteral(config.unitPrecision),
viewportUnit: types_1.stringLiteral(config.viewportUnit),
minPixelValue: types_1.numericLiteral(config.minPixelValue),
});
});