From d7ee5286bae1cc8be15637f075d696dd2a365e1e Mon Sep 17 00:00:00 2001 From: ruan-cat <1219043956@qq.com> Date: Tue, 22 Oct 2024 10:01:45 +0800 Subject: [PATCH 01/15] =?UTF-8?q?=F0=9F=8E=89=20init(vite-plugin-autogener?= =?UTF-8?q?ation-import-file):=20=E5=88=9D=E5=A7=8B=E5=8C=96=E8=AF=A5?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=E3=80=82=E5=87=86=E5=A4=87=E8=87=AA=E4=B8=BB?= =?UTF-8?q?=E5=86=85=E6=B5=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pnpm-workspace.yaml | 2 + .../core/package.json | 37 +++ .../core/src/index.ts | 267 ++++++++++++++++++ .../core/tsconfig.json | 18 ++ 4 files changed, 324 insertions(+) create mode 100644 vite-plugin-autogeneration-import-file/core/package.json create mode 100644 vite-plugin-autogeneration-import-file/core/src/index.ts create mode 100644 vite-plugin-autogeneration-import-file/core/tsconfig.json diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 8f6d960..364fcb6 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -7,6 +7,8 @@ packages: - "demos/*" - "utils" - "learn-create-compoents-lib/*" + # 在monorepo环境下,测试该依赖包是否存在内部bug?实在不行就自己发包。 + - "vite-plugin-autogeneration-import-file/*" # 配置系列的依赖包 为了实现便捷快速的配置分发 - "configs-package/*" diff --git a/vite-plugin-autogeneration-import-file/core/package.json b/vite-plugin-autogeneration-import-file/core/package.json new file mode 100644 index 0000000..2d0992e --- /dev/null +++ b/vite-plugin-autogeneration-import-file/core/package.json @@ -0,0 +1,37 @@ +{ + "name": "@ruan-cat-test/vite-plugin-autogeneration-import-file", + "private": true, + "version": "0.0.0", + "license": "MIT", + "description": "自动生成类型。工作区内的测试包。", + "type": "module", + "main": "dist/index.cjs", + "module": "dist/index.js", + "types": "dist/index.d.ts", + "exports": { + "import": "./dist/index.js", + "require": "./dist/index.cjs", + "default": "./dist/index.cjs" + }, + "files": [ + "dist" + ], + "scripts": { + "&& cd ./test && npm run dev": "待迁移", + "test": "pnpm run build", + "clean": "rimraf dist", + "build": "pnpm run clean && microbundle --target node -f esm,cjs" + }, + "dependencies": { + "fast-glob": "^3.2.11", + "micromatch": "^4.0.5" + }, + "devDependencies": { + "@types/micromatch": "^4.0.2", + "microbundle": "^0.15.0", + "rimraf": "^3.0.2" + }, + "peerDependencies": { + "vite": ">2.0.2" + } +} \ No newline at end of file diff --git a/vite-plugin-autogeneration-import-file/core/src/index.ts b/vite-plugin-autogeneration-import-file/core/src/index.ts new file mode 100644 index 0000000..efb171a --- /dev/null +++ b/vite-plugin-autogeneration-import-file/core/src/index.ts @@ -0,0 +1,267 @@ +import fg from "fast-glob"; +import micromatch from "micromatch"; +import * as fs from "fs"; +import * as path from "path"; +import { normalizePath } from "vite"; + +interface codeTemplate { + key: string; //标识符 + template: string; //模板 codeTemplate.template里的{{name}}会被替换为name + value?: string; //根据模板自动生成,不可传入 +} +type dirOptions = { + dir: string; //遍历路径 + toFile: string; //写入目标文件地址 + pattern: fg.Pattern | fg.Pattern[]; //匹配规则 参考 fast-glob + options?: fg.Options; //fast-glob 匹配参数 + name?: string | ((fileName: string) => string); //名称 当为字符串时里面的{{name}}会被替换为格式化后的驼峰名称, + nameFunction?: (name: string) => string; //自定义名称函数 + codeTemplates?: codeTemplate[]; //代码模板 + template?: string; //文件模板 会递归循环codeTemplates把template里的codeTemplate.key替换为对应的codeTemplate.value +}[]; + +//path转驼峰变量名并剔除最后的index/Index +export const getName = function ( + fileName: string, + nameTemplate: string | ((name: string) => string) = "{{name}}", +): string { + if (typeof nameTemplate == "function") { + return nameTemplate(fileName); + } + const index = fileName.lastIndexOf("."); + if (index > 0) { + fileName = fileName.slice(0, index); + } + let fileNameArr = nameTemplate + .replace(/\{\{name\}}/g, fileName.replace(/\\/g, "/")) + .replace(/[\/-]/g, "_") + .split("_"); + if (fileNameArr[fileNameArr.length - 1] == "index" || fileNameArr[fileNameArr.length - 1] == "Index") { + fileNameArr.pop(); + } + for (let i = 1, len = fileNameArr.length; i < len; i++) { + fileNameArr[i] = fileNameArr[i].slice(0, 1).toUpperCase() + fileNameArr[i].slice(1); + } + return fileNameArr.join(""); +}; + +//获取导入文件绝对地址 +export const getFileImportPath = function (dir: string, fileName: string): string { + fileName = path.resolve(dir, fileName); + if (fileName.endsWith(".ts")) { + fileName = fileName.slice(0, -3); + } + return fileName; +}; + +//获取导入文件代码 +export const getCode = function ( + dir: string, + fileName: string, + toFile: string, + name: string | ((name: string) => string) = undefined, + codeTemplates: codeTemplate[] = [], +): codeTemplate[] { + const filePath = getFileImportPath(dir, fileName); + let relativePath = normalizePath(path.relative(path.dirname(path.resolve(toFile)), filePath)); + if (!relativePath.startsWith("../")) { + relativePath = "./" + relativePath; + } + fileName = getName(fileName, name); + let codeTemplate: codeTemplate[] = JSON.parse(JSON.stringify(codeTemplates)); + if (!codeTemplate.length) { + codeTemplate.push({ + key: "//code", + template: 'export { default as {{name}} } from "{{path}}"\n', + }); + } + codeTemplate.forEach((item) => { + item.value = item.template.replace(/\{\{name\}\}/g, fileName).replace(new RegExp("{{path}}", "g"), relativePath); + }); + return codeTemplate; +}; + +export function readFileSync( + ...args: Parameters +): ReturnType | undefined { + try { + return fs.readFileSync(...args); + } catch { + return undefined; + } +} + +export const createPlugin = () => { + let isServer = false; + let loadFiles: { fileName: string; name: string; dir: string }[][] = []; + let tmpRemovePaths: string[] = []; + const toFileContents: Map = new Map(); + + const loadPath = async function ( + optionIndex: number, + dir: string, + toFile: string, + pattern: fg.Pattern | fg.Pattern[], + options: fg.Options, + name: string | ((name: string) => string) = undefined, + template: string = "", + codeTemplates: codeTemplate[] = [], + ) { + const entries = await fg(pattern, Object.assign({ cwd: dir, dot: true }, options)); + let str = template ? template : "//当前文件由vite-plugin-autogeneration-import-file自动生成\n//code"; + entries.forEach((fileName) => { + getCode(dir, fileName, toFile, name, codeTemplates).forEach((item) => { + str = str.replace(item.key, item.value + item.key); + }); + loadFiles[optionIndex].push({ + fileName, + name: getName(fileName, name), + dir, + }); + }); + str && fs.writeFileSync(toFile, str); + console.log(`mk ${toFile} success\n`); + }; + + //可由unplugin-vue-components使用 + function resolver(componentInclude: number[], directiveInclude: number[] = []): any[] { + let result = []; + if (componentInclude.length) { + result.push({ + type: "component", + resolve: async (componentName: string) => { + for (const index of componentInclude) { + let componentInfo = loadFiles[index].find(({ name }) => name == componentName); + if (componentInfo) { + return normalizePath(path.resolve(componentInfo.dir, componentInfo.fileName)); + } + } + }, + }); + } + if (directiveInclude.length) { + result.push({ + type: "directive", + resolve: async (directiveName: string) => { + for (const index of directiveInclude) { + let directiveInfo = loadFiles[index].find( + ({ name }) => name == directiveName || name == "V" + directiveName, + ); + if (directiveInfo) { + return normalizePath(path.resolve(directiveInfo.dir, directiveInfo.fileName)); + } + } + }, + }); + } + return result; + } + + function loadPathsPlugin(dirOptions: dirOptions) { + return { + name: "load-path-ts", + configureServer() { + //服务器启动时被调用 + dirOptions.forEach((item, index) => { + isServer = true; + fs.watch(item.dir, { recursive: true }, function (eventType: fs.WatchEventType, fileName: string) { + fileName = normalizePath(fileName); + if (eventType === "rename") { + let str = readFileSync(item.toFile, "utf8") || ""; + let filePath = path.resolve(item.dir, fileName); + if (fs.existsSync(filePath)) { + //存在 + let stat = fs.lstatSync(filePath); + let changeFiles: string[] = []; + let prefix = ""; + if (stat.isFile()) { + if (micromatch.isMatch(fileName, item.pattern)) { + changeFiles = [fileName]; + } + } else if (tmpRemovePaths.length && fs.existsSync(path.resolve(filePath, tmpRemovePaths[0]))) { + //如果是重命名文件夹 + changeFiles = tmpRemovePaths; + prefix = fileName + "/"; + } + changeFiles.forEach((fileName) => { + const code = getCode(item.dir, prefix + fileName, item.toFile, item.name, item.codeTemplates); + code.forEach((codeItem) => { + str = str.replace(codeItem.key, codeItem.value + codeItem.key); + }); + loadFiles[index].push({ + fileName: prefix + fileName, + name: getName(prefix + fileName, item.name), + dir: item.dir, + }); + }); + if (changeFiles.length) { + toFileContents.set(item.toFile, str); + fs.writeFileSync(item.toFile, str); + console.log(item.toFile + " add code"); + } + tmpRemovePaths = []; + } else { + //不存在文件 + let changeFiles: string[] = []; + let delNumber = 0; + loadFiles[index].slice(0).forEach((val, k) => { + if (val.fileName.startsWith(fileName + "/") || val.fileName == fileName) { + const code = getCode(item.dir, val.fileName, item.toFile, item.name, item.codeTemplates); + code.forEach((codeItem) => { + str = str.replace(codeItem.value, ""); + }); + loadFiles[index].splice(k - delNumber, 1); + delNumber++; + changeFiles.push(val.fileName); + } + }); + if (changeFiles.length) { + toFileContents.set(item.toFile, str); + fs.writeFileSync(item.toFile, str); + if (changeFiles[0] !== fileName) { + //代表是文件夹改变 + tmpRemovePaths = changeFiles.map((name) => name.slice(fileName.length + 1)); + } else { + tmpRemovePaths = []; + } + console.log(item.toFile + " remove code"); + } + } + } + }); + }); + }, + async buildStart() { + let proArr: Promise[] = []; + dirOptions.forEach((item, index) => { + loadFiles[index] = []; + proArr.push( + loadPath( + index, + item.dir, + item.toFile, + item.pattern, + item.options || {}, + item.name, + item.template, + item.codeTemplates, + ), + ); + }); + await Promise.allSettled(proArr); + if (isServer) { + dirOptions.forEach((item) => { + toFileContents.set(item.toFile, readFileSync(item.toFile, "utf8") as string); + fs.watch(item.toFile, {}, function (eventType: fs.WatchEventType, fileName: string) { + const content = toFileContents.get(item.toFile); + if (content !== undefined && content !== readFileSync(item.toFile, "utf8")) { + fs.writeFileSync(item.toFile, content); + } + }); + }); + } + }, + }; + } + return { autoImport: loadPathsPlugin, resolver }; +}; diff --git a/vite-plugin-autogeneration-import-file/core/tsconfig.json b/vite-plugin-autogeneration-import-file/core/tsconfig.json new file mode 100644 index 0000000..5e4bb44 --- /dev/null +++ b/vite-plugin-autogeneration-import-file/core/tsconfig.json @@ -0,0 +1,18 @@ +{ + "compilerOptions": { + "module": "NodeNext", + "noImplicitAny": true, + "removeComments": true, + "preserveConstEnums": true, + "sourceMap": true, + "types": [ + "node" + ], + "moduleResolution": "NodeNext", + "allowSyntheticDefaultImports": true, + }, + "include": [ + "src/*.ts", + "test/*.ts" + ], +} \ No newline at end of file From 9498653e5f49a2d382f9fe70845d3af19fc6963f Mon Sep 17 00:00:00 2001 From: ruan-cat <1219043956@qq.com> Date: Tue, 22 Oct 2024 10:15:46 +0800 Subject: [PATCH 02/15] =?UTF-8?q?=F0=9F=8E=89=20init(vite-plugin-autogener?= =?UTF-8?q?ation-import-file-test-app-1):=20=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E5=BA=94=E7=94=A8=E9=A1=B9=E7=9B=AE=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/extensions.json | 2 +- .vscode/settings.json | 2 +- .../commitlint-config/package.json | 2 +- demos/vercel-deploy-tool/package.json | 2 +- package.json | 2 +- packages/docs-01-star/package.json | 2 +- packages/vuepress-preset-config/package.json | 2 +- turbo.json | 22 ++--- utils/package.json | 2 +- utils/turbo.json | 30 ++----- .../app/.gitignore | 7 ++ .../app/README.md | 16 ++++ .../app/index.html | 13 +++ .../app/package.json | 22 +++++ .../app/public/vite.svg | 1 + .../app/src/App.vue | 34 ++++++++ .../app/src/assets/vue.svg | 1 + .../app/src/components/HelloWorld.vue | 37 +++++++++ .../app/src/main.ts | 5 ++ .../app/src/myComponents/test-a.vue | 0 .../app/src/myComponents/test.vue | 1 + .../app/src/myComponents/test/dir/index.vue | 1 + .../app/src/myComponents/test_b.vue | 0 .../app/src/myDirective/focus.ts | 8 ++ .../app/src/resovleComponents/resolve.vue | 1 + .../app/src/store/module.ts | 2 + .../app/src/style.css | 81 +++++++++++++++++++ .../app/tsconfig.json | 34 ++++++++ .../app/tsconfig.node.json | 9 +++ .../app/types/vite-env.d.ts | 7 ++ .../app/vite.config.ts | 73 +++++++++++++++++ .../core/package.json | 2 +- 32 files changed, 375 insertions(+), 48 deletions(-) create mode 100644 vite-plugin-autogeneration-import-file/app/.gitignore create mode 100644 vite-plugin-autogeneration-import-file/app/README.md create mode 100644 vite-plugin-autogeneration-import-file/app/index.html create mode 100644 vite-plugin-autogeneration-import-file/app/package.json create mode 100644 vite-plugin-autogeneration-import-file/app/public/vite.svg create mode 100644 vite-plugin-autogeneration-import-file/app/src/App.vue create mode 100644 vite-plugin-autogeneration-import-file/app/src/assets/vue.svg create mode 100644 vite-plugin-autogeneration-import-file/app/src/components/HelloWorld.vue create mode 100644 vite-plugin-autogeneration-import-file/app/src/main.ts create mode 100644 vite-plugin-autogeneration-import-file/app/src/myComponents/test-a.vue create mode 100644 vite-plugin-autogeneration-import-file/app/src/myComponents/test.vue create mode 100644 vite-plugin-autogeneration-import-file/app/src/myComponents/test/dir/index.vue create mode 100644 vite-plugin-autogeneration-import-file/app/src/myComponents/test_b.vue create mode 100644 vite-plugin-autogeneration-import-file/app/src/myDirective/focus.ts create mode 100644 vite-plugin-autogeneration-import-file/app/src/resovleComponents/resolve.vue create mode 100644 vite-plugin-autogeneration-import-file/app/src/store/module.ts create mode 100644 vite-plugin-autogeneration-import-file/app/src/style.css create mode 100644 vite-plugin-autogeneration-import-file/app/tsconfig.json create mode 100644 vite-plugin-autogeneration-import-file/app/tsconfig.node.json create mode 100644 vite-plugin-autogeneration-import-file/app/types/vite-env.d.ts create mode 100644 vite-plugin-autogeneration-import-file/app/vite.config.ts diff --git a/.vscode/extensions.json b/.vscode/extensions.json index 762e62d..f63d623 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -11,4 +11,4 @@ "Spades.vs-picgo" ], "unwantedRecommendations": [] -} \ No newline at end of file +} diff --git a/.vscode/settings.json b/.vscode/settings.json index ebe28ef..b50e404 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -97,4 +97,4 @@ // https://nodejs.org/en/learn/typescript/run#registering-tsx-via-node "typescript": "pnpm dotenvx run -- node --import=tsx $fullFileName" } -} \ No newline at end of file +} diff --git a/configs-package/commitlint-config/package.json b/configs-package/commitlint-config/package.json index 1468fff..a0cda09 100644 --- a/configs-package/commitlint-config/package.json +++ b/configs-package/commitlint-config/package.json @@ -51,4 +51,4 @@ "peerDependencies": { "typescript": "catalog:" } -} \ No newline at end of file +} diff --git a/demos/vercel-deploy-tool/package.json b/demos/vercel-deploy-tool/package.json index 28f5b8a..c9a3045 100644 --- a/demos/vercel-deploy-tool/package.json +++ b/demos/vercel-deploy-tool/package.json @@ -71,4 +71,4 @@ "@vitest/ui": "^2.0.5", "vitest": "^2.0.5" } -} \ No newline at end of file +} diff --git a/package.json b/package.json index fcf86cd..4397e51 100644 --- a/package.json +++ b/package.json @@ -106,4 +106,4 @@ "consola": "^3.2.3", "lodash-es": "catalog:" } -} \ No newline at end of file +} diff --git a/packages/docs-01-star/package.json b/packages/docs-01-star/package.json index 121faba..14361b6 100644 --- a/packages/docs-01-star/package.json +++ b/packages/docs-01-star/package.json @@ -18,4 +18,4 @@ "vitepress": "^1.3.0", "vue": "^3.4.30" } -} \ No newline at end of file +} diff --git a/packages/vuepress-preset-config/package.json b/packages/vuepress-preset-config/package.json index 1732097..78636d9 100644 --- a/packages/vuepress-preset-config/package.json +++ b/packages/vuepress-preset-config/package.json @@ -68,4 +68,4 @@ "vuepress-plugin-search-pro": "2.0.0-rc.57", "vuepress-theme-hope": "2.0.0-rc.58" } -} \ No newline at end of file +} diff --git a/turbo.json b/turbo.json index f847425..c66edd8 100644 --- a/turbo.json +++ b/turbo.json @@ -2,14 +2,10 @@ "$schema": "https://turbo.build/schema.json", "tasks": { "build": { - "dependsOn": [ - "^build" - ] + "dependsOn": ["^build"] }, "build:docs": { - "dependsOn": [ - "^build" - ] + "dependsOn": ["^build"] }, "docs:update-package": { "dependsOn": [] @@ -18,19 +14,13 @@ "dependsOn": [] }, "clear": { - "dependsOn": [ - "^clear" - ] + "dependsOn": ["^clear"] }, "//#changeset:publish": { - "dependsOn": [ - "^build" - ] + "dependsOn": ["^build"] }, "//#do-publish": { - "dependsOn": [ - "//#changeset:publish" - ] + "dependsOn": ["//#changeset:publish"] } } -} \ No newline at end of file +} diff --git a/utils/package.json b/utils/package.json index 90b4f9d..dcf0d77 100644 --- a/utils/package.json +++ b/utils/package.json @@ -74,4 +74,4 @@ "optional": true } } -} \ No newline at end of file +} diff --git a/utils/turbo.json b/utils/turbo.json index 5063710..19281d3 100644 --- a/utils/turbo.json +++ b/utils/turbo.json @@ -1,7 +1,5 @@ { - "extends": [ - "//" - ], + "extends": ["//"], "tasks": { "copy-readme": { "cache": false @@ -13,33 +11,19 @@ "cache": false }, "generate-md": { - "dependsOn": [ - "copy-readme", - "copy-changelog", - "typedoc" - ] + "dependsOn": ["copy-readme", "copy-changelog", "typedoc"] }, "docs:dev-main": { - "dependsOn": [ - "build", - "generate-md" - ] + "dependsOn": ["build", "generate-md"] }, "docs:dev": { - "dependsOn": [ - "docs:dev-main" - ] + "dependsOn": ["docs:dev-main"] }, "build:docs-main": { - "dependsOn": [ - "build", - "generate-md" - ] + "dependsOn": ["build", "generate-md"] }, "do-build-docs": { - "dependsOn": [ - "build:docs-main" - ] + "dependsOn": ["build:docs-main"] } } -} \ No newline at end of file +} diff --git a/vite-plugin-autogeneration-import-file/app/.gitignore b/vite-plugin-autogeneration-import-file/app/.gitignore new file mode 100644 index 0000000..af92c9e --- /dev/null +++ b/vite-plugin-autogeneration-import-file/app/.gitignore @@ -0,0 +1,7 @@ + + +# 自动生成的类型文件 都不参与提交 +*.d.ts + +# 垫片类型文件 需要提交至仓库 +!vite-env.d.ts \ No newline at end of file diff --git a/vite-plugin-autogeneration-import-file/app/README.md b/vite-plugin-autogeneration-import-file/app/README.md new file mode 100644 index 0000000..30b15e2 --- /dev/null +++ b/vite-plugin-autogeneration-import-file/app/README.md @@ -0,0 +1,16 @@ +# Vue 3 + TypeScript + Vite + +This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 ` + + diff --git a/vite-plugin-autogeneration-import-file/app/package.json b/vite-plugin-autogeneration-import-file/app/package.json new file mode 100644 index 0000000..ebb9e57 --- /dev/null +++ b/vite-plugin-autogeneration-import-file/app/package.json @@ -0,0 +1,22 @@ +{ + "name": "@ruan-cat-test/vite-plugin-autogeneration-import-file-test-app-1", + "private": true, + "version": "0.0.0", + "type": "module", + "scripts": { + "dev": "vite", + "build": " vite build", + "preview": "vite preview", + "vite-plugin-autogeneration-import-file": "file:../.." + }, + "dependencies": { + "vue": "^3.5.12" + }, + "devDependencies": { + "@vitejs/plugin-vue": "^5.1.4", + "typescript": "catalog:", + "unplugin-vue-components": "^0.27.4", + "vite": "^5.4.9", + "vue-tsc": "^2.1.6" + } +} \ No newline at end of file diff --git a/vite-plugin-autogeneration-import-file/app/public/vite.svg b/vite-plugin-autogeneration-import-file/app/public/vite.svg new file mode 100644 index 0000000..e7b8dfb --- /dev/null +++ b/vite-plugin-autogeneration-import-file/app/public/vite.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/vite-plugin-autogeneration-import-file/app/src/App.vue b/vite-plugin-autogeneration-import-file/app/src/App.vue new file mode 100644 index 0000000..f1f3b4b --- /dev/null +++ b/vite-plugin-autogeneration-import-file/app/src/App.vue @@ -0,0 +1,34 @@ + + + + + diff --git a/vite-plugin-autogeneration-import-file/app/src/assets/vue.svg b/vite-plugin-autogeneration-import-file/app/src/assets/vue.svg new file mode 100644 index 0000000..770e9d3 --- /dev/null +++ b/vite-plugin-autogeneration-import-file/app/src/assets/vue.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/vite-plugin-autogeneration-import-file/app/src/components/HelloWorld.vue b/vite-plugin-autogeneration-import-file/app/src/components/HelloWorld.vue new file mode 100644 index 0000000..137bcca --- /dev/null +++ b/vite-plugin-autogeneration-import-file/app/src/components/HelloWorld.vue @@ -0,0 +1,37 @@ + + + + + diff --git a/vite-plugin-autogeneration-import-file/app/src/main.ts b/vite-plugin-autogeneration-import-file/app/src/main.ts new file mode 100644 index 0000000..3c9bfeb --- /dev/null +++ b/vite-plugin-autogeneration-import-file/app/src/main.ts @@ -0,0 +1,5 @@ +import { createApp } from "vue"; +import "./style.css"; +import App from "./App.vue"; + +createApp(App).mount("#app"); diff --git a/vite-plugin-autogeneration-import-file/app/src/myComponents/test-a.vue b/vite-plugin-autogeneration-import-file/app/src/myComponents/test-a.vue new file mode 100644 index 0000000..e69de29 diff --git a/vite-plugin-autogeneration-import-file/app/src/myComponents/test.vue b/vite-plugin-autogeneration-import-file/app/src/myComponents/test.vue new file mode 100644 index 0000000..e598902 --- /dev/null +++ b/vite-plugin-autogeneration-import-file/app/src/myComponents/test.vue @@ -0,0 +1 @@ + diff --git a/vite-plugin-autogeneration-import-file/app/src/myComponents/test/dir/index.vue b/vite-plugin-autogeneration-import-file/app/src/myComponents/test/dir/index.vue new file mode 100644 index 0000000..db83f3d --- /dev/null +++ b/vite-plugin-autogeneration-import-file/app/src/myComponents/test/dir/index.vue @@ -0,0 +1 @@ + diff --git a/vite-plugin-autogeneration-import-file/app/src/myComponents/test_b.vue b/vite-plugin-autogeneration-import-file/app/src/myComponents/test_b.vue new file mode 100644 index 0000000..e69de29 diff --git a/vite-plugin-autogeneration-import-file/app/src/myDirective/focus.ts b/vite-plugin-autogeneration-import-file/app/src/myDirective/focus.ts new file mode 100644 index 0000000..bb3f813 --- /dev/null +++ b/vite-plugin-autogeneration-import-file/app/src/myDirective/focus.ts @@ -0,0 +1,8 @@ +const focus = { + mounted: (el: HTMLElement) => { + console.log(111, el); + el.focus(); + }, +}; + +export default focus; diff --git a/vite-plugin-autogeneration-import-file/app/src/resovleComponents/resolve.vue b/vite-plugin-autogeneration-import-file/app/src/resovleComponents/resolve.vue new file mode 100644 index 0000000..19d0589 --- /dev/null +++ b/vite-plugin-autogeneration-import-file/app/src/resovleComponents/resolve.vue @@ -0,0 +1 @@ + diff --git a/vite-plugin-autogeneration-import-file/app/src/store/module.ts b/vite-plugin-autogeneration-import-file/app/src/store/module.ts new file mode 100644 index 0000000..f928c27 --- /dev/null +++ b/vite-plugin-autogeneration-import-file/app/src/store/module.ts @@ -0,0 +1,2 @@ +//当前文件由vite-plugin-autogeneration-import-file自动生成 +//code diff --git a/vite-plugin-autogeneration-import-file/app/src/style.css b/vite-plugin-autogeneration-import-file/app/src/style.css new file mode 100644 index 0000000..8b105ad --- /dev/null +++ b/vite-plugin-autogeneration-import-file/app/src/style.css @@ -0,0 +1,81 @@ +:root { + font-family: Inter, Avenir, Helvetica, Arial, sans-serif; + font-size: 16px; + line-height: 24px; + font-weight: 400; + + color-scheme: light dark; + color: rgba(255, 255, 255, 0.87); + background-color: #242424; + + font-synthesis: none; + text-rendering: optimizeLegibility; + -webkit-font-smoothing: antialiased; + -moz-osx-font-smoothing: grayscale; + -webkit-text-size-adjust: 100%; +} + +a { + font-weight: 500; + color: #646cff; + text-decoration: inherit; +} +a:hover { + color: #535bf2; +} + +body { + margin: 0; + display: flex; + place-items: center; + min-width: 320px; + min-height: 100vh; +} + +h1 { + font-size: 3.2em; + line-height: 1.1; +} + +button { + border-radius: 8px; + border: 1px solid transparent; + padding: 0.6em 1.2em; + font-size: 1em; + font-weight: 500; + font-family: inherit; + background-color: #1a1a1a; + cursor: pointer; + transition: border-color 0.25s; +} +button:hover { + border-color: #646cff; +} +button:focus, +button:focus-visible { + outline: 4px auto -webkit-focus-ring-color; +} + +.card { + padding: 2em; +} + +#app { + max-width: 1280px; + margin: 0 auto; + padding: 2rem; + text-align: center; +} + +@media (prefers-color-scheme: light) { + :root { + color: #213547; + background-color: #ffffff; + } + a:hover { + color: #747bff; + } + button { + background-color: #f9f9f9; + } +} diff --git a/vite-plugin-autogeneration-import-file/app/tsconfig.json b/vite-plugin-autogeneration-import-file/app/tsconfig.json new file mode 100644 index 0000000..81e72bb --- /dev/null +++ b/vite-plugin-autogeneration-import-file/app/tsconfig.json @@ -0,0 +1,34 @@ +{ + "compilerOptions": { + "target": "ESNext", + "useDefineForClassFields": true, + "module": "ESNext", + "moduleResolution": "Node", + "strict": true, + "jsx": "preserve", + "sourceMap": true, + "resolveJsonModule": true, + "isolatedModules": true, + "esModuleInterop": true, + "lib": [ + "ESNext", + "DOM" + ], + "skipLibCheck": true + }, + "include": [ + "src/**/*.ts", + "src/**/*.d.ts", + "src/**/*.tsx", + "src/**/*.vue", + "myComponents.d.ts", + "myDirective.d.ts", + "components1.d.ts", + "types/vite-env.d.ts" + ], + "references": [ + { + "path": "./tsconfig.node.json" + } + ] +} \ No newline at end of file diff --git a/vite-plugin-autogeneration-import-file/app/tsconfig.node.json b/vite-plugin-autogeneration-import-file/app/tsconfig.node.json new file mode 100644 index 0000000..9d31e2a --- /dev/null +++ b/vite-plugin-autogeneration-import-file/app/tsconfig.node.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "composite": true, + "module": "ESNext", + "moduleResolution": "Node", + "allowSyntheticDefaultImports": true + }, + "include": ["vite.config.ts"] +} diff --git a/vite-plugin-autogeneration-import-file/app/types/vite-env.d.ts b/vite-plugin-autogeneration-import-file/app/types/vite-env.d.ts new file mode 100644 index 0000000..23dde32 --- /dev/null +++ b/vite-plugin-autogeneration-import-file/app/types/vite-env.d.ts @@ -0,0 +1,7 @@ +/// + +declare module "*.vue" { + import type { DefineComponent } from "vue"; + const component: DefineComponent<{}, {}, any>; + export default component; +} diff --git a/vite-plugin-autogeneration-import-file/app/vite.config.ts b/vite-plugin-autogeneration-import-file/app/vite.config.ts new file mode 100644 index 0000000..cb44ced --- /dev/null +++ b/vite-plugin-autogeneration-import-file/app/vite.config.ts @@ -0,0 +1,73 @@ +import { defineConfig } from "vite"; +import vue from "@vitejs/plugin-vue"; +import Components from "unplugin-vue-components/vite"; +import { getName, createPlugin } from "vite-plugin-autogeneration-import-file"; +const { autoImport, resolver } = createPlugin(); + +// https://vitejs.dev/config/ +export default defineConfig({ + plugins: [ + vue(), + autoImport([ + { + pattern: ["**/{index.vue,index.ts,index.js}", "*.{vue,ts,js}"], + dir: "./src/components", + toFile: "./components1.d.ts", + template: ` +import '@vue/runtime-core' +export {} +declare module '@vue/runtime-core' { + export interface GlobalComponents { + //import code + } +}`, + name: "_{{name}}", + codeTemplates: [{ key: "//import code", template: '{{name}}: typeof import("{{path}}")["default"]\n ' }], + }, + + { + pattern: ["**/*.{ts,js}", "*.{ts,js}"], + dir: "./src/store/modules", + toFile: "./src/store/module.ts", + name: (name) => { + name = getName(name); + return name[0].toUpperCase() + name.slice(1) + "Store"; + }, + }, + + { + pattern: ["**/{index.vue,index.ts,index.js}", "*.{vue,ts,js}"], + dir: "./src/myComponents", + toFile: "./myComponents.d.ts", + template: ` +import '@vue/runtime-core' +export {} +declare module '@vue/runtime-core' { + export interface GlobalComponents { + //import code + } +}`, + name: "_{{name}}", + codeTemplates: [{ key: "//import code", template: '{{name}}: typeof import("{{path}}")["default"]\n ' }], + }, + + { + pattern: ["**/{index.vue,index.ts,index.js}", "*.{vue,ts,js}"], + dir: "./src/myDirective", + toFile: "./myDirective.d.ts", + template: ` +import '@vue/runtime-core' +export {} +declare module '@vue/runtime-core' { + export interface ComponentCustomProperties { + //import code + } +}`, + name: "V_{{name}}", + codeTemplates: [{ key: "//import code", template: '{{name}}: typeof import("{{path}}")["default"]\n ' }], + }, + ]), + + Components({ dirs: [], dts: false, resolvers: [resolver([0, 2], [3])] }), + ], +}); diff --git a/vite-plugin-autogeneration-import-file/core/package.json b/vite-plugin-autogeneration-import-file/core/package.json index 2d0992e..32bd05e 100644 --- a/vite-plugin-autogeneration-import-file/core/package.json +++ b/vite-plugin-autogeneration-import-file/core/package.json @@ -34,4 +34,4 @@ "peerDependencies": { "vite": ">2.0.2" } -} \ No newline at end of file +} From b34bdde1b71ccf5dc671193f65b5eb174fb87ce8 Mon Sep 17 00:00:00 2001 From: ruan-cat <1219043956@qq.com> Date: Tue, 22 Oct 2024 10:30:21 +0800 Subject: [PATCH 03/15] =?UTF-8?q?=F0=9F=94=A7=20build(vite-plugin-autogene?= =?UTF-8?q?ration-import-file-test-app-1):=20=E5=AE=89=E8=A3=85=E5=B7=A5?= =?UTF-8?q?=E4=BD=9C=E5=8C=BA=E4=BE=9D=E8=B5=96=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/package.json | 2 ++ .../app/vite.config.ts | 11 +++++++++-- .../core/package.json | 5 +++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/vite-plugin-autogeneration-import-file/app/package.json b/vite-plugin-autogeneration-import-file/app/package.json index ebb9e57..89dc5fa 100644 --- a/vite-plugin-autogeneration-import-file/app/package.json +++ b/vite-plugin-autogeneration-import-file/app/package.json @@ -7,12 +7,14 @@ "dev": "vite", "build": " vite build", "preview": "vite preview", + "rm:node_modules": "rimraf node_modules", "vite-plugin-autogeneration-import-file": "file:../.." }, "dependencies": { "vue": "^3.5.12" }, "devDependencies": { + "@ruan-cat-test/vite-plugin-autogeneration-import-file": "workspace:^", "@vitejs/plugin-vue": "^5.1.4", "typescript": "catalog:", "unplugin-vue-components": "^0.27.4", diff --git a/vite-plugin-autogeneration-import-file/app/vite.config.ts b/vite-plugin-autogeneration-import-file/app/vite.config.ts index cb44ced..72c6a6e 100644 --- a/vite-plugin-autogeneration-import-file/app/vite.config.ts +++ b/vite-plugin-autogeneration-import-file/app/vite.config.ts @@ -1,13 +1,14 @@ import { defineConfig } from "vite"; import vue from "@vitejs/plugin-vue"; import Components from "unplugin-vue-components/vite"; -import { getName, createPlugin } from "vite-plugin-autogeneration-import-file"; +import { getName, createPlugin } from "@ruan-cat-test/vite-plugin-autogeneration-import-file"; const { autoImport, resolver } = createPlugin(); // https://vitejs.dev/config/ export default defineConfig({ plugins: [ vue(), + autoImport([ { pattern: ["**/{index.vue,index.ts,index.js}", "*.{vue,ts,js}"], @@ -68,6 +69,12 @@ declare module '@vue/runtime-core' { }, ]), - Components({ dirs: [], dts: false, resolvers: [resolver([0, 2], [3])] }), + Components({ + dirs: [], + dts: false, + resolvers: [ + // resolver([0, 2], [3]) + ], + }), ], }); diff --git a/vite-plugin-autogeneration-import-file/core/package.json b/vite-plugin-autogeneration-import-file/core/package.json index 32bd05e..213aaef 100644 --- a/vite-plugin-autogeneration-import-file/core/package.json +++ b/vite-plugin-autogeneration-import-file/core/package.json @@ -20,7 +20,8 @@ "&& cd ./test && npm run dev": "待迁移", "test": "pnpm run build", "clean": "rimraf dist", - "build": "pnpm run clean && microbundle --target node -f esm,cjs" + "build": "pnpm run clean && microbundle --target node -f esm,cjs", + "rm:node_modules": "rimraf node_modules" }, "dependencies": { "fast-glob": "^3.2.11", @@ -34,4 +35,4 @@ "peerDependencies": { "vite": ">2.0.2" } -} +} \ No newline at end of file From 89c7a14f8d28dda4ad614b63be2e7a618db23a7f Mon Sep 17 00:00:00 2001 From: ruan-cat <1219043956@qq.com> Date: Tue, 22 Oct 2024 10:31:02 +0800 Subject: [PATCH 04/15] =?UTF-8?q?=F0=9F=94=A7=20build:=20=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E5=8D=87=E7=BA=A7=E4=BE=9D=E8=B5=96=E5=8C=85=E3=80=82?= =?UTF-8?q?=20pnpm=20-r=20up=20rimraf@latest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demos/vercel-deploy-tool/package.json | 2 +- package.json | 2 +- vite-plugin-autogeneration-import-file/core/package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/demos/vercel-deploy-tool/package.json b/demos/vercel-deploy-tool/package.json index c9a3045..c084dc6 100644 --- a/demos/vercel-deploy-tool/package.json +++ b/demos/vercel-deploy-tool/package.json @@ -59,7 +59,7 @@ "lodash-es": "catalog:", "mkdirp": "^3.0.1", "pathe": "^1.1.2", - "rimraf": "^5.0.7", + "rimraf": "^6.0.1", "shx": "^0.3.4", "vercel": "^34.2.7" }, diff --git a/package.json b/package.json index 4397e51..36aa780 100644 --- a/package.json +++ b/package.json @@ -84,7 +84,7 @@ "pkg-types": "^1.2.1", "prettier": "^3.3.3", "prettier-plugin-lint-md": "^1.0.1", - "rimraf": "^5.0.10", + "rimraf": "^6.0.1", "shx": "^0.3.4", "tsx": "^4.19.1", "turbo": "^2.1.3", diff --git a/vite-plugin-autogeneration-import-file/core/package.json b/vite-plugin-autogeneration-import-file/core/package.json index 213aaef..7b6e198 100644 --- a/vite-plugin-autogeneration-import-file/core/package.json +++ b/vite-plugin-autogeneration-import-file/core/package.json @@ -30,7 +30,7 @@ "devDependencies": { "@types/micromatch": "^4.0.2", "microbundle": "^0.15.0", - "rimraf": "^3.0.2" + "rimraf": "^6.0.1" }, "peerDependencies": { "vite": ">2.0.2" From 85b0b23ef04304a75e0e0c308227e32731b7ee2f Mon Sep 17 00:00:00 2001 From: ruan-cat <1219043956@qq.com> Date: Tue, 22 Oct 2024 10:45:14 +0800 Subject: [PATCH 05/15] =?UTF-8?q?=E2=9A=99=EF=B8=8F=20config(vite-plugin-a?= =?UTF-8?q?utogeneration-import-file-test-app-1):=20=E5=87=86=E5=A4=87?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E8=BF=90=E8=A1=8C=E5=89=8D=E7=9A=84turbo?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E6=8F=92=E4=BB=B6=E5=91=BD=E4=BB=A4=EF=BC=8C?= =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BB=BB=E5=8A=A1=E8=B0=83=E5=BA=A6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/package.json | 5 ++-- .../app/turbo.json | 13 ++++++++++ .../app/vite.config.ts | 24 ++++++++++++------- .../core/package.json | 1 - 4 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 vite-plugin-autogeneration-import-file/app/turbo.json diff --git a/vite-plugin-autogeneration-import-file/app/package.json b/vite-plugin-autogeneration-import-file/app/package.json index 89dc5fa..aec1878 100644 --- a/vite-plugin-autogeneration-import-file/app/package.json +++ b/vite-plugin-autogeneration-import-file/app/package.json @@ -3,12 +3,13 @@ "private": true, "version": "0.0.0", "type": "module", + "description": " `vite-plugin-autogeneration-import-file` 的测试应用。 ", "scripts": { "dev": "vite", "build": " vite build", "preview": "vite preview", - "rm:node_modules": "rimraf node_modules", - "vite-plugin-autogeneration-import-file": "file:../.." + "predev": "turbo do-plugin-build", + "rm:node_modules": "rimraf node_modules" }, "dependencies": { "vue": "^3.5.12" diff --git a/vite-plugin-autogeneration-import-file/app/turbo.json b/vite-plugin-autogeneration-import-file/app/turbo.json new file mode 100644 index 0000000..7532f42 --- /dev/null +++ b/vite-plugin-autogeneration-import-file/app/turbo.json @@ -0,0 +1,13 @@ +{ + "extends": [ + "//" + ], + "tasks": { + "do-plugin-build": { + "cache": false, + "dependsOn": [ + "@ruan-cat-test/vite-plugin-autogeneration-import-file#build" + ] + } + } +} \ No newline at end of file diff --git a/vite-plugin-autogeneration-import-file/app/vite.config.ts b/vite-plugin-autogeneration-import-file/app/vite.config.ts index 72c6a6e..1b5e563 100644 --- a/vite-plugin-autogeneration-import-file/app/vite.config.ts +++ b/vite-plugin-autogeneration-import-file/app/vite.config.ts @@ -10,6 +10,7 @@ export default defineConfig({ vue(), autoImport([ + // components { pattern: ["**/{index.vue,index.ts,index.js}", "*.{vue,ts,js}"], dir: "./src/components", @@ -26,16 +27,18 @@ declare module '@vue/runtime-core' { codeTemplates: [{ key: "//import code", template: '{{name}}: typeof import("{{path}}")["default"]\n ' }], }, - { - pattern: ["**/*.{ts,js}", "*.{ts,js}"], - dir: "./src/store/modules", - toFile: "./src/store/module.ts", - name: (name) => { - name = getName(name); - return name[0].toUpperCase() + name.slice(1) + "Store"; - }, - }, + // module + // { + // pattern: ["**/*.{ts,js}", "*.{ts,js}"], + // dir: "./src/store/modules", + // toFile: "./src/store/module.ts", + // name: (name) => { + // name = getName(name); + // return name[0].toUpperCase() + name.slice(1) + "Store"; + // }, + // }, + // myComponents { pattern: ["**/{index.vue,index.ts,index.js}", "*.{vue,ts,js}"], dir: "./src/myComponents", @@ -52,6 +55,7 @@ declare module '@vue/runtime-core' { codeTemplates: [{ key: "//import code", template: '{{name}}: typeof import("{{path}}")["default"]\n ' }], }, + // myDirective { pattern: ["**/{index.vue,index.ts,index.js}", "*.{vue,ts,js}"], dir: "./src/myDirective", @@ -69,6 +73,8 @@ declare module '@vue/runtime-core' { }, ]), + // FIXME: 莫名其妙地称类型不匹配 需要修复 + // @ts-ignore Components({ dirs: [], dts: false, diff --git a/vite-plugin-autogeneration-import-file/core/package.json b/vite-plugin-autogeneration-import-file/core/package.json index 7b6e198..8add4f4 100644 --- a/vite-plugin-autogeneration-import-file/core/package.json +++ b/vite-plugin-autogeneration-import-file/core/package.json @@ -17,7 +17,6 @@ "dist" ], "scripts": { - "&& cd ./test && npm run dev": "待迁移", "test": "pnpm run build", "clean": "rimraf dist", "build": "pnpm run clean && microbundle --target node -f esm,cjs", From 621ebe72c930a7947d94329d46f4290f056ea20b Mon Sep 17 00:00:00 2001 From: ruan-cat <1219043956@qq.com> Date: Tue, 22 Oct 2024 10:55:01 +0800 Subject: [PATCH 06/15] =?UTF-8?q?=F0=9F=90=8E=20ci:=20=E6=8F=90=E4=BE=9B?= =?UTF-8?q?=E7=AE=80=E5=8D=95=E7=9A=84=E5=88=A0=E9=99=A4turbo=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E7=9A=84=E5=91=BD=E4=BB=A4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vite-plugin-autogeneration-import-file/app/package.json | 3 ++- vite-plugin-autogeneration-import-file/core/package.json | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/vite-plugin-autogeneration-import-file/app/package.json b/vite-plugin-autogeneration-import-file/app/package.json index aec1878..5194805 100644 --- a/vite-plugin-autogeneration-import-file/app/package.json +++ b/vite-plugin-autogeneration-import-file/app/package.json @@ -9,7 +9,8 @@ "build": " vite build", "preview": "vite preview", "predev": "turbo do-plugin-build", - "rm:node_modules": "rimraf node_modules" + "rm:node_modules": "rimraf node_modules", + "clean": "rimraf dist .turbo" }, "dependencies": { "vue": "^3.5.12" diff --git a/vite-plugin-autogeneration-import-file/core/package.json b/vite-plugin-autogeneration-import-file/core/package.json index 8add4f4..a4a0a8a 100644 --- a/vite-plugin-autogeneration-import-file/core/package.json +++ b/vite-plugin-autogeneration-import-file/core/package.json @@ -18,7 +18,7 @@ ], "scripts": { "test": "pnpm run build", - "clean": "rimraf dist", + "clean": "rimraf dist .turbo", "build": "pnpm run clean && microbundle --target node -f esm,cjs", "rm:node_modules": "rimraf node_modules" }, From d2faba9d3a7fed126a63826518b4a395ace90196 Mon Sep 17 00:00:00 2001 From: ruan-cat <1219043956@qq.com> Date: Tue, 22 Oct 2024 11:29:46 +0800 Subject: [PATCH 07/15] =?UTF-8?q?=F0=9F=90=8E=20ci(vite-plugin-autogenerat?= =?UTF-8?q?ion-import-file-test-app-1):=20=E6=8B=93=E5=B1=95=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=91=BD=E4=BB=A4=EF=BC=8C=E6=8F=90=E4=BE=9B=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E7=B1=BB=E5=9E=8B=E6=96=87=E4=BB=B6=E7=9A=84=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=EF=BC=9B=E6=8B=93=E5=B1=95turbo=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=B0=83=E5=BA=A6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 3 +- .../app/package.json | 3 +- .../app/scripts/del-types.ts | 50 +++++++++++++++++++ .../app/turbo.json | 7 +++ .../core/package.json | 3 +- 5 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 vite-plugin-autogeneration-import-file/app/scripts/del-types.ts diff --git a/package.json b/package.json index 36aa780..ea4e2fb 100644 --- a/package.json +++ b/package.json @@ -62,10 +62,12 @@ "@types/node": "^20.16.11", "@vuepress/bundler-vite": "2.0.0-rc.18", "commitizen": "^4.3.1", + "consola": "^3.2.3", "cpx2": "^7.0.2", "cross-env": "^7.0.3", "cz-git": "^1.10.1", "czg": "^1.10.1", + "del": "^8.0.0", "esbuild": "^0.23.1", "esbuild-register": "^3.6.0", "eslint": "^9.12.0", @@ -103,7 +105,6 @@ }, "dependencies": { "@dotenvx/dotenvx": "^1.17.0", - "consola": "^3.2.3", "lodash-es": "catalog:" } } diff --git a/vite-plugin-autogeneration-import-file/app/package.json b/vite-plugin-autogeneration-import-file/app/package.json index 5194805..7e902b3 100644 --- a/vite-plugin-autogeneration-import-file/app/package.json +++ b/vite-plugin-autogeneration-import-file/app/package.json @@ -8,8 +8,9 @@ "dev": "vite", "build": " vite build", "preview": "vite preview", - "predev": "turbo do-plugin-build", + "predev": "turbo do-predev", "rm:node_modules": "rimraf node_modules", + "rm:types": "node --import=tsx ./scripts/del-types.ts", "clean": "rimraf dist .turbo" }, "dependencies": { diff --git a/vite-plugin-autogeneration-import-file/app/scripts/del-types.ts b/vite-plugin-autogeneration-import-file/app/scripts/del-types.ts new file mode 100644 index 0000000..ece50e0 --- /dev/null +++ b/vite-plugin-autogeneration-import-file/app/scripts/del-types.ts @@ -0,0 +1,50 @@ +// 删除多余的类型 + +import { deleteAsync } from "del"; +import { consola } from "consola"; + +import { dirname, resolve } from "node:path"; + +/** + * 拼接路径 + * @private + * 不作为公共方法 与特定路径强耦合的 + * + * @deprecated + * 不需要 对应的删除函数不需要生成具体的路径 + */ +function joinPath(dir: string): string { + const resPath = resolve("..", "types", dir); + // console.log(" in joinPath => ", resPath); + return resPath; +} + +/** + * TODO: 拆分成各个小配置字符串,移植到vite配置内,作为静态常量。 + */ +const filenames = [ + // + // "components-instance.d.ts", + // "components.d.ts", + // 填写glob语法 对于 deleteAsync 函数匹配有效。 + "components*.d.ts", + "typed-router.d.ts", + "auto-imports.d.ts", +]; + +type Filename = (typeof filenames)[number]; + +function createTypeFilePath(filename: Filename) { + return `./types/${filename}`; +} + +filenames.forEach(async (filename) => { + const typeFilePath = createTypeFilePath(filename); + try { + const resDeleteAsync = await deleteAsync(typeFilePath); + // consola.info(" 查看删除文件的返回值路径: ", resDeleteAsync); + consola.success(`删除类型文件 ${filename} 成功`); + } catch (error) { + consola.error(`删除类型文件 ${filename} 失败`); + } +}); diff --git a/vite-plugin-autogeneration-import-file/app/turbo.json b/vite-plugin-autogeneration-import-file/app/turbo.json index 7532f42..b703c8a 100644 --- a/vite-plugin-autogeneration-import-file/app/turbo.json +++ b/vite-plugin-autogeneration-import-file/app/turbo.json @@ -8,6 +8,13 @@ "dependsOn": [ "@ruan-cat-test/vite-plugin-autogeneration-import-file#build" ] + }, + "rm:types": {}, + "do-predev": { + "dependsOn": [ + "rm:types", + "do-plugin-build" + ] } } } \ No newline at end of file diff --git a/vite-plugin-autogeneration-import-file/core/package.json b/vite-plugin-autogeneration-import-file/core/package.json index a4a0a8a..7e95796 100644 --- a/vite-plugin-autogeneration-import-file/core/package.json +++ b/vite-plugin-autogeneration-import-file/core/package.json @@ -19,7 +19,8 @@ "scripts": { "test": "pnpm run build", "clean": "rimraf dist .turbo", - "build": "pnpm run clean && microbundle --target node -f esm,cjs", + "clean:dist": "rimraf dist", + "build": "pnpm run clean:dist && microbundle --target node -f esm,cjs", "rm:node_modules": "rimraf node_modules" }, "dependencies": { From f5f92e637f8beaa33aa3adf040462e4dbef9b32c Mon Sep 17 00:00:00 2001 From: ruan-cat <1219043956@qq.com> Date: Tue, 22 Oct 2024 11:49:32 +0800 Subject: [PATCH 08/15] =?UTF-8?q?=E2=9C=A8=20feat(vite-plugin-autogenerati?= =?UTF-8?q?on-import-file-test-app-1):=20=E5=AE=8C=E6=88=90=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E7=94=9F=E6=88=90=E3=80=81=E5=88=A0=E9=99=A4=E3=80=81?= =?UTF-8?q?=E4=B8=8E=E5=89=8D=E7=BD=AE=E6=89=93=E5=8C=85=E7=AD=89=E8=A1=8C?= =?UTF-8?q?=E4=B8=BA=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/.gitignore | 5 +- .../app/scripts/del-types.ts | 22 +-------- .../app/tsconfig.json | 5 +- .../app/turbo.json | 4 +- .../app/vite.config.ts | 48 +++++++++++++------ 5 files changed, 42 insertions(+), 42 deletions(-) diff --git a/vite-plugin-autogeneration-import-file/app/.gitignore b/vite-plugin-autogeneration-import-file/app/.gitignore index af92c9e..4242db8 100644 --- a/vite-plugin-autogeneration-import-file/app/.gitignore +++ b/vite-plugin-autogeneration-import-file/app/.gitignore @@ -1,7 +1,10 @@ # 自动生成的类型文件 都不参与提交 -*.d.ts +# *.d.ts + +# 不提交生成出来的类型文件 +generate-types*.d.ts # 垫片类型文件 需要提交至仓库 !vite-env.d.ts \ No newline at end of file diff --git a/vite-plugin-autogeneration-import-file/app/scripts/del-types.ts b/vite-plugin-autogeneration-import-file/app/scripts/del-types.ts index ece50e0..012b9f3 100644 --- a/vite-plugin-autogeneration-import-file/app/scripts/del-types.ts +++ b/vite-plugin-autogeneration-import-file/app/scripts/del-types.ts @@ -5,29 +5,9 @@ import { consola } from "consola"; import { dirname, resolve } from "node:path"; -/** - * 拼接路径 - * @private - * 不作为公共方法 与特定路径强耦合的 - * - * @deprecated - * 不需要 对应的删除函数不需要生成具体的路径 - */ -function joinPath(dir: string): string { - const resPath = resolve("..", "types", dir); - // console.log(" in joinPath => ", resPath); - return resPath; -} - -/** - * TODO: 拆分成各个小配置字符串,移植到vite配置内,作为静态常量。 - */ const filenames = [ - // - // "components-instance.d.ts", - // "components.d.ts", // 填写glob语法 对于 deleteAsync 函数匹配有效。 - "components*.d.ts", + "generate-types*.d.ts", "typed-router.d.ts", "auto-imports.d.ts", ]; diff --git a/vite-plugin-autogeneration-import-file/app/tsconfig.json b/vite-plugin-autogeneration-import-file/app/tsconfig.json index 81e72bb..a084126 100644 --- a/vite-plugin-autogeneration-import-file/app/tsconfig.json +++ b/vite-plugin-autogeneration-import-file/app/tsconfig.json @@ -21,10 +21,7 @@ "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue", - "myComponents.d.ts", - "myDirective.d.ts", - "components1.d.ts", - "types/vite-env.d.ts" + "./types" ], "references": [ { diff --git a/vite-plugin-autogeneration-import-file/app/turbo.json b/vite-plugin-autogeneration-import-file/app/turbo.json index b703c8a..9867694 100644 --- a/vite-plugin-autogeneration-import-file/app/turbo.json +++ b/vite-plugin-autogeneration-import-file/app/turbo.json @@ -9,7 +9,9 @@ "@ruan-cat-test/vite-plugin-autogeneration-import-file#build" ] }, - "rm:types": {}, + "rm:types": { + "cache": false + }, "do-predev": { "dependsOn": [ "rm:types", diff --git a/vite-plugin-autogeneration-import-file/app/vite.config.ts b/vite-plugin-autogeneration-import-file/app/vite.config.ts index 1b5e563..c2d17c7 100644 --- a/vite-plugin-autogeneration-import-file/app/vite.config.ts +++ b/vite-plugin-autogeneration-import-file/app/vite.config.ts @@ -1,11 +1,28 @@ +import { dirname, resolve } from "node:path"; +import { fileURLToPath, URL } from "node:url"; +import * as fs from "node:fs"; + import { defineConfig } from "vite"; import vue from "@vitejs/plugin-vue"; import Components from "unplugin-vue-components/vite"; import { getName, createPlugin } from "@ruan-cat-test/vite-plugin-autogeneration-import-file"; + const { autoImport, resolver } = createPlugin(); +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); +function pathResolve(dir: string) { + const resPath = resolve(__dirname, ".", dir); + return resPath; +} + // https://vitejs.dev/config/ export default defineConfig({ + server: { + port: 8080, + open: true, + }, + plugins: [ vue(), @@ -13,8 +30,8 @@ export default defineConfig({ // components { pattern: ["**/{index.vue,index.ts,index.js}", "*.{vue,ts,js}"], - dir: "./src/components", - toFile: "./components1.d.ts", + dir: pathResolve("./src/components"), + toFile: pathResolve("./types/generate-types-components.d.ts"), template: ` import '@vue/runtime-core' export {} @@ -28,21 +45,22 @@ declare module '@vue/runtime-core' { }, // module - // { - // pattern: ["**/*.{ts,js}", "*.{ts,js}"], - // dir: "./src/store/modules", - // toFile: "./src/store/module.ts", - // name: (name) => { - // name = getName(name); - // return name[0].toUpperCase() + name.slice(1) + "Store"; - // }, - // }, + { + pattern: ["**/*.{ts,js}", "*.{ts,js}"], + // dir: pathResolve("./src/store/modules"), + dir: pathResolve("./src/store"), + toFile: pathResolve("./types/generate-types-store-modules.d.ts"), + name: (name) => { + name = getName(name); + return name[0].toUpperCase() + name.slice(1) + "Store"; + }, + }, // myComponents { pattern: ["**/{index.vue,index.ts,index.js}", "*.{vue,ts,js}"], - dir: "./src/myComponents", - toFile: "./myComponents.d.ts", + dir: pathResolve("./src/myComponents"), + toFile: pathResolve("./types/generate-types-myComponents.d.ts"), template: ` import '@vue/runtime-core' export {} @@ -58,8 +76,8 @@ declare module '@vue/runtime-core' { // myDirective { pattern: ["**/{index.vue,index.ts,index.js}", "*.{vue,ts,js}"], - dir: "./src/myDirective", - toFile: "./myDirective.d.ts", + dir: pathResolve("./src/myDirective"), + toFile: pathResolve("./types/generate-types-myDirective.d.ts"), template: ` import '@vue/runtime-core' export {} From 54316bdfd4a608aa81582a4261ac1b993831649c Mon Sep 17 00:00:00 2001 From: ruan-cat <1219043956@qq.com> Date: Tue, 22 Oct 2024 11:53:45 +0800 Subject: [PATCH 09/15] =?UTF-8?q?=F0=9F=90=B3=20chore(vite-plugin-autogene?= =?UTF-8?q?ration-import-file-test-app-1):=20=E5=AE=8C=E6=88=90=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=EF=BC=8C=E5=8F=91=E7=8E=B0=E7=BB=84=E4=BB=B6=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E5=AE=8C=E6=88=90=E8=87=AA=E5=8A=A8=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vite-plugin-autogeneration-import-file/app/vite.config.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/vite-plugin-autogeneration-import-file/app/vite.config.ts b/vite-plugin-autogeneration-import-file/app/vite.config.ts index c2d17c7..aef14e0 100644 --- a/vite-plugin-autogeneration-import-file/app/vite.config.ts +++ b/vite-plugin-autogeneration-import-file/app/vite.config.ts @@ -96,9 +96,7 @@ declare module '@vue/runtime-core' { Components({ dirs: [], dts: false, - resolvers: [ - // resolver([0, 2], [3]) - ], + resolvers: [resolver([0, 2], [3])], }), ], }); From 04ea9e388503ba1200d115bc4cb589452ed954fe Mon Sep 17 00:00:00 2001 From: ruan-cat <1219043956@qq.com> Date: Tue, 22 Oct 2024 15:52:25 +0800 Subject: [PATCH 10/15] =?UTF-8?q?=F0=9F=90=9E=20fix(vite-plugin-autogenera?= =?UTF-8?q?tion-import-file,vite-plugin-autogeneration-import-file-test-ap?= =?UTF-8?q?p-1):=20=E5=AE=8C=E6=88=90bug=E4=BF=AE=E5=A4=8D=EF=BC=8C?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86=E7=BB=84=E4=BB=B6=E7=9A=84=E5=90=8D?= =?UTF-8?q?=E7=A7=B0=E6=B3=A8=E5=86=8C=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/src/App.vue | 10 +- .../app/src/myComponents/test.vue | 4 +- .../app/src/myComponents/test/dir/index.vue | 4 +- .../app/template/components.template.d.ts | 37 +++++ .../app/vite.config.ts | 92 +++++++----- .../core/src/index.ts | 134 ++++++++++++------ 6 files changed, 196 insertions(+), 85 deletions(-) create mode 100644 vite-plugin-autogeneration-import-file/app/template/components.template.d.ts diff --git a/vite-plugin-autogeneration-import-file/app/src/App.vue b/vite-plugin-autogeneration-import-file/app/src/App.vue index f1f3b4b..1112da2 100644 --- a/vite-plugin-autogeneration-import-file/app/src/App.vue +++ b/vite-plugin-autogeneration-import-file/app/src/App.vue @@ -1,7 +1,7 @@