@lukoweb/apitogo 0.1.96 → 0.1.97
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/cli/cli.js +269 -221
- package/package.json +1 -1
- package/src/vite/config.ts +3 -0
- package/src/vite/plugin-offroot-resolve.ts +71 -0
- package/src/vite/plugin.ts +2 -0
package/package.json
CHANGED
package/src/vite/config.ts
CHANGED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import type { Plugin, ResolveIdOptions } from "vite";
|
|
3
|
+
import { getCurrentConfig } from "../config/loader.js";
|
|
4
|
+
|
|
5
|
+
function stripQuery(id: string): string {
|
|
6
|
+
const query = id.indexOf("?");
|
|
7
|
+
return query === -1 ? id : id.slice(0, query);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function isInside(parent: string, child: string): boolean {
|
|
11
|
+
const relative = path.relative(path.resolve(parent), path.resolve(child));
|
|
12
|
+
return (
|
|
13
|
+
relative === "" ||
|
|
14
|
+
(!relative.startsWith("..") && !path.isAbsolute(relative))
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function isBareSpecifier(source: string): boolean {
|
|
19
|
+
return (
|
|
20
|
+
!source.startsWith("\0") &&
|
|
21
|
+
!source.startsWith(".") &&
|
|
22
|
+
!source.startsWith("/") &&
|
|
23
|
+
!path.isAbsolute(source)
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function isInsideNodeModules(filePath: string): boolean {
|
|
28
|
+
return filePath.split(path.sep).includes("node_modules");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** True when Vite followed a symlink (or glob) to markdown outside the site. */
|
|
32
|
+
export function shouldResolveFromSiteRoot(
|
|
33
|
+
source: string,
|
|
34
|
+
importer: string | undefined,
|
|
35
|
+
rootDir: string,
|
|
36
|
+
): boolean {
|
|
37
|
+
if (!importer || !isBareSpecifier(source)) {
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
const importerPath = stripQuery(importer);
|
|
41
|
+
if (isInside(rootDir, importerPath) || isInsideNodeModules(importerPath)) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Linked docs live outside the site (e.g. ../doc/*.md). Vite import-analysis
|
|
49
|
+
* then cannot resolve `@lukoweb/apitogo/components` from that folder.
|
|
50
|
+
* Re-resolve bare imports as if they came from the site package.json.
|
|
51
|
+
*/
|
|
52
|
+
export default function viteOffrootResolvePlugin(): Plugin {
|
|
53
|
+
return {
|
|
54
|
+
name: "zudoku-offroot-resolve",
|
|
55
|
+
enforce: "pre",
|
|
56
|
+
async resolveId(
|
|
57
|
+
source: string,
|
|
58
|
+
importer: string | undefined,
|
|
59
|
+
options: ResolveIdOptions,
|
|
60
|
+
) {
|
|
61
|
+
const rootDir = getCurrentConfig().__meta.rootDir;
|
|
62
|
+
if (!shouldResolveFromSiteRoot(source, importer, rootDir)) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
return this.resolve(source, path.join(rootDir, "package.json"), {
|
|
66
|
+
...options,
|
|
67
|
+
skipSelf: true,
|
|
68
|
+
});
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
}
|
package/src/vite/plugin.ts
CHANGED
|
@@ -16,6 +16,7 @@ import viteMarkdownExportPlugin from "./plugin-markdown-export.js";
|
|
|
16
16
|
import viteMdxPlugin from "./plugin-mdx.js";
|
|
17
17
|
import viteModulesPlugin from "./plugin-modules.js";
|
|
18
18
|
import { viteNavigationPlugin } from "./plugin-navigation.js";
|
|
19
|
+
import viteOffrootResolvePlugin from "./plugin-offroot-resolve.js";
|
|
19
20
|
import { viteSearchPlugin } from "./plugin-search.js";
|
|
20
21
|
import { viteShikiRegisterPlugin } from "./plugin-shiki-register.js";
|
|
21
22
|
import { viteThemePlugin } from "./plugin-theme.js";
|
|
@@ -23,6 +24,7 @@ import { viteThemePlugin } from "./plugin-theme.js";
|
|
|
23
24
|
export default function vitePlugin(): PluginOption {
|
|
24
25
|
return [
|
|
25
26
|
viteShikiRegisterPlugin(),
|
|
27
|
+
viteOffrootResolvePlugin(),
|
|
26
28
|
viteConfigReloadPlugin(),
|
|
27
29
|
viteLocalManifestPlugin(),
|
|
28
30
|
viteMdxPlugin(),
|