UNPKG

4.69 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 var desc = Object.getOwnPropertyDescriptor(m, k);
5 if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6 desc = { enumerable: true, get: function() { return m[k]; } };
7 }
8 Object.defineProperty(o, k2, desc);
9}) : (function(o, m, k, k2) {
10 if (k2 === undefined) k2 = k;
11 o[k2] = m[k];
12}));
13var __exportStar = (this && this.__exportStar) || function(m, exports) {
14 for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15};
16Object.defineProperty(exports, "__esModule", { value: true });
17exports.parseHtmlScript = exports.parsePublicPath = exports.formatOpenHost = exports.addHtmlSuffix = exports.stripTrailingSlash = exports.stripBasename = exports.hasBasename = exports.addTrailingSlash = exports.addLeadingSlash = exports.getRootPath = exports.emptyTogglableObj = void 0;
18const os_1 = require("os");
19const path = require("path");
20exports.emptyTogglableObj = {
21 enable: false,
22 config: {}
23};
24const getRootPath = function () {
25 return path.resolve(__dirname, '../../');
26};
27exports.getRootPath = getRootPath;
28const addLeadingSlash = (url = '') => (url.charAt(0) === '/' ? url : '/' + url);
29exports.addLeadingSlash = addLeadingSlash;
30const addTrailingSlash = (url = '') => (url.charAt(url.length - 1) === '/' ? url : url + '/');
31exports.addTrailingSlash = addTrailingSlash;
32const hasBasename = (path = '', prefix = '') => new RegExp('^' + prefix + '(\\/|\\?|#|$)', 'i').test(path) || path === prefix;
33exports.hasBasename = hasBasename;
34const stripBasename = (path = '', prefix = '') => (0, exports.hasBasename)(path, prefix) ? path.substr(prefix.length) : path;
35exports.stripBasename = stripBasename;
36const stripTrailingSlash = (path = '') => (path.charAt(path.length - 1) === '/' ? path.substring(0, path.length - 1) : path);
37exports.stripTrailingSlash = stripTrailingSlash;
38const addHtmlSuffix = (path = '') => `${path}.html`;
39exports.addHtmlSuffix = addHtmlSuffix;
40const formatOpenHost = host => {
41 var _a;
42 let result = host;
43 // 配置host为0.0.0.0时,可以转换为ip打开, 其他以配置host默认打开
44 if (result === '0.0.0.0') {
45 // 设置localhost为初值, 防止没正确获取到host时以0.0.0.0打开
46 result = 'localhost';
47 const interfaces = (0, os_1.networkInterfaces)();
48 for (const devName in interfaces) {
49 const isEnd = (_a = interfaces[devName]) === null || _a === void 0 ? void 0 : _a.some(item => {
50 // 取IPv4, 不为127.0.0.1的内网ip
51 if (['IPv4', 4, '4'].includes(item.family) && item.address !== '127.0.0.1' && !item.internal) {
52 result = item.address;
53 return true;
54 }
55 return false;
56 });
57 // 若获取到ip, 结束遍历
58 if (isEnd) {
59 break;
60 }
61 }
62 }
63 return result;
64};
65exports.formatOpenHost = formatOpenHost;
66function parsePublicPath(publicPath = '/') {
67 return ['', 'auto'].includes(publicPath) ? publicPath : (0, exports.addTrailingSlash)(publicPath);
68}
69exports.parsePublicPath = parsePublicPath;
70function parseHtmlScript(pxtransformOption = {}) {
71 var _a, _b, _c;
72 const options = (pxtransformOption === null || pxtransformOption === void 0 ? void 0 : pxtransformOption.config) || {};
73 const max = (_a = options === null || options === void 0 ? void 0 : options.maxRootSize) !== null && _a !== void 0 ? _a : 40;
74 const min = (_b = options === null || options === void 0 ? void 0 : options.minRootSize) !== null && _b !== void 0 ? _b : 20;
75 const baseFontSize = (options === null || options === void 0 ? void 0 : options.baseFontSize) || (min > 1 ? min : 20);
76 const designWidth = (input => typeof options.designWidth === 'function'
77 ? options.designWidth(input)
78 : options.designWidth)(baseFontSize);
79 const rootValue = baseFontSize / options.deviceRatio[designWidth] * 2;
80 if (((_c = options === null || options === void 0 ? void 0 : options.targetUnit) !== null && _c !== void 0 ? _c : 'rem') === 'rem') {
81 return `!function(n){function f(){var e=n.document.documentElement,w=e.getBoundingClientRect().width,x=${rootValue}*w/${designWidth};e.style.fontSize=x>=${max}?"${max}px":x<=${min}?"${min}px":x+"px"}n.addEventListener("resize",(function(){f()})),f()}(window);`;
82 }
83}
84exports.parseHtmlScript = parseHtmlScript;
85__exportStar(require("./app"), exports);
86//# sourceMappingURL=index.js.map
\No newline at end of file