diff --git a/docs/composables.d.ts b/docs/composables.d.ts index 4bc082a8..8d652c7c 100644 --- a/docs/composables.d.ts +++ b/docs/composables.d.ts @@ -1,6 +1,5 @@ -// generated by iles -// We suggest you to commit this file into source control - +// Generated by 'unplugin-auto-import' +export {} declare global { const definePageComponent: typeof import('iles')['definePageComponent'] const useDocuments: typeof import('iles')['useDocuments'] @@ -8,5 +7,3 @@ declare global { const usePage: typeof import('iles')['usePage'] const useRoute: typeof import('iles')['useRoute'] } - -export { } diff --git a/packages/iles/package.json b/packages/iles/package.json index d0055f75..b67c34cc 100644 --- a/packages/iles/package.json +++ b/packages/iles/package.json @@ -82,6 +82,7 @@ "picocolors": "^1.0.0", "unist-util-visit": "^4.1.0", "unplugin-vue-components": "^0.21.1", + "unplugin-auto-import": "^0.11.4", "vite": "^3.2.0", "vue": "^3.2.37", "vue-router": "^4.0.12" diff --git a/packages/iles/src/node/config.ts b/packages/iles/src/node/config.ts index 926538c6..fe1008d1 100644 --- a/packages/iles/src/node/config.ts +++ b/packages/iles/src/node/config.ts @@ -6,6 +6,7 @@ import creatDebugger from 'debug' import { loadConfigFromFile, mergeConfig as mergeViteConfig } from 'vite' import vue from '@vitejs/plugin-vue' import components from 'unplugin-vue-components/vite' +import composables from 'unplugin-auto-import/vite' import pages from '@islands/pages' import mdx from '@islands/mdx' @@ -136,6 +137,7 @@ async function setNamedPlugins (config: AppConfig, env: ConfigEnv, plugins: Name tagName.startsWith('ile-') || ceChecks.some(fn => fn!(tagName)) plugins.components = components(config.components) + plugins.composables = composables(config.composables) plugins.vue = vue(config.vue) const optionalPlugins = { @@ -281,6 +283,20 @@ function appConfigDefaults (appConfig: AppConfig, userConfig: UserConfig, env: C ], transformer: 'vue3', }, + composables: { + dts: resolve(root, 'composables.d.ts'), + imports: [ + { + iles: [ + 'definePageComponent', + 'useDocuments', + 'useHead', + 'usePage', + 'useRoute', + ], + }, + ], + }, } } diff --git a/packages/iles/src/node/plugin/composables.ts b/packages/iles/src/node/plugin/composables.ts deleted file mode 100644 index f3bf8f16..00000000 --- a/packages/iles/src/node/plugin/composables.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { promises as fs } from 'fs' -import { resolve } from 'pathe' -import { uniq } from './utils' -import { parseImports } from './parse' - -const definitionRegex = /(?:function|const|let|var)\s+(definePageComponent|use(?:Page|Route|Head|Documents)\b)/g -const composableUsageRegex = /\b(definePageComponent|use(?:Page|Route|Head|Documents))\s*\(/g - -const composables = [ - 'definePageComponent', - 'useDocuments', - 'useHead', - 'usePage', - 'useRoute', -] - -export async function autoImportComposables (code: string, id: string): Promise { - const matches = Array.from(code.matchAll(composableUsageRegex)) - if (matches.length === 0) return - - const imports = await parseImports(code) - const defined = new Set(Array.from(code.matchAll(definitionRegex)).map(a => a[1])) - - const composables = uniq(matches.map(a => a[1])) - .filter(composable => !defined.has(composable) && !imports[composable]) - .join(', ') - - if (composables) - return `${code}\nimport { ${composables} } from "iles"` -} - -export function writeComposablesDTS (root: string) { - fs.writeFile(resolve(root, 'composables.d.ts'), `// generated by iles -// We suggest you to commit this file into source control - -declare global { -${composables.map(fn => ` const ${fn}: typeof import('iles')['${fn}']`).join('\n')} -} - -export { } -`, 'utf-8') -} diff --git a/packages/iles/src/node/plugin/plugin.ts b/packages/iles/src/node/plugin/plugin.ts index 6bfd9b91..34fe58ff 100644 --- a/packages/iles/src/node/plugin/plugin.ts +++ b/packages/iles/src/node/plugin/plugin.ts @@ -16,7 +16,6 @@ import { parseId } from './parse' import { wrapIslandsInSFC, wrapLayout } from './wrap' import { extendSite } from './site' import { detectMDXComponents } from './markdown' -import { autoImportComposables, writeComposablesDTS } from './composables' import documents from './documents' function isMarkdown (path: string) { @@ -63,8 +62,6 @@ export default function IslandsPlugins (appConfig: AppConfig): PluginOption[] { root = config.root isBuild = config.command === 'build' appConfig.resolvePath = config.createResolver() - - writeComposablesDTS(root) }, async resolveId (id) { if (id === ILES_APP_ENTRY) @@ -147,21 +144,10 @@ export default function IslandsPlugins (appConfig: AppConfig): PluginOption[] { plugins.vue, ...appConfig.vitePlugins, plugins.components, + plugins.composables, documents(appConfig), - { - name: 'iles:composables', - enforce: 'post', - async transform (code, id) { - if (!id.startsWith(appConfig.srcDir)) return - - const { path, query } = parseId(id) - if (isVueScript(path, query) || /\.[tj]sx?/.test(path)) - return await autoImportComposables(code, id) - }, - }, - { name: 'iles:page-data', enforce: 'post', diff --git a/packages/iles/types/shared.d.ts b/packages/iles/types/shared.d.ts index fa5809f4..0c73901b 100644 --- a/packages/iles/types/shared.d.ts +++ b/packages/iles/types/shared.d.ts @@ -5,6 +5,8 @@ import type { App, Ref, DefineComponent, VNode, AsyncComponentLoader } from 'vue import type VuePlugin, { Options as VueOptions } from '@vitejs/plugin-vue' import type ComponentsPlugin from 'unplugin-vue-components/vite' import type { Options as ComponentOptions } from 'unplugin-vue-components/types' +import type composablesPlugin from 'unplugin-auto-import/vite' +import type { Options as composablesOptions } from 'unplugin-auto-import/types' import type { Options as RequiredSolidOptions } from 'vite-plugin-solid' import type { Options as SvelteOptions } from '@sveltejs/vite-plugin-svelte' @@ -107,6 +109,7 @@ export interface NamedPlugins { pages: { api: PagesApi } vue: ReturnType components: ReturnType + composables: ReturnType } export interface SSGContext { @@ -128,6 +131,7 @@ export interface BaseIlesConfig extends PagesOptions { * imports for components in Vue and MDX files. */ components: ComponentOptions + composables: composablesOptions /** * Configuration options for @preact/preset-vite */ diff --git a/playground/the-vue-point/composables.d.ts b/playground/the-vue-point/composables.d.ts index 4bc082a8..8d652c7c 100644 --- a/playground/the-vue-point/composables.d.ts +++ b/playground/the-vue-point/composables.d.ts @@ -1,6 +1,5 @@ -// generated by iles -// We suggest you to commit this file into source control - +// Generated by 'unplugin-auto-import' +export {} declare global { const definePageComponent: typeof import('iles')['definePageComponent'] const useDocuments: typeof import('iles')['useDocuments'] @@ -8,5 +7,3 @@ declare global { const usePage: typeof import('iles')['usePage'] const useRoute: typeof import('iles')['useRoute'] } - -export { } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 211f7c6f..92aece43 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -220,6 +220,7 @@ importers: tsup: 6.1.2 typescript: 4.4.4 unist-util-visit: ^4.1.0 + unplugin-auto-import: ^0.11.4 unplugin-vue-components: ^0.21.1 vfile: ^5.2.0 vite: ^3.2.0 @@ -248,6 +249,7 @@ importers: pathe: 0.2.0 picocolors: 1.0.0 unist-util-visit: 4.1.0 + unplugin-auto-import: 0.11.4_rollup@2.59.0 unplugin-vue-components: 0.21.1_zikqhrjqvenuidxnsgj4kweaxy vite: 3.2.0 vue: 3.2.37 @@ -577,6 +579,10 @@ packages: /@antfu/utils/0.5.2: resolution: {integrity: sha512-CQkeV+oJxUazwjlHD0/3ZD08QWKuGQkhnrKo3e6ly5pd48VUpXbb77q0xMU4+vc2CkJnDS02Eq/M9ugyX20XZA==} + /@antfu/utils/0.6.2: + resolution: {integrity: sha512-Qdwda+qE1KOKXuM8Gnn09zIJSA293J5putdHBx4J9sdbHzqp5QY4OmH77KgwJoi0PuY+kbddkVVdkSoZ9ruiug==} + dev: false + /@apideck/better-ajv-errors/0.3.3_ajv@8.11.0: resolution: {integrity: sha512-9o+HO2MbJhJHjDYZaDxJmSDckvDpiuItEsrIShV0DXeCshXWRHhqYyU/PKHMkuClOmFnZhRd6wzv4vpDu/dRKg==} engines: {node: '>=10'} @@ -3400,6 +3406,21 @@ packages: estree-walker: 2.0.2 picomatch: 2.3.1 + /@rollup/pluginutils/5.0.2_rollup@2.59.0: + resolution: {integrity: sha512-pTd9rIsP92h+B6wWwFbW8RkZv4hiR/xKsqre4SIuAOaOEQRxi0lqLke9k2/7WegC85GgUs9pjmOjCUi3In4vwA==} + engines: {node: '>=14.0.0'} + peerDependencies: + rollup: ^1.20.0||^2.0.0||^3.0.0 + peerDependenciesMeta: + rollup: + optional: true + dependencies: + '@types/estree': 1.0.0 + estree-walker: 2.0.2 + picomatch: 2.3.1 + rollup: 2.59.0 + dev: false + /@sinonjs/commons/1.8.3: resolution: {integrity: sha512-xkNcLAn/wZaX14RPlwizcKicDk9G3F8m2nU3L7Ukm5zBgTwiT0wsoFAHx9Jq56fJA1z/7uKGtCRu16sOUCLIHQ==} dependencies: @@ -3723,6 +3744,10 @@ packages: /@types/estree/0.0.50: resolution: {integrity: sha512-C6N5s2ZFtuZRj54k2/zyRhNDjJwwcViAM3Nbm8zjBpbqAdZ00mr0CFxvSKeO8Y/e03WVFLpQMdHYVfUd6SB+Hw==} + /@types/estree/1.0.0: + resolution: {integrity: sha512-WulqXMDUTYAXCjZnk6JtIHPigp55cVtDgDrO2gHRwhyJto21+1zbVCtOYB2L1F9w4qCQ0rOGWBnBe0FNTiEJIQ==} + dev: false + /@types/fs-extra/9.0.12: resolution: {integrity: sha512-I+bsBr67CurCGnSenZZ7v94gd3tc3+Aj2taxMT4yu4ABLuOgOjeFxX3dokG24ztSRg5tnT00sL8BszO7gSMoIw==} dependencies: @@ -4285,6 +4310,11 @@ packages: engines: {node: '>=0.4.0'} hasBin: true + /acorn/8.8.1: + resolution: {integrity: sha512-7zFpHzhnqYKrkYdUjF1HI1bzd0VygEGX8lFk4k5zVMqHEoES+P+7TKI+EvLO9WVMJ8eekdO0aDEK044xTXwPPA==} + engines: {node: '>=0.4.0'} + hasBin: true + /add-stream/1.0.0: resolution: {integrity: sha512-qQLMr+8o0WC4FZGQTcJiKBVC59JylcPSrTtk6usvmIDFUOCKegapy1VHQwRbFMOFyb/inzUVqHs+eMYKDM1YeQ==} dev: true @@ -6225,7 +6255,6 @@ packages: /escape-string-regexp/5.0.0: resolution: {integrity: sha512-/veY75JbMK4j1yjvuUxuVsiS/hr/4iHs9FTT6cgTexxdE0Ly/glccBAkloH/DofkjRbZU3bnoj38mOmhkZ0lHw==} engines: {node: '>=12'} - dev: true /escodegen/2.0.0: resolution: {integrity: sha512-mmHKys/C8BFUGI+MAWNcSYoORYLMdPzjrknd2Vc+bUsjN5bXcr8EhrNB+UTqfL1y3I9c4fw2ihgtMPQLBRiQxw==} @@ -6691,6 +6720,17 @@ packages: merge2: 1.4.1 micromatch: 4.0.5 + /fast-glob/3.2.12: + resolution: {integrity: sha512-DVj4CQIYYow0BlaelwK1pHl5n5cRSJfM60UA0zK891sVInoPri2Ekj7+e1CT3/3qxXenpI+nBBmQAcJPJgaj4w==} + engines: {node: '>=8.6.0'} + dependencies: + '@nodelib/fs.stat': 2.0.5 + '@nodelib/fs.walk': 1.2.8 + glob-parent: 5.1.2 + merge2: 1.4.1 + micromatch: 4.0.5 + dev: false + /fast-glob/3.2.7: resolution: {integrity: sha512-rYGMRwip6lUMvYD3BTScMwT1HtAs2d71SMv66Vrxs0IekGZEjhM0pcMfjQPnknBt2zeCwQMEupiN02ZP4DiT1Q==} engines: {node: '>=8'} @@ -8422,6 +8462,9 @@ packages: resolution: {integrity: sha512-fQzRfAbIBnR0IQvftw9FJveWiHp72Fg20giDrHz6TdfB12UH/uue0D3hm57UB5KgAVuniLMCaS8P1IMj9NR7cA==} dev: true + /jsonc-parser/3.2.0: + resolution: {integrity: sha512-gfFQZrcTc8CnKXp6Y4/CBT3fTc0OVuDofpre4aEeEpSBPV5X5v4+Vmx+8snU7RLPrNHPKSgLxGo9YuQzz20o+w==} + /jsonfile/6.1.0: resolution: {integrity: sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==} dependencies: @@ -8533,6 +8576,11 @@ packages: resolution: {integrity: sha512-lL87ytIGP2FU5PWwNDo0w3WhIo2gopIAxPg9RxDYF7m4rr5ahuZxP22xnJHIvaLTe4Z9P6uKKY2UHiwyB4pcrw==} engines: {node: '>=14'} + /local-pkg/0.4.2: + resolution: {integrity: sha512-mlERgSPrbxU3BP4qBqAvvwlgW4MTg78iwJdGGnv7kibKjWcJksrG3t6LB5lXI93wXRDvG4NpUgJFmTG4T6rdrg==} + engines: {node: '>=14'} + dev: false + /locate-path/2.0.0: resolution: {integrity: sha512-NCI2kiDkyR7VeEKm27Kda/iQHyKJe1Bu0FlTbYp3CqJu+9IFe9bLyAjMxf5ZDDbEg+iMPzB5zYyUTSm8wVTKmA==} engines: {node: '>=4'} @@ -8631,7 +8679,6 @@ packages: engines: {node: '>=12'} dependencies: sourcemap-codec: 1.4.8 - dev: true /make-dir/3.1.0: resolution: {integrity: sha512-g3FeP20LNwhALb/6Cz6Dd4F2ngze0jz7tbzrD2wAV+o9FeNHe4rL+yK2md0J/fiSf1sa1ADhXqi5+oVwOM/eGw==} @@ -9362,6 +9409,15 @@ packages: import-meta-resolve: 1.1.1 dev: false + /mlly/0.5.16: + resolution: {integrity: sha512-LaJ8yuh4v0zEmge/g3c7jjFlhoCPfQn6RCjXgm9A0Qiuochq4BcuOxVfWmdnCoLTlg2MV+hqhOek+W2OhG0Lwg==} + dependencies: + acorn: 8.8.1 + pathe: 0.3.9 + pkg-types: 0.3.6 + ufo: 0.8.6 + dev: false + /modify-values/1.0.1: resolution: {integrity: sha512-xV2bxeN6F7oYjZWTe/YPAy6MN2M+sL4u/Rlm2AHCIVGfo2p1yGmBHQ6vHehl4bRTZBdHu3TSkWdYgkwpYzAGSw==} engines: {node: '>=0.10.0'} @@ -9885,6 +9941,10 @@ packages: resolution: {integrity: sha512-sTitTPYnn23esFR3RlqYBWn4c45WGeLcsKzQiUpXJAyfcWkolvlYpV8FLo7JishK946oQwMFUCHXQ9AjGPKExw==} dev: false + /pathe/0.3.9: + resolution: {integrity: sha512-6Y6s0vT112P3jD8dGfuS6r+lpa0qqNrLyHPOwvXMnyNTQaYiwgau2DP3aNDsR13xqtGj7rrPo+jFUATpU6/s+g==} + dev: false + /pathval/1.1.1: resolution: {integrity: sha512-Dp6zGqpTdETdR63lehJYPeIOqpiNBNtc7BpWSLrOje7UaIsE5aY92r/AunQA7rsXvet3lrJ3JnZX29UPTKXyKQ==} dev: true @@ -9934,6 +9994,14 @@ packages: find-up: 6.3.0 dev: true + /pkg-types/0.3.6: + resolution: {integrity: sha512-uQZutkkh6axl1GxDm5/+8ivVdwuJ5pyDGqJeSiIWIUWIqYiK3p9QKozN/Rv6eVvFoeSWkN1uoYeSDBwwBJBtbg==} + dependencies: + jsonc-parser: 3.2.0 + mlly: 0.5.16 + pathe: 0.3.9 + dev: false + /pluralize/8.0.0: resolution: {integrity: sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA==} engines: {node: '>=4'} @@ -10632,6 +10700,10 @@ packages: xmlchars: 2.2.0 dev: true + /scule/0.3.2: + resolution: {integrity: sha512-zIvPdjOH8fv8CgrPT5eqtxHQXmPNnV/vHJYffZhE43KZkvULvpCTvOt1HPlFaCZx287INL9qaqrZg34e8NgI4g==} + dev: false + /section-matter/1.0.0: resolution: {integrity: sha512-vfD3pmTzGpufjScBh50YHKzEu2lxBWhVEHsNGoEXmCmn2hKGfeNLYMzCJpe8cD7gqX7TJluOVpBkAequ6dgMmA==} engines: {node: '>=4'} @@ -11036,6 +11108,12 @@ packages: engines: {node: '>=8'} dev: true + /strip-literal/0.4.2: + resolution: {integrity: sha512-pv48ybn4iE1O9RLgCAN0iU4Xv7RlBTiit6DKmMiErbs9x1wH6vXBs45tWc0H5wUIF6TLTrKweqkmYF/iraQKNw==} + dependencies: + acorn: 8.8.1 + dev: false + /style-search/0.1.0: resolution: {integrity: sha512-Dj1Okke1C3uKKwQcetra4jSuk0DqbzbYtXipzFlFMZtowbF1x7BKJwB9AayVMyFARvU8EDrZdcax4At/452cAg==} dev: true @@ -11575,6 +11653,9 @@ packages: resolution: {integrity: sha512-/+BmBDe8GvlB2nIflWasLLAInjYG0bC9HRnfEpNi4sw77J2AJNnEVnTDReVrehoh825+Q/evF3THXTAweyam2g==} dev: true + /ufo/0.8.6: + resolution: {integrity: sha512-fk6CmUgwKCfX79EzcDQQpSCMxrHstvbLswFChHS0Vump+kFkw7nJBfTZoC1j0bOGoY9I7R3n2DGek5ajbcYnOw==} + /uglify-js/3.16.1: resolution: {integrity: sha512-X5BGTIDH8U6IQ1TIRP62YC36k+ULAa1d59BxlWvPUJ1NkW5L3FwcGfEzuVvGmhJFBu0YJ5Ge25tmRISqCmLiRQ==} engines: {node: '>=0.8.0'} @@ -11645,6 +11726,24 @@ packages: trough: 2.0.2 vfile: 5.2.0 + /unimport/0.7.0_rollup@2.59.0: + resolution: {integrity: sha512-Cr0whz4toYVid3JHlni/uThwavDVVCk6Zw0Gxnol1c7DprTA+Isr4T+asO6rDGkhkgV7r3vSdSs5Ym8F15JA+w==} + dependencies: + '@rollup/pluginutils': 5.0.2_rollup@2.59.0 + escape-string-regexp: 5.0.0 + fast-glob: 3.2.12 + local-pkg: 0.4.2 + magic-string: 0.26.7 + mlly: 0.5.16 + pathe: 0.3.9 + pkg-types: 0.3.6 + scule: 0.3.2 + strip-literal: 0.4.2 + unplugin: 0.10.2 + transitivePeerDependencies: + - rollup + dev: false + /unique-string/2.0.0: resolution: {integrity: sha512-uNaeirEPvpZWSgzwsPGtU2zVSTrn/8L5q/IexZmH0eH6SA73CmAA5U4GwORTxQAZs95TAXLNqeLoPPNO5gZfWg==} engines: {node: '>=8'} @@ -11721,6 +11820,25 @@ packages: resolution: {integrity: sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==} engines: {node: '>= 10.0.0'} + /unplugin-auto-import/0.11.4_rollup@2.59.0: + resolution: {integrity: sha512-lh/bRDRYwgnb9Cm5ur8TlTMGxA1GRZvgzCvBIf0vyuVRy7ebWcWefFElpUDpr8vLl+ZRGsPVCOGiYJ8TCR625Q==} + engines: {node: '>=14'} + peerDependencies: + '@vueuse/core': '*' + peerDependenciesMeta: + '@vueuse/core': + optional: true + dependencies: + '@antfu/utils': 0.6.2 + '@rollup/pluginutils': 5.0.2_rollup@2.59.0 + local-pkg: 0.4.2 + magic-string: 0.26.7 + unimport: 0.7.0_rollup@2.59.0 + unplugin: 0.10.2 + transitivePeerDependencies: + - rollup + dev: false + /unplugin-icons/0.12.17: resolution: {integrity: sha512-y6KNKlwcLXlbfpZY48lO0blsH/uwHbvxD2sgCaw1AhgFk9Bjw7hA1kQxhUWPAIRySeobve2YP7HGKLHoIctXPA==} peerDependencies: @@ -11781,6 +11899,15 @@ packages: - webpack dev: false + /unplugin/0.10.2: + resolution: {integrity: sha512-6rk7GUa4ICYjae5PrAllvcDeuT8pA9+j5J5EkxbMFaV+SalHhxZ7X2dohMzu6C3XzsMT+6jwR/+pwPNR3uK9MA==} + dependencies: + acorn: 8.8.1 + chokidar: 3.5.3 + webpack-sources: 3.2.3 + webpack-virtual-modules: 0.4.6 + dev: false + /unplugin/0.2.21: resolution: {integrity: sha512-IJ15/L5XbhnV7J09Zjk0FT5HEkBjkXucWAXQWRsmEtUxmmxwh23yavrmDbCF6ZPxWiVB28+wnKIHePTRRpQPbQ==} peerDependencies: @@ -12363,6 +12490,9 @@ packages: resolution: {integrity: sha512-h9atBP/bsZohWpHnr+2sic8Iecb60GxftXsWNLLLSqewgIsGzByd2gcIID4nXcG+3tNe4GQG3dLcff3kXupdRA==} dev: false + /webpack-virtual-modules/0.4.6: + resolution: {integrity: sha512-5tyDlKLqPfMqjT3Q9TAqf2YqjwmnUleZwzJi1A5qXnlBCdj2AtOJ6wAWdglTIDOPgOiOrXeBeFcsQ8+aGQ6QbA==} + /whatwg-encoding/1.0.5: resolution: {integrity: sha512-b5lim54JOPN9HtzvK9HFXvBma/rnfFeqsic0hSpjtDbVxR3dJKLc+KB4V6GgiGOvl7CY/KNh8rxSo9DKQrnUEw==} dependencies: