@mintlify/common 1.0.1246 → 1.0.1248
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.
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import type { Root } from 'mdast';
|
|
2
2
|
import type { FileType, FileWithImports } from '../../types/mdx/index.js';
|
|
3
|
+
import type { ImportMap } from '../../types/mdx/snippets/import.js';
|
|
3
4
|
import { type PackageImportOptions } from '../islands/packageImports.js';
|
|
4
5
|
export type ResolveImportsWarning = {
|
|
5
6
|
type: 'invalid-import-path' | 'missing-file' | 'import-resolution-error' | 'default-import-no-default-export' | 'export-collision' | 'undeclared-package' | 'package-import-conflict';
|
|
6
7
|
message: string;
|
|
7
8
|
};
|
|
8
9
|
export declare const resolveAllImports: (params: {
|
|
9
|
-
snippets: FileType
|
|
10
|
+
snippets: (FileType & {
|
|
11
|
+
importMap?: ImportMap;
|
|
12
|
+
})[];
|
|
10
13
|
fileWithImports: FileWithImports;
|
|
11
14
|
onWarning?: (warning: ResolveImportsWarning) => void;
|
|
12
15
|
packages?: PackageImportOptions;
|
|
@@ -7,6 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
+
import { jsx, toJs } from 'estree-util-to-js';
|
|
10
11
|
import { remove } from 'unist-util-remove';
|
|
11
12
|
import { visit } from 'unist-util-visit';
|
|
12
13
|
import { MdxImportSpecifier } from '../../types/mdx/snippets/import.js';
|
|
@@ -35,12 +36,90 @@ const takeImports = (tree) => __awaiter(void 0, void 0, void 0, function* () {
|
|
|
35
36
|
remove(tree, { cascade: false }, (node) => carriers.has(node) && !remaining.has(node));
|
|
36
37
|
return importMap;
|
|
37
38
|
});
|
|
39
|
+
const splitTopLevelImports = (tree) => __awaiter(void 0, void 0, void 0, function* () {
|
|
40
|
+
const carriers = tree.children.filter(hasImportDeclaration);
|
|
41
|
+
if (carriers.length === 0)
|
|
42
|
+
return { tree: Object.assign(Object.assign({}, tree), { children: [...tree.children] }), importMap: {} };
|
|
43
|
+
const { importMap } = yield findAndRemoveImports({
|
|
44
|
+
type: 'root',
|
|
45
|
+
children: structuredClone(carriers),
|
|
46
|
+
});
|
|
47
|
+
const taken = new Set(carriers);
|
|
48
|
+
return {
|
|
49
|
+
tree: Object.assign(Object.assign({}, tree), { children: tree.children.filter((node) => !taken.has(node)) }),
|
|
50
|
+
importMap,
|
|
51
|
+
};
|
|
52
|
+
});
|
|
53
|
+
const exportName = (name) => 'value' in name ? String(name.value) : name.name;
|
|
54
|
+
const toImportSpecifier = ({ local, exported }) => {
|
|
55
|
+
const imported = exportName(local);
|
|
56
|
+
const bound = exportName(exported);
|
|
57
|
+
if (bound === 'default')
|
|
58
|
+
return undefined;
|
|
59
|
+
if (imported === 'default')
|
|
60
|
+
return { type: MdxImportSpecifier.ImportDefaultSpecifier, name: bound };
|
|
61
|
+
if (imported === bound)
|
|
62
|
+
return { type: MdxImportSpecifier.ImportSpecifier, name: imported };
|
|
63
|
+
return { type: MdxImportSpecifier.RenamedImportSpecifier, name: imported, renamedName: bound };
|
|
64
|
+
};
|
|
65
|
+
const isPackageReExport = (statement, packageNames) => {
|
|
66
|
+
var _a;
|
|
67
|
+
return statement.type === 'ExportNamedDeclaration' &&
|
|
68
|
+
typeof ((_a = statement.source) === null || _a === void 0 ? void 0 : _a.value) === 'string' &&
|
|
69
|
+
isNonHostBareImport(statement.source.value) &&
|
|
70
|
+
isDeclaredPackageImport(statement.source.value, packageNames);
|
|
71
|
+
};
|
|
72
|
+
const takePackageReExports = (tree, packageNames) => {
|
|
73
|
+
const reExports = [];
|
|
74
|
+
tree.children = tree.children.flatMap((node) => {
|
|
75
|
+
var _a;
|
|
76
|
+
if (!isMdxJsEsm(node) || !estreeIsProgram(node))
|
|
77
|
+
return [node];
|
|
78
|
+
const statements = node.data.estree.body;
|
|
79
|
+
const reExported = statements.filter((statement) => isPackageReExport(statement, packageNames));
|
|
80
|
+
for (const statement of reExported) {
|
|
81
|
+
if (statement.type !== 'ExportNamedDeclaration' ||
|
|
82
|
+
typeof ((_a = statement.source) === null || _a === void 0 ? void 0 : _a.value) !== 'string')
|
|
83
|
+
continue;
|
|
84
|
+
reExports.push([
|
|
85
|
+
statement.source.value,
|
|
86
|
+
statement.specifiers.flatMap((s) => { var _a; return (_a = toImportSpecifier(s)) !== null && _a !== void 0 ? _a : []; }),
|
|
87
|
+
]);
|
|
88
|
+
}
|
|
89
|
+
const rest = statements.filter((statement) => !reExported.includes(statement));
|
|
90
|
+
if (rest.length === statements.length)
|
|
91
|
+
return [node];
|
|
92
|
+
if (rest.length === 0)
|
|
93
|
+
return [];
|
|
94
|
+
const estree = Object.assign(Object.assign({}, node.data.estree), { body: rest });
|
|
95
|
+
return [
|
|
96
|
+
Object.assign(Object.assign({}, node), { value: toJs(estree, { handlers: jsx }).value, data: Object.assign(Object.assign({}, node.data), { estree }) }),
|
|
97
|
+
];
|
|
98
|
+
});
|
|
99
|
+
return reExports;
|
|
100
|
+
};
|
|
101
|
+
const declaredPackageImports = (snippet, packages) => {
|
|
102
|
+
var _a;
|
|
103
|
+
return Object.entries((_a = snippet.importMap) !== null && _a !== void 0 ? _a : {}).flatMap(([source, specifiers]) => {
|
|
104
|
+
var _a;
|
|
105
|
+
const resolvedPath = resolveImportPath(source, snippet.filename);
|
|
106
|
+
if (resolvedPath == null) {
|
|
107
|
+
return isNonHostBareImport(source) && isDeclaredPackageImport(source, packages.packageNames)
|
|
108
|
+
? [[source, specifiers]]
|
|
109
|
+
: [];
|
|
110
|
+
}
|
|
111
|
+
return ((_a = packages.packageSnippets) === null || _a === void 0 ? void 0 : _a.has(resolvedPath)) ? [[resolvedPath, specifiers]] : [];
|
|
112
|
+
});
|
|
113
|
+
};
|
|
38
114
|
export const resolveAllImports = (params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
39
|
-
var _a, _b, _c, _d;
|
|
115
|
+
var _a, _b, _c, _d, _e;
|
|
40
116
|
const { snippets, fileWithImports, onWarning, packages } = params;
|
|
41
117
|
let ast = fileWithImports.tree;
|
|
118
|
+
const kept = packages && /\.mdx?$/.test(fileWithImports.filename)
|
|
119
|
+
? takePackageReExports(ast, packages.packageNames)
|
|
120
|
+
: [];
|
|
42
121
|
const exportMap = getExportMapFromTree(ast);
|
|
43
|
-
const
|
|
122
|
+
const strippedSnippets = new Map();
|
|
44
123
|
for (const source of Object.keys(fileWithImports.importMap)) {
|
|
45
124
|
const resolvedPath = resolveImportPath(source, fileWithImports.filename);
|
|
46
125
|
if (packages && resolvedPath == null && isNonHostBareImport(source)) {
|
|
@@ -71,8 +150,8 @@ export const resolveAllImports = (params) => __awaiter(void 0, void 0, void 0, f
|
|
|
71
150
|
}
|
|
72
151
|
continue;
|
|
73
152
|
}
|
|
74
|
-
const
|
|
75
|
-
if (
|
|
153
|
+
const foundSnippet = snippets.find((snippet) => snippet.filename === resolvedPath);
|
|
154
|
+
if (foundSnippet == undefined) {
|
|
76
155
|
const message = `Could not find file ${resolvedPath} - imported from ${fileWithImports.filename}`;
|
|
77
156
|
if (onWarning) {
|
|
78
157
|
onWarning({ type: 'missing-file', message });
|
|
@@ -82,6 +161,16 @@ export const resolveAllImports = (params) => __awaiter(void 0, void 0, void 0, f
|
|
|
82
161
|
}
|
|
83
162
|
continue;
|
|
84
163
|
}
|
|
164
|
+
let importedSnippet = (_e = strippedSnippets.get(foundSnippet)) !== null && _e !== void 0 ? _e : foundSnippet;
|
|
165
|
+
if (packages && !strippedSnippets.has(foundSnippet)) {
|
|
166
|
+
const { tree, importMap } = yield splitTopLevelImports(foundSnippet.tree);
|
|
167
|
+
importedSnippet = Object.assign(Object.assign({}, foundSnippet), { tree });
|
|
168
|
+
strippedSnippets.set(foundSnippet, importedSnippet);
|
|
169
|
+
kept.push(...Object.entries(importMap), ...declaredPackageImports(foundSnippet, packages));
|
|
170
|
+
if (/\.mdx?$/.test(foundSnippet.filename)) {
|
|
171
|
+
kept.push(...takePackageReExports(tree, packages.packageNames));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
85
174
|
const snippetExportMap = getExportMapFromTree(importedSnippet.tree);
|
|
86
175
|
const specifiers = fileWithImports.importMap[source];
|
|
87
176
|
if (specifiers == undefined)
|