完善生产查询与登录权限配置
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
var __create = Object.create;
|
||||
var __defProp = Object.defineProperty;
|
||||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||
var __getProtoOf = Object.getPrototypeOf;
|
||||
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
||||
var __export = (target, all) => {
|
||||
for (var name in all)
|
||||
__defProp(target, name, { get: all[name], enumerable: true });
|
||||
};
|
||||
var __copyProps = (to, from, except, desc) => {
|
||||
if (from && typeof from === "object" || typeof from === "function") {
|
||||
for (let key of __getOwnPropNames(from))
|
||||
if (!__hasOwnProp.call(to, key) && key !== except)
|
||||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
||||
}
|
||||
return to;
|
||||
};
|
||||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
||||
// If the importer is in node compatibility mode or this is not an ESM
|
||||
// file that has been converted to a CommonJS file using a Babel-
|
||||
// compatible transform (i.e. "__esModule" has not been set), then set
|
||||
// "default" to the CommonJS "module.exports" for node compatibility.
|
||||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
||||
mod
|
||||
));
|
||||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
||||
var root_exports = {};
|
||||
__export(root_exports, {
|
||||
UniRoot: () => UniRoot,
|
||||
default: () => root_default
|
||||
});
|
||||
module.exports = __toCommonJS(root_exports);
|
||||
var import_node_path = require("node:path");
|
||||
var import_node_process = __toESM(require("node:process"));
|
||||
var import_node_fs = require("node:fs");
|
||||
var import_vite = require("vite");
|
||||
var import_root = require("./root.cjs");
|
||||
var import_page = require("./page.cjs");
|
||||
var import_utils = require("./utils.cjs");
|
||||
function normalizeId(id) {
|
||||
return (0, import_vite.normalizePath)(id).replace(/^[A-Z]:/, (match) => match.toLowerCase());
|
||||
}
|
||||
function UniRoot(options) {
|
||||
options = {
|
||||
rootFileName: "App.root",
|
||||
...options
|
||||
};
|
||||
const rootPath = import_node_process.default.env.UNI_INPUT_DIR || (0, import_node_path.join)(import_node_process.default.cwd(), "src");
|
||||
const appRootPath = (0, import_node_path.resolve)(rootPath, `${options.rootFileName}.vue`);
|
||||
const pagesPath = (0, import_node_path.resolve)(rootPath, "pages.json");
|
||||
let pagesJson = (0, import_utils.loadPagesJson)(pagesPath, rootPath);
|
||||
let watcher = null;
|
||||
let hasPlatformPlugin = false;
|
||||
const normalizedAppRoot = normalizeId(appRootPath);
|
||||
return {
|
||||
name: "vite-plugin-uni-root",
|
||||
enforce: "pre",
|
||||
// 在其他插件之前执行,确保页面模板先被包裹
|
||||
configResolved({ plugins }) {
|
||||
hasPlatformPlugin = plugins.some((v) => v.name === "vite-plugin-uni-platform");
|
||||
},
|
||||
configureServer(server) {
|
||||
const reload = (0, import_utils.debounce)(() => {
|
||||
try {
|
||||
pagesJson = (0, import_utils.loadPagesJson)(pagesPath, rootPath);
|
||||
server.ws.send({ type: "full-reload" });
|
||||
} catch (e) {
|
||||
console.error("[vite-plugin-uni-root] pages.json reload failed:", e);
|
||||
}
|
||||
}, 100);
|
||||
watcher = (0, import_node_fs.watch)(pagesPath, (eventType) => {
|
||||
if (eventType === "change")
|
||||
reload();
|
||||
});
|
||||
},
|
||||
load(id) {
|
||||
const normalizedId = normalizeId(id);
|
||||
if (normalizedId === normalizedAppRoot) {
|
||||
const code = (0, import_node_fs.readFileSync)(appRootPath, "utf-8");
|
||||
return (0, import_root.rebuildRootApp)(code).code;
|
||||
}
|
||||
},
|
||||
async transform(code, id) {
|
||||
const normalizedId = normalizeId(id);
|
||||
const normalizedRootPath = normalizeId(rootPath);
|
||||
if (normalizedId === `${normalizedRootPath}/main.ts` || normalizedId === `${normalizedRootPath}/main.js`) {
|
||||
return (0, import_root.registerRootApp)(code, options.rootFileName);
|
||||
}
|
||||
if (normalizedId === normalizedAppRoot) {
|
||||
return (0, import_root.rebuildRootApp)(code);
|
||||
}
|
||||
const pageId = hasPlatformPlugin ? (0, import_utils.normalizePlatformPath)(normalizedId) : normalizedId;
|
||||
const isPage = pagesJson.some((p) => pageId === p || pageId.startsWith(`${p}?`));
|
||||
if (isPage) {
|
||||
return (0, import_page.transformPage)(code);
|
||||
}
|
||||
},
|
||||
buildEnd() {
|
||||
if (watcher) {
|
||||
watcher.close();
|
||||
watcher = null;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
var root_default = UniRoot;
|
||||
// Annotate the CommonJS export names for ESM import in node:
|
||||
0 && (module.exports = {
|
||||
UniRoot
|
||||
});
|
||||
@@ -0,0 +1,78 @@
|
||||
import { resolve, join } from "node:path";
|
||||
import process from "node:process";
|
||||
import { watch, readFileSync } from "node:fs";
|
||||
import { normalizePath } from "vite";
|
||||
import { registerRootApp, rebuildRootApp } from "./root.mjs";
|
||||
import { transformPage } from "./page.mjs";
|
||||
import { loadPagesJson, normalizePlatformPath, debounce } from "./utils.mjs";
|
||||
function normalizeId(id) {
|
||||
return normalizePath(id).replace(/^[A-Z]:/, (match) => match.toLowerCase());
|
||||
}
|
||||
function UniRoot(options) {
|
||||
options = {
|
||||
rootFileName: "App.root",
|
||||
...options
|
||||
};
|
||||
const rootPath = process.env.UNI_INPUT_DIR || join(process.cwd(), "src");
|
||||
const appRootPath = resolve(rootPath, `${options.rootFileName}.vue`);
|
||||
const pagesPath = resolve(rootPath, "pages.json");
|
||||
let pagesJson = loadPagesJson(pagesPath, rootPath);
|
||||
let watcher = null;
|
||||
let hasPlatformPlugin = false;
|
||||
const normalizedAppRoot = normalizeId(appRootPath);
|
||||
return {
|
||||
name: "vite-plugin-uni-root",
|
||||
enforce: "pre",
|
||||
// 在其他插件之前执行,确保页面模板先被包裹
|
||||
configResolved({ plugins }) {
|
||||
hasPlatformPlugin = plugins.some((v) => v.name === "vite-plugin-uni-platform");
|
||||
},
|
||||
configureServer(server) {
|
||||
const reload = debounce(() => {
|
||||
try {
|
||||
pagesJson = loadPagesJson(pagesPath, rootPath);
|
||||
server.ws.send({ type: "full-reload" });
|
||||
} catch (e) {
|
||||
console.error("[vite-plugin-uni-root] pages.json reload failed:", e);
|
||||
}
|
||||
}, 100);
|
||||
watcher = watch(pagesPath, (eventType) => {
|
||||
if (eventType === "change")
|
||||
reload();
|
||||
});
|
||||
},
|
||||
load(id) {
|
||||
const normalizedId = normalizeId(id);
|
||||
if (normalizedId === normalizedAppRoot) {
|
||||
const code = readFileSync(appRootPath, "utf-8");
|
||||
return rebuildRootApp(code).code;
|
||||
}
|
||||
},
|
||||
async transform(code, id) {
|
||||
const normalizedId = normalizeId(id);
|
||||
const normalizedRootPath = normalizeId(rootPath);
|
||||
if (normalizedId === `${normalizedRootPath}/main.ts` || normalizedId === `${normalizedRootPath}/main.js`) {
|
||||
return registerRootApp(code, options.rootFileName);
|
||||
}
|
||||
if (normalizedId === normalizedAppRoot) {
|
||||
return rebuildRootApp(code);
|
||||
}
|
||||
const pageId = hasPlatformPlugin ? normalizePlatformPath(normalizedId) : normalizedId;
|
||||
const isPage = pagesJson.some((p) => pageId === p || pageId.startsWith(`${p}?`));
|
||||
if (isPage) {
|
||||
return transformPage(code);
|
||||
}
|
||||
},
|
||||
buildEnd() {
|
||||
if (watcher) {
|
||||
watcher.close();
|
||||
watcher = null;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
var root_default = UniRoot;
|
||||
export {
|
||||
UniRoot,
|
||||
root_default as default
|
||||
};
|
||||
@@ -0,0 +1,76 @@
|
||||
var __defProp = Object.defineProperty;
|
||||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
||||
var __export = (target, all) => {
|
||||
for (var name in all)
|
||||
__defProp(target, name, { get: all[name], enumerable: true });
|
||||
};
|
||||
var __copyProps = (to, from, except, desc) => {
|
||||
if (from && typeof from === "object" || typeof from === "function") {
|
||||
for (let key of __getOwnPropNames(from))
|
||||
if (!__hasOwnProp.call(to, key) && key !== except)
|
||||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
||||
}
|
||||
return to;
|
||||
};
|
||||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
||||
var page_exports = {};
|
||||
__export(page_exports, {
|
||||
transformPage: () => transformPage
|
||||
});
|
||||
module.exports = __toCommonJS(page_exports);
|
||||
var import_utils = require("./utils.cjs");
|
||||
async function transformPage(code) {
|
||||
const sfc = await (0, import_utils.parseSFC)(code);
|
||||
const openMatch = code.match(/<template\b[^>]*>/);
|
||||
if (!openMatch)
|
||||
return null;
|
||||
const closeTagStart = code.lastIndexOf("</template>");
|
||||
if (closeTagStart === -1)
|
||||
return null;
|
||||
const openTagEnd = openMatch.index + openMatch[0].length;
|
||||
const content = code.slice(openTagEnd, closeTagStart);
|
||||
let pageMetaSource = "";
|
||||
let newContent = content;
|
||||
if (sfc == null ? void 0 : sfc.template) {
|
||||
const pageMetaNode = (0, import_utils.findNode)(sfc, "PageMeta");
|
||||
if (pageMetaNode) {
|
||||
pageMetaSource = pageMetaNode.loc.source;
|
||||
const metaStart = pageMetaNode.loc.start.offset;
|
||||
const metaEnd = pageMetaNode.loc.end.offset;
|
||||
const metaStartInContent = metaStart - openTagEnd;
|
||||
const metaEndInContent = metaEnd - openTagEnd;
|
||||
newContent = content.slice(0, metaStartInContent) + content.slice(metaEndInContent);
|
||||
}
|
||||
} else {
|
||||
const pageMetaMatch = content.match(/<page-meta\b[^>]*>[\s\S]*?<\/page-meta>/);
|
||||
if (pageMetaMatch) {
|
||||
pageMetaSource = pageMetaMatch[0];
|
||||
newContent = content.replace(pageMetaMatch[0], "");
|
||||
} else {
|
||||
const selfClosingMatch = content.match(/<page-meta\b[^>]*\/>/);
|
||||
if (selfClosingMatch) {
|
||||
pageMetaSource = selfClosingMatch[0];
|
||||
newContent = content.replace(selfClosingMatch[0], "");
|
||||
}
|
||||
}
|
||||
}
|
||||
const wrappedContent = `
|
||||
${pageMetaSource}
|
||||
<global-root-view>${newContent}</global-root-view>
|
||||
`;
|
||||
return {
|
||||
code: code.slice(0, openTagEnd) + wrappedContent + code.slice(closeTagStart),
|
||||
map: {
|
||||
version: 3,
|
||||
sources: [],
|
||||
names: [],
|
||||
mappings: ""
|
||||
}
|
||||
};
|
||||
}
|
||||
// Annotate the CommonJS export names for ESM import in node:
|
||||
0 && (module.exports = {
|
||||
transformPage
|
||||
});
|
||||
@@ -0,0 +1,53 @@
|
||||
import { parseSFC, findNode } from "./utils.mjs";
|
||||
async function transformPage(code) {
|
||||
const sfc = await parseSFC(code);
|
||||
const openMatch = code.match(/<template\b[^>]*>/);
|
||||
if (!openMatch)
|
||||
return null;
|
||||
const closeTagStart = code.lastIndexOf("</template>");
|
||||
if (closeTagStart === -1)
|
||||
return null;
|
||||
const openTagEnd = openMatch.index + openMatch[0].length;
|
||||
const content = code.slice(openTagEnd, closeTagStart);
|
||||
let pageMetaSource = "";
|
||||
let newContent = content;
|
||||
if (sfc == null ? void 0 : sfc.template) {
|
||||
const pageMetaNode = findNode(sfc, "PageMeta");
|
||||
if (pageMetaNode) {
|
||||
pageMetaSource = pageMetaNode.loc.source;
|
||||
const metaStart = pageMetaNode.loc.start.offset;
|
||||
const metaEnd = pageMetaNode.loc.end.offset;
|
||||
const metaStartInContent = metaStart - openTagEnd;
|
||||
const metaEndInContent = metaEnd - openTagEnd;
|
||||
newContent = content.slice(0, metaStartInContent) + content.slice(metaEndInContent);
|
||||
}
|
||||
} else {
|
||||
const pageMetaMatch = content.match(/<page-meta\b[^>]*>[\s\S]*?<\/page-meta>/);
|
||||
if (pageMetaMatch) {
|
||||
pageMetaSource = pageMetaMatch[0];
|
||||
newContent = content.replace(pageMetaMatch[0], "");
|
||||
} else {
|
||||
const selfClosingMatch = content.match(/<page-meta\b[^>]*\/>/);
|
||||
if (selfClosingMatch) {
|
||||
pageMetaSource = selfClosingMatch[0];
|
||||
newContent = content.replace(selfClosingMatch[0], "");
|
||||
}
|
||||
}
|
||||
}
|
||||
const wrappedContent = `
|
||||
${pageMetaSource}
|
||||
<global-root-view>${newContent}</global-root-view>
|
||||
`;
|
||||
return {
|
||||
code: code.slice(0, openTagEnd) + wrappedContent + code.slice(closeTagStart),
|
||||
map: {
|
||||
version: 3,
|
||||
sources: [],
|
||||
names: [],
|
||||
mappings: ""
|
||||
}
|
||||
};
|
||||
}
|
||||
export {
|
||||
transformPage
|
||||
};
|
||||
@@ -0,0 +1,53 @@
|
||||
var __defProp = Object.defineProperty;
|
||||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
||||
var __export = (target, all) => {
|
||||
for (var name in all)
|
||||
__defProp(target, name, { get: all[name], enumerable: true });
|
||||
};
|
||||
var __copyProps = (to, from, except, desc) => {
|
||||
if (from && typeof from === "object" || typeof from === "function") {
|
||||
for (let key of __getOwnPropNames(from))
|
||||
if (!__hasOwnProp.call(to, key) && key !== except)
|
||||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
||||
}
|
||||
return to;
|
||||
};
|
||||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
||||
var root_exports = {};
|
||||
__export(root_exports, {
|
||||
rebuildRootApp: () => rebuildRootApp,
|
||||
registerRootApp: () => registerRootApp
|
||||
});
|
||||
module.exports = __toCommonJS(root_exports);
|
||||
function createEmptySourcemap() {
|
||||
return {
|
||||
version: 3,
|
||||
sources: [],
|
||||
names: [],
|
||||
mappings: ""
|
||||
};
|
||||
}
|
||||
function registerRootApp(code, fileName = "App.root") {
|
||||
const importCode = `import GlobalRootView from "./${fileName}.vue";`;
|
||||
const vueUseComponentCode = `app.component("global-root-view", GlobalRootView);`;
|
||||
let newCode = importCode + "\n" + code;
|
||||
const ssrAppMatch = newCode.match(/(const\s+app\s*=\s*createSSRApp\([^)]+\);)/);
|
||||
if (ssrAppMatch) {
|
||||
newCode = newCode.replace(/(const\s+app\s*=\s*createSSRApp\([^)]+\);)/, `$1
|
||||
${vueUseComponentCode}`);
|
||||
} else {
|
||||
newCode = newCode.replace(/(createApp[\s\S]*?)(return\s*\{)/, `$1${vueUseComponentCode}
|
||||
$2`);
|
||||
}
|
||||
return { code: newCode, map: createEmptySourcemap() };
|
||||
}
|
||||
function rebuildRootApp(code) {
|
||||
return { code, map: createEmptySourcemap() };
|
||||
}
|
||||
// Annotate the CommonJS export names for ESM import in node:
|
||||
0 && (module.exports = {
|
||||
rebuildRootApp,
|
||||
registerRootApp
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
function createEmptySourcemap() {
|
||||
return {
|
||||
version: 3,
|
||||
sources: [],
|
||||
names: [],
|
||||
mappings: ""
|
||||
};
|
||||
}
|
||||
function registerRootApp(code, fileName = "App.root") {
|
||||
const importCode = `import GlobalRootView from "./${fileName}.vue";`;
|
||||
const vueUseComponentCode = `app.component("global-root-view", GlobalRootView);`;
|
||||
let newCode = importCode + "\n" + code;
|
||||
const ssrAppMatch = newCode.match(/(const\s+app\s*=\s*createSSRApp\([^)]+\);)/);
|
||||
if (ssrAppMatch) {
|
||||
newCode = newCode.replace(/(const\s+app\s*=\s*createSSRApp\([^)]+\);)/, `$1
|
||||
${vueUseComponentCode}`);
|
||||
} else {
|
||||
newCode = newCode.replace(/(createApp[\s\S]*?)(return\s*\{)/, `$1${vueUseComponentCode}
|
||||
$2`);
|
||||
}
|
||||
return { code: newCode, map: createEmptySourcemap() };
|
||||
}
|
||||
function rebuildRootApp(code) {
|
||||
return { code, map: createEmptySourcemap() };
|
||||
}
|
||||
export {
|
||||
rebuildRootApp,
|
||||
registerRootApp
|
||||
};
|
||||
@@ -0,0 +1,136 @@
|
||||
var __create = Object.create;
|
||||
var __defProp = Object.defineProperty;
|
||||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||
var __getProtoOf = Object.getPrototypeOf;
|
||||
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
||||
var __export = (target, all) => {
|
||||
for (var name in all)
|
||||
__defProp(target, name, { get: all[name], enumerable: true });
|
||||
};
|
||||
var __copyProps = (to, from, except, desc) => {
|
||||
if (from && typeof from === "object" || typeof from === "function") {
|
||||
for (let key of __getOwnPropNames(from))
|
||||
if (!__hasOwnProp.call(to, key) && key !== except)
|
||||
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
||||
}
|
||||
return to;
|
||||
};
|
||||
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
||||
// If the importer is in node compatibility mode or this is not an ESM
|
||||
// file that has been converted to a CommonJS file using a Babel-
|
||||
// compatible transform (i.e. "__esModule" has not been set), then set
|
||||
// "default" to the CommonJS "module.exports" for node compatibility.
|
||||
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
||||
mod
|
||||
));
|
||||
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
||||
var utils_exports = {};
|
||||
__export(utils_exports, {
|
||||
debounce: () => debounce,
|
||||
findNode: () => findNode,
|
||||
formatPagePath: () => formatPagePath,
|
||||
loadPagesJson: () => loadPagesJson,
|
||||
normalizePlatformPath: () => normalizePlatformPath,
|
||||
parseSFC: () => parseSFC,
|
||||
stripJsonComments: () => stripJsonComments,
|
||||
toKebabCase: () => toKebabCase,
|
||||
toPascalCase: () => toPascalCase
|
||||
});
|
||||
module.exports = __toCommonJS(utils_exports);
|
||||
var import_node_fs = require("node:fs");
|
||||
var import_node_path = require("node:path");
|
||||
var import_vite = require("vite");
|
||||
async function parseSFC(code) {
|
||||
try {
|
||||
const { parse } = await import("vue/compiler-sfc");
|
||||
return parse(code, { pad: "space" }).descriptor;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
function stripJsonComments(str) {
|
||||
return str.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
|
||||
}
|
||||
function formatPagePath(root, path) {
|
||||
return (0, import_vite.normalizePath)(`${(0, import_node_path.join)(root, path)}.vue`).replace(/^[A-Z]:/, (match) => match.toLowerCase());
|
||||
}
|
||||
function loadPagesJson(path, rootPath) {
|
||||
const raw = (0, import_node_fs.readFileSync)(path, "utf-8");
|
||||
const { pages = [], subPackages = [] } = JSON.parse(stripJsonComments(raw));
|
||||
return [
|
||||
...pages.map((page) => formatPagePath(rootPath, page.path)),
|
||||
...subPackages.map(({ pages: subPages = [], root = "" }) => {
|
||||
return subPages.map((page) => formatPagePath((0, import_node_path.join)(rootPath, root), page.path));
|
||||
}).flat()
|
||||
];
|
||||
}
|
||||
function toKebabCase(str) {
|
||||
return str.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[_\s]+/g, "-").toLowerCase();
|
||||
}
|
||||
function toPascalCase(str) {
|
||||
return str.replace(/(^\w|-+\w)/g, (match) => match.toUpperCase().replace(/-/g, ""));
|
||||
}
|
||||
function findNode(sfc, rawTagName) {
|
||||
var _a, _b;
|
||||
const templateSource = (_a = sfc.template) == null ? void 0 : _a.content;
|
||||
if (!templateSource)
|
||||
return;
|
||||
let tagName = "";
|
||||
if (templateSource.includes(`<${toKebabCase(rawTagName)}`)) {
|
||||
tagName = toKebabCase(rawTagName);
|
||||
} else if (templateSource.includes(`<${toPascalCase(rawTagName)}`)) {
|
||||
tagName = toPascalCase(rawTagName);
|
||||
}
|
||||
if (!tagName)
|
||||
return;
|
||||
const nodeAst = (_b = sfc.template) == null ? void 0 : _b.ast;
|
||||
if (!nodeAst)
|
||||
return;
|
||||
const traverse = (nodes) => {
|
||||
var _a2;
|
||||
for (const node of nodes) {
|
||||
if (node.type === 1) {
|
||||
if (node.tag === tagName)
|
||||
return node;
|
||||
if ((_a2 = node.children) == null ? void 0 : _a2.length) {
|
||||
const found = traverse(node.children);
|
||||
if (found)
|
||||
return found;
|
||||
}
|
||||
}
|
||||
}
|
||||
return void 0;
|
||||
};
|
||||
return traverse(nodeAst.children);
|
||||
}
|
||||
function normalizePlatformPath(id) {
|
||||
const platform = process.env.UNI_PLATFORM;
|
||||
if (!platform)
|
||||
return id;
|
||||
const regex = new RegExp(`\\.${platform}\\.vue$`);
|
||||
if (regex.test(id)) {
|
||||
return id.replace(`.${platform}.`, ".");
|
||||
}
|
||||
return id;
|
||||
}
|
||||
function debounce(fn, delay) {
|
||||
let timer = null;
|
||||
return (...args) => {
|
||||
if (timer)
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(() => fn(...args), delay);
|
||||
};
|
||||
}
|
||||
// Annotate the CommonJS export names for ESM import in node:
|
||||
0 && (module.exports = {
|
||||
debounce,
|
||||
findNode,
|
||||
formatPagePath,
|
||||
loadPagesJson,
|
||||
normalizePlatformPath,
|
||||
parseSFC,
|
||||
stripJsonComments,
|
||||
toKebabCase,
|
||||
toPascalCase
|
||||
});
|
||||
@@ -0,0 +1,95 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { normalizePath } from "vite";
|
||||
async function parseSFC(code) {
|
||||
try {
|
||||
const { parse } = await import("vue/compiler-sfc");
|
||||
return parse(code, { pad: "space" }).descriptor;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
function stripJsonComments(str) {
|
||||
return str.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, "");
|
||||
}
|
||||
function formatPagePath(root, path) {
|
||||
return normalizePath(`${join(root, path)}.vue`).replace(/^[A-Z]:/, (match) => match.toLowerCase());
|
||||
}
|
||||
function loadPagesJson(path, rootPath) {
|
||||
const raw = readFileSync(path, "utf-8");
|
||||
const { pages = [], subPackages = [] } = JSON.parse(stripJsonComments(raw));
|
||||
return [
|
||||
...pages.map((page) => formatPagePath(rootPath, page.path)),
|
||||
...subPackages.map(({ pages: subPages = [], root = "" }) => {
|
||||
return subPages.map((page) => formatPagePath(join(rootPath, root), page.path));
|
||||
}).flat()
|
||||
];
|
||||
}
|
||||
function toKebabCase(str) {
|
||||
return str.replace(/([a-z])([A-Z])/g, "$1-$2").replace(/[_\s]+/g, "-").toLowerCase();
|
||||
}
|
||||
function toPascalCase(str) {
|
||||
return str.replace(/(^\w|-+\w)/g, (match) => match.toUpperCase().replace(/-/g, ""));
|
||||
}
|
||||
function findNode(sfc, rawTagName) {
|
||||
var _a, _b;
|
||||
const templateSource = (_a = sfc.template) == null ? void 0 : _a.content;
|
||||
if (!templateSource)
|
||||
return;
|
||||
let tagName = "";
|
||||
if (templateSource.includes(`<${toKebabCase(rawTagName)}`)) {
|
||||
tagName = toKebabCase(rawTagName);
|
||||
} else if (templateSource.includes(`<${toPascalCase(rawTagName)}`)) {
|
||||
tagName = toPascalCase(rawTagName);
|
||||
}
|
||||
if (!tagName)
|
||||
return;
|
||||
const nodeAst = (_b = sfc.template) == null ? void 0 : _b.ast;
|
||||
if (!nodeAst)
|
||||
return;
|
||||
const traverse = (nodes) => {
|
||||
var _a2;
|
||||
for (const node of nodes) {
|
||||
if (node.type === 1) {
|
||||
if (node.tag === tagName)
|
||||
return node;
|
||||
if ((_a2 = node.children) == null ? void 0 : _a2.length) {
|
||||
const found = traverse(node.children);
|
||||
if (found)
|
||||
return found;
|
||||
}
|
||||
}
|
||||
}
|
||||
return void 0;
|
||||
};
|
||||
return traverse(nodeAst.children);
|
||||
}
|
||||
function normalizePlatformPath(id) {
|
||||
const platform = process.env.UNI_PLATFORM;
|
||||
if (!platform)
|
||||
return id;
|
||||
const regex = new RegExp(`\\.${platform}\\.vue$`);
|
||||
if (regex.test(id)) {
|
||||
return id.replace(`.${platform}.`, ".");
|
||||
}
|
||||
return id;
|
||||
}
|
||||
function debounce(fn, delay) {
|
||||
let timer = null;
|
||||
return (...args) => {
|
||||
if (timer)
|
||||
clearTimeout(timer);
|
||||
timer = setTimeout(() => fn(...args), delay);
|
||||
};
|
||||
}
|
||||
export {
|
||||
debounce,
|
||||
findNode,
|
||||
formatPagePath,
|
||||
loadPagesJson,
|
||||
normalizePlatformPath,
|
||||
parseSFC,
|
||||
stripJsonComments,
|
||||
toKebabCase,
|
||||
toPascalCase
|
||||
};
|
||||
Reference in New Issue
Block a user