@longzai-intelligence-bun/alias-plugin 0.0.9

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/README.md ADDED
@@ -0,0 +1,122 @@
1
+ # @longzai-intelligence-bun/alias-plugin
2
+
3
+ Bun.build 路径别名解析插件,在打包时将路径别名(如 `@webview/*`、`@bun/*`)解析为实际文件绝对路径。
4
+
5
+ ## 背景
6
+
7
+ `Bun.build()` 打包器既不支持 `alias` 选项,也不自动解析 `tsconfig.json` 的 `paths`。这导致使用路径别名的源码在 `Bun.build()` 打包时报「无法解析模块」。本插件通过 `onResolve` 钩子在打包时把别名前缀映射为实际文件绝对路径,补齐 `Bun.build()` 的别名解析能力缺口。
8
+
9
+ ## 安装
10
+
11
+ ```bash
12
+ bun add -D @longzai-intelligence-bun/alias-plugin
13
+ ```
14
+
15
+ ## 快速开始
16
+
17
+ ```typescript
18
+ import { createAliasPlugin } from '@longzai-intelligence-bun/alias-plugin';
19
+
20
+ const aliasPlugin = createAliasPlugin(import.meta.dir, {
21
+ '@webview': 'src/webview',
22
+ '@bun': 'src/bun',
23
+ });
24
+
25
+ const result = await Bun.build({
26
+ entrypoints: ['src/bun/index.ts'],
27
+ plugins: [aliasPlugin],
28
+ });
29
+ ```
30
+
31
+ ## 核心 API
32
+
33
+ ### createAliasPlugin(rootDir, aliasMap)
34
+
35
+ 创建路径别名解析插件实例。
36
+
37
+ | 参数 | 类型 | 说明 |
38
+ | --- | --- | --- |
39
+ | `rootDir` | `string` | 项目根目录的绝对路径,别名目标目录相对此解析 |
40
+ | `aliasMap` | `AliasMap` | 别名前缀到目标目录的映射 |
41
+
42
+ `AliasMap` 类型:
43
+
44
+ ```typescript
45
+ type AliasMap = Record<string, string>;
46
+ ```
47
+
48
+ 键为别名前缀(如 `@webview`),值为 `rootDir` 下的目标目录(如 `src/webview`)。插件匹配 `@webview/foo/bar` 后,会解析为 `rootDir/src/webview/foo/bar`。
49
+
50
+ ### aliasMap 配置示例
51
+
52
+ ```typescript
53
+ const aliasMap = {
54
+ '@webview': 'src/webview',
55
+ '@bun': 'src/bun',
56
+ '@shared': 'src/shared',
57
+ } as const;
58
+ ```
59
+
60
+ ## 工作原理
61
+
62
+ 1. 根据 `aliasMap` 的键构建别名前缀正则表达式,作为 `onResolve` 的 `filter`。
63
+ 2. 对匹配别名前缀的导入路径,剥除别名前缀得到相对路径。
64
+ 3. 拼接 `rootDir + 目标目录 + 相对路径` 得到待解析基础路径。
65
+ 4. 依次尝试补全扩展名和 index 文件,命中第一个存在的文件即返回其绝对路径。
66
+
67
+ 扩展名补全顺序:
68
+
69
+ | 序号 | 候选路径 |
70
+ | --- | --- |
71
+ | 1 | 原路径 |
72
+ | 2 | `.ts` |
73
+ | 3 | `.tsx` |
74
+ | 4 | `.js` |
75
+ | 5 | `/index.ts` |
76
+ | 6 | `/index.tsx` |
77
+
78
+ ## 能力边界
79
+
80
+ ### 支持
81
+
82
+ - 前缀式别名(`@alias/sub/path` → 目标目录 + 子路径)。
83
+ - 自动补全 `.ts` / `.tsx` / `.js` 及对应 `index` 文件。
84
+ - 多别名共存(一个插件实例处理多个别名前缀)。
85
+
86
+ ### 不支持
87
+
88
+ - **通配符路径**(如 tsconfig `paths` 的 `@/*`、`*` 占位符)。本插件只做前缀匹配,不做 glob 通配。
89
+ - **node_modules 包别名**(如把 `react` 重映射到 `preact`)。本插件仅解析文件系统路径。
90
+ - **构建后别名替换**。本插件作用于打包时(`onResolve`),不处理 dist 产物的文本替换——后者由 `@longzai-intelligence-typescript/resolve-aliases`(post-tsgo 工具)负责,两者时机与机制完全不同。
91
+
92
+ ## 典型场景:Electrobun 桌面应用
93
+
94
+ [Longzai Intelligence Electrobun](../../../longzai-intelligence-electrobun) 直接调用 `Bun.build()` 打包主进程与 webview 视图,其配置(`lzi-electrobun.config.ts`)的 `build.bun.plugins` / `build.views.*.plugins` 接受 `Bun.build()` 插件。将本插件传入即可在 Electrobun 打包链路启用别名解析:
95
+
96
+ ```typescript
97
+ import { createAliasPlugin } from '@longzai-intelligence-bun/alias-plugin';
98
+
99
+ const aliasPlugin = createAliasPlugin(import.meta.dir, {
100
+ '@webview': 'src/webview',
101
+ '@bun': 'src/bun',
102
+ });
103
+
104
+ export default {
105
+ build: {
106
+ bun: {
107
+ entrypoint: 'src/bun/index.ts',
108
+ plugins: [aliasPlugin],
109
+ },
110
+ views: {
111
+ webview: {
112
+ entrypoint: 'src/webview/index.tsx',
113
+ plugins: [aliasPlugin],
114
+ },
115
+ },
116
+ },
117
+ };
118
+ ```
119
+
120
+ ## 许可证
121
+
122
+ UNLICENSED
@@ -0,0 +1,25 @@
1
+ import "./rolldown-runtime-BqT_7tdF.js";
2
+ //#region src/index.d.ts
3
+ /**
4
+ * Bun.build 路径别名解析插件
5
+ *
6
+ * Bun.build 不支持 alias 选项,也不自动解析 tsconfig paths。
7
+ * 此插件在打包时将路径别名(如 @webview/*、@bun/*)解析为实际文件绝对路径,
8
+ * 支持自动补全扩展名和 index 文件解析。
9
+ */
10
+ /**
11
+ * 别名映射规则
12
+ *
13
+ * 键为别名前缀(如 "@webview"),值为项目根目录下的目标目录(如 "src/webview")。
14
+ */
15
+ type AliasMap = Record<string, string>;
16
+ /**
17
+ * 创建路径别名解析插件
18
+ *
19
+ * @param rootDir - 项目根目录的绝对路径
20
+ * @param aliasMap - 别名前缀到目标目录的映射
21
+ * @returns Bun.build 插件实例
22
+ */
23
+ declare function createAliasPlugin(rootDir: string, aliasMap: AliasMap): BunPlugin;
24
+ //#endregion
25
+ export { AliasMap, createAliasPlugin };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ import"./rolldown-runtime-BqT_7tdF.js";import{statSync as e}from"node:fs";import{resolve as t}from"node:path";function n(t){try{return e(t).isFile()}catch{return!1}}function r(e){let t=[e,`${e}.ts`,`${e}.tsx`,`${e}.js`,`${e}/index.ts`,`${e}/index.tsx`];for(let e of t)if(n(e))return e;return null}function i(e,n){let i=Object.keys(n).map(e=>e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`)),a=RegExp(`^(${i.join(`|`)})/`);return{name:`alias-resolver`,setup(i){i.onResolve({filter:a},i=>{for(let[a,o]of Object.entries(n))if(i.path.startsWith(a+`/`)){let n=i.path.slice(a.length),s=r(t(e,o+n));if(s)return{path:s}}})}}}export{i as createAliasPlugin};
@@ -0,0 +1 @@
1
+ import"node:module";export{};
package/package.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "name": "@longzai-intelligence-bun/alias-plugin",
3
+ "version": "0.0.9",
4
+ "description": "Bun.build 路径别名解析插件",
5
+ "license": "UNLICENSED",
6
+ "files": [
7
+ "dist"
8
+ ],
9
+ "type": "module",
10
+ "main": "./dist/index.js",
11
+ "module": "./dist/index.mjs",
12
+ "types": "./dist/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.mjs",
17
+ "require": "./dist/index.js"
18
+ }
19
+ },
20
+ "scripts": {
21
+ "build": "lzi-builder",
22
+ "build:prod": "NODE_ENV=production bun run build",
23
+ "prepublishOnly": "bun run build:prod",
24
+ "typecheck": "bun run typecheck:app && bun run typecheck:node && bun run typecheck:test",
25
+ "typecheck:app": "lzi-tsgo typecheck tsconfig/app.json",
26
+ "typecheck:node": "lzi-tsgo typecheck tsconfig/node.json",
27
+ "typecheck:test": "lzi-tsgo typecheck tsconfig/test.json",
28
+ "lint": "oxlint && oxfmt --check",
29
+ "lint:fix": "oxlint --fix && oxfmt",
30
+ "test": "bun test",
31
+ "test:watch": "bun test --watch",
32
+ "test:coverage": "bun test --coverage"
33
+ },
34
+ "peerDependencies": {
35
+ "bun": ">=1.0.0"
36
+ }
37
+ }