mp-lens
Version:
微信小程序分析工具 (Unused Code, Dependencies, Visualization)
73 lines • 3.16 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getCanonicalBasePath = getCanonicalBasePath;
exports.generateNodeIdAndLabel = generateNodeIdAndLabel;
const path = __importStar(require("path"));
/**
* Normalizes a given absolute path by removing the file extension and any trailing '/index'.
* This creates a canonical base path for a component or page.
* e.g., /path/to/comp/index.wxml -> /path/to/comp
* e.g., /path/to/page.json -> /path/to/page
* @param absolutePath The absolute file path.
* @returns The canonical absolute base path.
*/
function getCanonicalBasePath(absolutePath) {
// Regex to remove common miniprogram extensions
let basePath = absolutePath.replace(/\.(wxml|wxss|js|ts|json)$/, '');
const indexSuffix = path.sep + 'index';
if (basePath.endsWith(indexSuffix)) {
basePath = basePath.slice(0, -indexSuffix.length);
}
return basePath;
}
/**
* Generates a canonical node ID and label from a file's absolute path.
* The ID is prefixed and relative to the miniapp root, ensuring uniqueness and consistency.
* @param type The type of the node ('Page' or 'Component').
* @param absolutePath The absolute path to one of the entity's files (e.g., .wxml, .json).
* @param miniappRoot The absolute path to the miniapp root directory.
* @returns An object containing the canonical id and label.
*/
function generateNodeIdAndLabel(type, absolutePath, miniappRoot) {
const canonicalBasePath = getCanonicalBasePath(absolutePath);
const relativePath = path.relative(miniappRoot, canonicalBasePath).replace(/\\/g, '/');
const prefix = type === 'Page' ? 'page:' : 'comp:';
return {
id: `${prefix}${relativePath}`,
label: relativePath,
};
}
//# sourceMappingURL=id-helper.js.map