@dcloudio/uni-cli-utils 3.0.0-alpha-3090920231127001
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/easycom/context.d.ts +22 -0
- package/dist/easycom/context.js +146 -0
- package/dist/easycom/declaration.d.ts +16 -0
- package/dist/easycom/declaration.js +114 -0
- package/dist/easycom/index.d.ts +1 -0
- package/dist/easycom/index.js +5 -0
- package/dist/easycom/options.d.ts +4 -0
- package/dist/easycom/options.js +143 -0
- package/dist/easycom/types.d.ts +45 -0
- package/dist/easycom/types.js +2 -0
- package/dist/easycom/utils.d.ts +3 -0
- package/dist/easycom/utils.js +22 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +17 -0
- package/dist/utils.d.ts +9 -0
- package/dist/utils.js +33 -0
- package/package.json +36 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { throttle } from 'throttle-debounce';
|
|
3
|
+
import type fs from 'node:fs';
|
|
4
|
+
import type { ComponentInfo, Options, ResolvedOptions } from './types';
|
|
5
|
+
export declare class EasyComContext {
|
|
6
|
+
options: ResolvedOptions;
|
|
7
|
+
private _componentPaths;
|
|
8
|
+
private _componentMap;
|
|
9
|
+
constructor(rawOptions: Options);
|
|
10
|
+
setupWatcher(watcher: fs.FSWatcher): void;
|
|
11
|
+
removeComponents(paths: string | string[]): boolean;
|
|
12
|
+
addComponents(paths: string | string[], parseTag: (filename: string) => string): boolean;
|
|
13
|
+
onUpdate(_path: string): void;
|
|
14
|
+
private updateComponentNameMap;
|
|
15
|
+
_searched: boolean;
|
|
16
|
+
searchGlob(): void;
|
|
17
|
+
searchComponents(): void;
|
|
18
|
+
searchCustomComponents(): void;
|
|
19
|
+
_generateDeclaration(removeUnused?: boolean): Promise<void>;
|
|
20
|
+
generateDeclaration: throttle<(removeUnused?: boolean) => Promise<void>>;
|
|
21
|
+
get componentMap(): Record<string, ComponentInfo>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.EasyComContext = void 0;
|
|
7
|
+
const throttle_debounce_1 = require("throttle-debounce");
|
|
8
|
+
const debug_1 = __importDefault(require("debug"));
|
|
9
|
+
const fast_glob_1 = __importDefault(require("fast-glob"));
|
|
10
|
+
const declaration_1 = require("./declaration");
|
|
11
|
+
const options_1 = require("./options");
|
|
12
|
+
const utils_1 = require("./utils");
|
|
13
|
+
const utils_2 = require("../utils");
|
|
14
|
+
const debug = {
|
|
15
|
+
components: (0, debug_1.default)('easycom:context:components'),
|
|
16
|
+
search: (0, debug_1.default)('easycom:context:search'),
|
|
17
|
+
declaration: (0, debug_1.default)('easycom:declaration'),
|
|
18
|
+
};
|
|
19
|
+
class EasyComContext {
|
|
20
|
+
constructor(rawOptions) {
|
|
21
|
+
this._componentPaths = new Map();
|
|
22
|
+
this._componentMap = {};
|
|
23
|
+
this._searched = false;
|
|
24
|
+
this.options = (0, options_1.resolveOptions)(rawOptions);
|
|
25
|
+
this.generateDeclaration = (0, throttle_debounce_1.throttle)(500, this._generateDeclaration.bind(this), { noLeading: false });
|
|
26
|
+
}
|
|
27
|
+
setupWatcher(watcher) {
|
|
28
|
+
const { globs, customGlobs } = this.options;
|
|
29
|
+
watcher.on('unlink', (path) => {
|
|
30
|
+
if ((0, utils_1.matchGlobs)(path, globs)) {
|
|
31
|
+
path = (0, utils_2.slash)(path);
|
|
32
|
+
this.removeComponents(path);
|
|
33
|
+
this.onUpdate(path);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
customGlobs.find((custom) => {
|
|
37
|
+
if ((0, utils_1.matchGlobs)(path, [custom.glob])) {
|
|
38
|
+
path = (0, utils_2.slash)(path);
|
|
39
|
+
this.removeComponents(path);
|
|
40
|
+
this.onUpdate(path);
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
watcher.on('add', (path) => {
|
|
47
|
+
if ((0, utils_1.matchGlobs)(path, globs)) {
|
|
48
|
+
path = (0, utils_2.slash)(path);
|
|
49
|
+
this.addComponents(path, utils_1.parseTag);
|
|
50
|
+
this.onUpdate(path);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
customGlobs.find((custom) => {
|
|
54
|
+
if ((0, utils_1.matchGlobs)(path, [custom.glob])) {
|
|
55
|
+
path = (0, utils_2.slash)(path);
|
|
56
|
+
this.addComponents(path, custom.parseTag);
|
|
57
|
+
this.onUpdate(path);
|
|
58
|
+
return true;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
removeComponents(paths) {
|
|
65
|
+
debug.components('remove', paths);
|
|
66
|
+
const size = this._componentPaths.size;
|
|
67
|
+
(0, utils_2.toArray)(paths).forEach((p) => this._componentPaths.delete(p));
|
|
68
|
+
if (this._componentPaths.size !== size) {
|
|
69
|
+
this.updateComponentNameMap();
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
addComponents(paths, parseTag) {
|
|
75
|
+
debug.components('add', paths);
|
|
76
|
+
const size = this._componentPaths.size;
|
|
77
|
+
(0, utils_2.toArray)(paths).forEach((p) => this._componentPaths.set(p, parseTag(p)));
|
|
78
|
+
if (this._componentPaths.size !== size) {
|
|
79
|
+
this.updateComponentNameMap();
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
onUpdate(_path) {
|
|
85
|
+
this.generateDeclaration();
|
|
86
|
+
}
|
|
87
|
+
updateComponentNameMap() {
|
|
88
|
+
this._componentMap = {};
|
|
89
|
+
this._componentPaths.forEach((tag, path) => {
|
|
90
|
+
if (this._componentMap[tag]) {
|
|
91
|
+
if ((0, utils_1.removeExtname)(path) === (0, utils_1.removeExtname)(this._componentMap[tag].from)) {
|
|
92
|
+
// 仅后缀不同,如.uvue|.vue|.nvue
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
if (!this.options.allowOverrides) {
|
|
96
|
+
console.warn(`[easycom] component "${tag}"(${path}) has naming conflicts with other components, ignored.`);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
this._componentMap[tag] = {
|
|
101
|
+
as: tag,
|
|
102
|
+
from: path,
|
|
103
|
+
};
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
searchGlob() {
|
|
107
|
+
if (this._searched)
|
|
108
|
+
return;
|
|
109
|
+
this.searchComponents();
|
|
110
|
+
debug.search(this._componentMap);
|
|
111
|
+
this._searched = true;
|
|
112
|
+
}
|
|
113
|
+
searchComponents() {
|
|
114
|
+
debug.search(`started with: [${this.options.globs.join(', ')}]`);
|
|
115
|
+
const root = this.options.inputDir;
|
|
116
|
+
const files = fast_glob_1.default.sync(this.options.globs, {
|
|
117
|
+
ignore: ['node_modules', 'unpackage', 'uniCloud-*', 'static'],
|
|
118
|
+
onlyFiles: true,
|
|
119
|
+
cwd: root,
|
|
120
|
+
absolute: true,
|
|
121
|
+
});
|
|
122
|
+
debug.search(`${files.length} components found.`);
|
|
123
|
+
this.addComponents(files, utils_1.parseTag);
|
|
124
|
+
this.searchCustomComponents();
|
|
125
|
+
}
|
|
126
|
+
searchCustomComponents() {
|
|
127
|
+
debug.search(`started with: [${this.options.customGlobs.join(', ')}]`);
|
|
128
|
+
const root = this.options.inputDir;
|
|
129
|
+
this.options.customGlobs.forEach((custom) => {
|
|
130
|
+
const files = fast_glob_1.default.sync(custom.glob, {
|
|
131
|
+
onlyFiles: true,
|
|
132
|
+
cwd: root,
|
|
133
|
+
absolute: true,
|
|
134
|
+
});
|
|
135
|
+
debug.search(`${files.length} components found.`);
|
|
136
|
+
this.addComponents(files, custom.parseTag);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
_generateDeclaration(removeUnused = false) {
|
|
140
|
+
return (0, declaration_1.writeDeclaration)(this, this.options.dts, removeUnused);
|
|
141
|
+
}
|
|
142
|
+
get componentMap() {
|
|
143
|
+
return this._componentMap;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
exports.EasyComContext = EasyComContext;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { EasyComContext } from './context';
|
|
2
|
+
import { ComponentInfo } from './types';
|
|
3
|
+
export declare function parseDeclaration(code: string): DeclarationImports | undefined;
|
|
4
|
+
/**
|
|
5
|
+
* Converts array of `ComponentInfo` to an import map
|
|
6
|
+
*
|
|
7
|
+
* `{ name: "typeof import(path)[importName]", ... }`
|
|
8
|
+
*/
|
|
9
|
+
export declare function stringifyComponentsInfo(filepath: string, components: ComponentInfo[]): Record<string, string>;
|
|
10
|
+
export interface DeclarationImports {
|
|
11
|
+
component: Record<string, string>;
|
|
12
|
+
}
|
|
13
|
+
export declare function getDeclarationImports(ctx: EasyComContext, filepath: string): DeclarationImports | undefined;
|
|
14
|
+
export declare function stringifyDeclarationImports(imports: Record<string, string>): string[];
|
|
15
|
+
export declare function getDeclaration(ctx: EasyComContext, filepath: string, originalImports?: DeclarationImports): string | undefined;
|
|
16
|
+
export declare function writeDeclaration(ctx: EasyComContext, filepath: string, removeUnused?: boolean): Promise<void>;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.writeDeclaration = exports.getDeclaration = exports.stringifyDeclarationImports = exports.getDeclarationImports = exports.stringifyComponentsInfo = exports.parseDeclaration = void 0;
|
|
4
|
+
const node_path_1 = require("node:path");
|
|
5
|
+
const node_fs_1 = require("node:fs");
|
|
6
|
+
const promises_1 = require("node:fs/promises");
|
|
7
|
+
const utils_1 = require("../utils");
|
|
8
|
+
const multilineCommentsRE = /\/\*.*?\*\//gms;
|
|
9
|
+
const singlelineCommentsRE = /\/\/.*$/gm;
|
|
10
|
+
function extractImports(code) {
|
|
11
|
+
return Object.fromEntries(Array.from(code.matchAll(/['"]?([^\s'"]+)['"]?\s*:\s*(.+?)[,;\n]/g)).map((i) => [i[1], i[2]]));
|
|
12
|
+
}
|
|
13
|
+
function parseDeclaration(code) {
|
|
14
|
+
if (!code)
|
|
15
|
+
return;
|
|
16
|
+
code = code.replace(multilineCommentsRE, '').replace(singlelineCommentsRE, '');
|
|
17
|
+
const imports = {
|
|
18
|
+
component: {},
|
|
19
|
+
};
|
|
20
|
+
const componentDeclaration = /export\s+interface\s+GlobalComponents\s*{(.*?)}/s.exec(code)?.[0];
|
|
21
|
+
if (componentDeclaration)
|
|
22
|
+
imports.component = extractImports(componentDeclaration);
|
|
23
|
+
return imports;
|
|
24
|
+
}
|
|
25
|
+
exports.parseDeclaration = parseDeclaration;
|
|
26
|
+
/**
|
|
27
|
+
* Converts `ComponentInfo` to an array
|
|
28
|
+
*
|
|
29
|
+
* `[name, "typeof import(path)[importName]"]`
|
|
30
|
+
*/
|
|
31
|
+
function stringifyComponentInfo(filepath, { from: path, as: name, name: importName }) {
|
|
32
|
+
if (!name)
|
|
33
|
+
return undefined;
|
|
34
|
+
const related = (0, node_path_1.isAbsolute)(path)
|
|
35
|
+
? `./${(0, node_path_1.relative)((0, node_path_1.dirname)(filepath), path)}`
|
|
36
|
+
: path;
|
|
37
|
+
const entry = `typeof import('${(0, utils_1.slash)(related)}')['${importName || 'default'}']`;
|
|
38
|
+
return [name, entry];
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Converts array of `ComponentInfo` to an import map
|
|
42
|
+
*
|
|
43
|
+
* `{ name: "typeof import(path)[importName]", ... }`
|
|
44
|
+
*/
|
|
45
|
+
function stringifyComponentsInfo(filepath, components) {
|
|
46
|
+
return Object.fromEntries(components
|
|
47
|
+
.map((info) => stringifyComponentInfo(filepath, info))
|
|
48
|
+
.filter(utils_1.notNullish));
|
|
49
|
+
}
|
|
50
|
+
exports.stringifyComponentsInfo = stringifyComponentsInfo;
|
|
51
|
+
function getDeclarationImports(ctx, filepath) {
|
|
52
|
+
const component = stringifyComponentsInfo(filepath, [
|
|
53
|
+
...Object.values(ctx.componentMap),
|
|
54
|
+
]);
|
|
55
|
+
if (Object.keys(component).length === 0)
|
|
56
|
+
return;
|
|
57
|
+
return { component };
|
|
58
|
+
}
|
|
59
|
+
exports.getDeclarationImports = getDeclarationImports;
|
|
60
|
+
function stringifyDeclarationImports(imports) {
|
|
61
|
+
return Object.entries(imports)
|
|
62
|
+
.sort(([a], [b]) => a.localeCompare(b))
|
|
63
|
+
.map(([name, v]) => {
|
|
64
|
+
if (!/^\w+$/.test(name))
|
|
65
|
+
name = `'${name}'`;
|
|
66
|
+
return `${name}: ${v}`;
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
exports.stringifyDeclarationImports = stringifyDeclarationImports;
|
|
70
|
+
function getDeclaration(ctx, filepath, originalImports) {
|
|
71
|
+
const imports = getDeclarationImports(ctx, filepath);
|
|
72
|
+
if (!imports)
|
|
73
|
+
return;
|
|
74
|
+
const declarations = {
|
|
75
|
+
component: stringifyDeclarationImports({
|
|
76
|
+
...originalImports?.component,
|
|
77
|
+
...imports.component,
|
|
78
|
+
}),
|
|
79
|
+
};
|
|
80
|
+
const head = `export {}
|
|
81
|
+
|
|
82
|
+
declare module 'vue' {`;
|
|
83
|
+
let code = `/* eslint-disable */
|
|
84
|
+
/* prettier-ignore */
|
|
85
|
+
// @ts-nocheck
|
|
86
|
+
${head}`;
|
|
87
|
+
if (Object.keys(declarations.component).length > 0) {
|
|
88
|
+
code += `
|
|
89
|
+
export interface GlobalComponents {
|
|
90
|
+
${declarations.component.join('\n ')}
|
|
91
|
+
}`;
|
|
92
|
+
}
|
|
93
|
+
code += '\n}\n';
|
|
94
|
+
return code;
|
|
95
|
+
}
|
|
96
|
+
exports.getDeclaration = getDeclaration;
|
|
97
|
+
async function writeFile(filePath, content) {
|
|
98
|
+
await (0, promises_1.mkdir)((0, node_path_1.dirname)(filePath), { recursive: true });
|
|
99
|
+
return await (0, promises_1.writeFile)(filePath, content, 'utf-8');
|
|
100
|
+
}
|
|
101
|
+
async function writeDeclaration(ctx, filepath, removeUnused = false) {
|
|
102
|
+
const originalContent = (0, node_fs_1.existsSync)(filepath)
|
|
103
|
+
? await (0, promises_1.readFile)(filepath, 'utf-8')
|
|
104
|
+
: '';
|
|
105
|
+
const originalImports = removeUnused
|
|
106
|
+
? undefined
|
|
107
|
+
: parseDeclaration(originalContent);
|
|
108
|
+
const code = getDeclaration(ctx, filepath, originalImports);
|
|
109
|
+
if (!code)
|
|
110
|
+
return;
|
|
111
|
+
if (code !== originalContent)
|
|
112
|
+
await writeFile(filepath, code);
|
|
113
|
+
}
|
|
114
|
+
exports.writeDeclaration = writeDeclaration;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { EasyComContext } from './context';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EasyComContext = void 0;
|
|
4
|
+
var context_1 = require("./context");
|
|
5
|
+
Object.defineProperty(exports, "EasyComContext", { enumerable: true, get: function () { return context_1.EasyComContext; } });
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { EasyComCustom, Options, ResolvedOptions } from './types';
|
|
2
|
+
export declare function resolveOptions(rawOptions: Options): ResolvedOptions;
|
|
3
|
+
export declare function parseGlob(glob: string, inputDir: string): string;
|
|
4
|
+
export declare function parseEasyComTag(tag: string, path: string, inputDir: string): EasyComCustom;
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseEasyComTag = exports.parseGlob = exports.resolveOptions = void 0;
|
|
4
|
+
const node_fs_1 = require("node:fs");
|
|
5
|
+
const node_path_1 = require("node:path");
|
|
6
|
+
const jsonc_parser_1 = require("jsonc-parser");
|
|
7
|
+
const resolve_1 = require("resolve");
|
|
8
|
+
const utils_1 = require("../utils");
|
|
9
|
+
const AUTO_SCAN_GLOBS = [
|
|
10
|
+
'components/**/*.{vue,uvue,nvue}',
|
|
11
|
+
'uni_modules/*/components/**/*.{vue,uvue,nvue}',
|
|
12
|
+
];
|
|
13
|
+
function normalizeGlob(str, replace = '*') {
|
|
14
|
+
// 将所有 $0、$1 等占位符,替换为*
|
|
15
|
+
return str.replace(/\$\d+/g, replace);
|
|
16
|
+
}
|
|
17
|
+
function resolveOptions(rawOptions) {
|
|
18
|
+
const options = {
|
|
19
|
+
dts: (0, node_path_1.resolve)(rawOptions.outputDir, '../.dts/easycom.d.ts'),
|
|
20
|
+
allowOverrides: false,
|
|
21
|
+
...rawOptions,
|
|
22
|
+
...initGlobs(rawOptions.inputDir),
|
|
23
|
+
};
|
|
24
|
+
return options;
|
|
25
|
+
}
|
|
26
|
+
exports.resolveOptions = resolveOptions;
|
|
27
|
+
function initGlobs(inputDir) {
|
|
28
|
+
const customGlobs = [];
|
|
29
|
+
let autoScan = true;
|
|
30
|
+
const pagesJsonFilename = (0, node_path_1.resolve)(inputDir, 'pages.json');
|
|
31
|
+
if ((0, node_fs_1.existsSync)(pagesJsonFilename)) {
|
|
32
|
+
const pagesJson = (0, jsonc_parser_1.parse)((0, node_fs_1.readFileSync)(pagesJsonFilename, 'utf8'));
|
|
33
|
+
const easyComOptions = pagesJson?.easycom;
|
|
34
|
+
if (easyComOptions) {
|
|
35
|
+
if (easyComOptions.autoscan === false) {
|
|
36
|
+
autoScan = false;
|
|
37
|
+
}
|
|
38
|
+
const customOptions = easyComOptions.custom;
|
|
39
|
+
if (customOptions) {
|
|
40
|
+
Object.keys(customOptions).forEach((tag) => {
|
|
41
|
+
customGlobs.push(parseEasyComTag(tag, customOptions[tag], inputDir));
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return { globs: autoScan ? AUTO_SCAN_GLOBS : [], customGlobs };
|
|
47
|
+
}
|
|
48
|
+
function parseGlob(glob, inputDir) {
|
|
49
|
+
const parts = glob.split('/');
|
|
50
|
+
const index = parts.findIndex((part) => part.includes('*'));
|
|
51
|
+
let globParts = [];
|
|
52
|
+
if (index > -1) {
|
|
53
|
+
globParts = parts.splice(index);
|
|
54
|
+
}
|
|
55
|
+
const popParts = [];
|
|
56
|
+
function findPath(parts) {
|
|
57
|
+
if (parts.length === 0) {
|
|
58
|
+
return glob;
|
|
59
|
+
}
|
|
60
|
+
const checkPath = parts.join('/');
|
|
61
|
+
try {
|
|
62
|
+
const resolvedPath = (0, resolve_1.sync)(checkPath, {
|
|
63
|
+
basedir: inputDir,
|
|
64
|
+
packageFilter(pkg) {
|
|
65
|
+
if (!pkg.main) {
|
|
66
|
+
pkg.main = 'package.json';
|
|
67
|
+
}
|
|
68
|
+
return pkg;
|
|
69
|
+
},
|
|
70
|
+
// preserveSymlinks: false,
|
|
71
|
+
});
|
|
72
|
+
return (0, utils_1.slash)((0, node_path_1.join)((0, node_path_1.dirname)(resolvedPath), ...popParts, ...globParts));
|
|
73
|
+
}
|
|
74
|
+
catch (err) {
|
|
75
|
+
const part = parts.pop();
|
|
76
|
+
if (part) {
|
|
77
|
+
popParts.push(part);
|
|
78
|
+
}
|
|
79
|
+
return findPath(parts);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return findPath(parts);
|
|
83
|
+
}
|
|
84
|
+
exports.parseGlob = parseGlob;
|
|
85
|
+
function parseEasyComTag(tag, path, inputDir) {
|
|
86
|
+
let glob = normalizeGlob(path);
|
|
87
|
+
// 项目根目录
|
|
88
|
+
if (glob.startsWith('@/')) {
|
|
89
|
+
glob = glob.slice(2);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
// npm
|
|
93
|
+
glob = parseGlob(glob, inputDir);
|
|
94
|
+
}
|
|
95
|
+
const mappings = [];
|
|
96
|
+
const matches = path.match(/\$\d+/g);
|
|
97
|
+
if (matches) {
|
|
98
|
+
matches.forEach((m, index) => {
|
|
99
|
+
mappings.push([index + 1, parseInt(m.slice(1))]);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* 映射关系
|
|
104
|
+
* @dcloudio/uni-ui/lib/uni-$1/uni-$1.vue 会被转换为 @dcloudio/uni-ui/lib/uni-(.*)/uni-(.*).vue
|
|
105
|
+
* [[1, 1],[2, 1]]
|
|
106
|
+
* 第一个元素是转换后的(.*)索引位置,第二个元素是转换前的索引位置
|
|
107
|
+
*/
|
|
108
|
+
const regex = normalizeGlob(path, '(.*)');
|
|
109
|
+
return {
|
|
110
|
+
tag,
|
|
111
|
+
path,
|
|
112
|
+
glob,
|
|
113
|
+
parseTag(filename) {
|
|
114
|
+
const matches = filename.match(regex);
|
|
115
|
+
if (matches) {
|
|
116
|
+
let newTag = replaceWithIndexes(tag);
|
|
117
|
+
for (let i = mappings.length - 1; i >= 0; i--) {
|
|
118
|
+
const mapping = mappings[i];
|
|
119
|
+
newTag = newTag.replace('$' + mapping[1], matches[mapping[0]]);
|
|
120
|
+
}
|
|
121
|
+
return newTag;
|
|
122
|
+
}
|
|
123
|
+
return '';
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
exports.parseEasyComTag = parseEasyComTag;
|
|
128
|
+
function replaceWithIndexes(pattern) {
|
|
129
|
+
// 匹配(.*)的正则表达式
|
|
130
|
+
const regex = /\(\.\*\)/g;
|
|
131
|
+
let index = 1;
|
|
132
|
+
// 替换所有的(.*)为$+索引的形式
|
|
133
|
+
let replaced = pattern.replace(regex, function () {
|
|
134
|
+
return '$' + index++;
|
|
135
|
+
});
|
|
136
|
+
if (replaced.startsWith('^')) {
|
|
137
|
+
replaced = replaced.slice(1);
|
|
138
|
+
}
|
|
139
|
+
if (replaced.endsWith('$')) {
|
|
140
|
+
replaced = replaced.slice(0, -1);
|
|
141
|
+
}
|
|
142
|
+
return replaced;
|
|
143
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export interface Options {
|
|
2
|
+
inputDir: string;
|
|
3
|
+
outputDir: string;
|
|
4
|
+
dts?: string;
|
|
5
|
+
allowOverrides?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export interface ResolvedOptions extends Required<Options> {
|
|
8
|
+
globs: string[];
|
|
9
|
+
customGlobs: EasyComCustom[];
|
|
10
|
+
}
|
|
11
|
+
interface ImportInfo {
|
|
12
|
+
as?: string;
|
|
13
|
+
name?: string;
|
|
14
|
+
from: string;
|
|
15
|
+
}
|
|
16
|
+
type SideEffectsInfo = (ImportInfo | string)[] | ImportInfo | string | undefined;
|
|
17
|
+
export interface ComponentInfo extends ImportInfo {
|
|
18
|
+
sideEffects?: SideEffectsInfo;
|
|
19
|
+
}
|
|
20
|
+
export interface EasyComOptions {
|
|
21
|
+
autoscan?: boolean;
|
|
22
|
+
custom?: Record<string, string>;
|
|
23
|
+
}
|
|
24
|
+
export interface EasyComCustom {
|
|
25
|
+
/**
|
|
26
|
+
* 组件标签名(正则)
|
|
27
|
+
* ^uni-(.*)
|
|
28
|
+
*/
|
|
29
|
+
tag: string;
|
|
30
|
+
/**
|
|
31
|
+
* 组件路径
|
|
32
|
+
* @dcloudio/uni-ui/lib/uni-$1/uni-$1.vue
|
|
33
|
+
*/
|
|
34
|
+
path: string;
|
|
35
|
+
/**
|
|
36
|
+
* 绝对路径的glob
|
|
37
|
+
*/
|
|
38
|
+
glob: string;
|
|
39
|
+
/**
|
|
40
|
+
* 根据文件解析组件名
|
|
41
|
+
* @param filename
|
|
42
|
+
*/
|
|
43
|
+
parseTag(filename: string): string;
|
|
44
|
+
}
|
|
45
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.removeExtname = exports.parseTag = exports.matchGlobs = void 0;
|
|
4
|
+
const minimatch_1 = require("minimatch");
|
|
5
|
+
const utils_1 = require("../utils");
|
|
6
|
+
const node_path_1 = require("node:path");
|
|
7
|
+
function matchGlobs(filepath, globs) {
|
|
8
|
+
for (const glob of globs) {
|
|
9
|
+
if ((0, minimatch_1.minimatch)((0, utils_1.slash)(filepath), glob))
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
exports.matchGlobs = matchGlobs;
|
|
15
|
+
function parseTag(filename) {
|
|
16
|
+
return removeExtname((0, node_path_1.basename)(filename));
|
|
17
|
+
}
|
|
18
|
+
exports.parseTag = parseTag;
|
|
19
|
+
function removeExtname(filename) {
|
|
20
|
+
return filename.replace(/\.\w+$/, '');
|
|
21
|
+
}
|
|
22
|
+
exports.removeExtname = removeExtname;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './easycom';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __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
|
+
}));
|
|
13
|
+
var __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
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./easycom"), exports);
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare function notNullish<T>(v: T | null | undefined): v is NonNullable<T>;
|
|
2
|
+
export declare function slash(str: string): string;
|
|
3
|
+
export type Nullable<T> = T | null | undefined;
|
|
4
|
+
export type Arrayable<T> = T | Array<T>;
|
|
5
|
+
export declare function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T>;
|
|
6
|
+
export declare function pascalCase(str: string): string;
|
|
7
|
+
export declare function camelCase(str: string): string;
|
|
8
|
+
export declare function kebabCase(key: string): string;
|
|
9
|
+
export declare function capitalize(str: string): string;
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.capitalize = exports.kebabCase = exports.camelCase = exports.pascalCase = exports.toArray = exports.slash = exports.notNullish = void 0;
|
|
4
|
+
function notNullish(v) {
|
|
5
|
+
return v != null;
|
|
6
|
+
}
|
|
7
|
+
exports.notNullish = notNullish;
|
|
8
|
+
function slash(str) {
|
|
9
|
+
return str.replace(/\\/g, '/');
|
|
10
|
+
}
|
|
11
|
+
exports.slash = slash;
|
|
12
|
+
function toArray(array) {
|
|
13
|
+
array = array ?? [];
|
|
14
|
+
return Array.isArray(array) ? array : [array];
|
|
15
|
+
}
|
|
16
|
+
exports.toArray = toArray;
|
|
17
|
+
function pascalCase(str) {
|
|
18
|
+
return capitalize(camelCase(str));
|
|
19
|
+
}
|
|
20
|
+
exports.pascalCase = pascalCase;
|
|
21
|
+
function camelCase(str) {
|
|
22
|
+
return str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : ''));
|
|
23
|
+
}
|
|
24
|
+
exports.camelCase = camelCase;
|
|
25
|
+
function kebabCase(key) {
|
|
26
|
+
const result = key.replace(/([A-Z])/g, ' $1').trim();
|
|
27
|
+
return result.split(' ').join('-').toLowerCase();
|
|
28
|
+
}
|
|
29
|
+
exports.kebabCase = kebabCase;
|
|
30
|
+
function capitalize(str) {
|
|
31
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
32
|
+
}
|
|
33
|
+
exports.capitalize = capitalize;
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dcloudio/uni-cli-utils",
|
|
3
|
+
"version": "3.0.0-alpha-3090920231127001",
|
|
4
|
+
"description": "@dcloudio/uni-cli-utils",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"lib"
|
|
10
|
+
],
|
|
11
|
+
"engines": {
|
|
12
|
+
"node": "^14.18.0 || >=16.0.0"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/dcloudio/uni-app.git",
|
|
17
|
+
"directory": "packages/uni-cli-utils"
|
|
18
|
+
},
|
|
19
|
+
"license": "Apache-2.0",
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/dcloudio/uni-app/issues"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"debug": "^4.3.3",
|
|
25
|
+
"fast-glob": "^3.2.11",
|
|
26
|
+
"jsonc-parser": "^3.2.0",
|
|
27
|
+
"minimatch": "^9.0.3",
|
|
28
|
+
"resolve": "^1.22.1",
|
|
29
|
+
"throttle-debounce": "^5.0.0"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@types/debug": "^4.1.7",
|
|
33
|
+
"@types/resolve": "^1.20.2",
|
|
34
|
+
"@types/throttle-debounce": "^5.0.2"
|
|
35
|
+
}
|
|
36
|
+
}
|