@minar-kotonoha/vite-plugin-bootstrap 0.1.1

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,34 @@
1
+ # @minar-kotonoha/vite-plugin-bootstrap
2
+
3
+ 虚拟模块 `virtual:bootstrap` — 自动发现项目的 `index.ts` 默认导出,将 Vue App 实例作为参数传入。
4
+
5
+ ## 用法
6
+
7
+ ```ts
8
+ // vite.config.ts
9
+ import { bootstrapPlugin } from '@minar-kotonoha/vite-plugin-bootstrap'
10
+
11
+ export default defineConfig({
12
+ plugins: [bootstrapPlugin()] // 默认读取项目根 index.ts
13
+ })
14
+ ```
15
+
16
+ 可通过 `entryFile` 参数指定自定义启动入口:
17
+
18
+ ```ts
19
+ plugins: [bootstrapPlugin('app.ts')] // 读取 app.ts 而非 index.ts
20
+ ```
21
+
22
+ 在项目根目录的入口文件(默认 `index.ts`)中:
23
+
24
+ ```ts
25
+ import type { App } from 'vue'
26
+ import type { Router } from 'vue-router'
27
+
28
+ export default (app: App, router: Router) => {
29
+ // 在此处初始化你的应用
30
+ app.use(createPinia())
31
+ }
32
+ ```
33
+
34
+ 插件会将 `virtual:bootstrap` 解析为该默认导出,这样 `template/main.ts` 就可以通过 `import { bootstrap } from 'virtual:bootstrap'` 加载,无需硬编码项目路径。
package/index.ts ADDED
@@ -0,0 +1,32 @@
1
+ import { resolve } from 'node:path'
2
+ import { exists } from 'fs-extra'
3
+ import type { PluginOption } from 'vite'
4
+
5
+ const virtualModuleId = 'virtual:bootstrap'
6
+
7
+ /**
8
+ * virtual:bootstrap 插件
9
+ *
10
+ * 搜索项目根目录的启动文件(默认 index.ts)的默认导出,
11
+ * 将 Vue App 实例作为参数传入。
12
+ *
13
+ * @param entryFile 启动入口文件名(相对于 cwd),默认 'index.ts'
14
+ */
15
+ export function bootstrapPlugin(entryFile?: string) {
16
+ const entry = entryFile || 'index.ts'
17
+ return {
18
+ name: '@minar-kotonoha/vite-plugin-bootstrap',
19
+ resolveId(id: string, importer: string | undefined) {
20
+ if (importer && id === virtualModuleId) {
21
+ return `\0${id}`
22
+ }
23
+ },
24
+ load: async (id: string) => {
25
+ if (id.startsWith(`\0${virtualModuleId}`)) {
26
+ return (await exists(resolve(process.cwd(), entry)))
27
+ ? `import * as Index from '@/index';export const bootstrap = Index.default ?? (() => {});`
28
+ : `export const bootstrap = () =>{};`
29
+ }
30
+ },
31
+ } satisfies PluginOption
32
+ }
package/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@minar-kotonoha/vite-plugin-bootstrap",
3
+ "version": "0.1.1",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": "./index.ts",
7
+ "./package.json": "./package.json"
8
+ },
9
+ "peerDependencies": {
10
+ "vite": "catalog:"
11
+ },
12
+ "dependencies": {
13
+ "fs-extra": "^11.3.6"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public",
17
+ "registry": "https://registry.npmjs.org/"
18
+ }
19
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "esnext",
4
+ "moduleResolution": "bundler",
5
+ "target": "esnext",
6
+ "lib": ["esnext"],
7
+ "strict": true,
8
+ "types": ["node"]
9
+ },
10
+ "include": ["index.ts"]
11
+ }