-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: experimental auto export of data loaders
- Loading branch information
Showing
10 changed files
with
242 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { defineColadaLoader } from 'unplugin-vue-router/data-loaders/pinia-colada' | ||
import { ref } from 'vue' | ||
|
||
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) | ||
export const simulateError = ref(false) | ||
|
||
export const useUserData = defineColadaLoader('/users/colada-loader.[id]', { | ||
async query(to, { signal }) { | ||
console.log('[🍹] coladaLoader', to.fullPath) | ||
// signal.addEventListener('abort', () => { | ||
// console.log('[🍹❌] aborted', to.fullPath) | ||
// }) | ||
// we need to read these before the delay | ||
const id = to.params.id | ||
// @ts-expect-error: no param "name"! | ||
const name = to.params.name | ||
|
||
await delay(500) | ||
if (simulateError.value) { | ||
throw new Error('Simulated Error') | ||
} | ||
|
||
const user = { | ||
id, | ||
name, | ||
when: new Date().toUTCString(), | ||
} | ||
|
||
return user | ||
}, | ||
key: (to) => { | ||
// console.log('[🍹] key', to.fullPath) | ||
return ['loader-users', to.params.id] | ||
}, | ||
staleTime: 10000, | ||
// lazy: (to, from) => to.name && to.name === from?.name, | ||
}) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { createFilter } from '@rollup/pluginutils' | ||
import MagicString from 'magic-string' | ||
import { findStaticImports, parseStaticImport } from 'mlly' | ||
import { type UnpluginOptions } from 'unplugin' | ||
|
||
const ignoredSpecifiers = ['vue', 'vue-router', 'pinia'] | ||
|
||
export function extractLoadersToExport(code: string): string[] { | ||
const imports = findStaticImports(code) | ||
const importNames = imports.flatMap((i) => { | ||
const parsed = parseStaticImport(i) | ||
|
||
// bail out faster for anything that is not a data loader | ||
if ( | ||
// NOTE: move out to a regexp if the option is exposed | ||
parsed.specifier.startsWith('@vueuse/') && | ||
ignoredSpecifiers.includes(parsed.specifier) | ||
) | ||
return [] | ||
|
||
return [ | ||
parsed.defaultImport, | ||
...Object.values(parsed.namedImports || {}), | ||
].filter((v): v is string => !!v && !v.startsWith('_')) | ||
}) | ||
|
||
return importNames | ||
} | ||
|
||
export function createAutoExportPlugin(): UnpluginOptions { | ||
const filterVueComponents = createFilter( | ||
[/\.vue$/, /\.vue\?vue/, /\.vue\?v=/] | ||
// [/[\\/]node_modules[\\/]/, /[\\/]\.git[\\/]/, /[\\/]\.nuxt[\\/]/] | ||
) | ||
|
||
return { | ||
name: 'unplugin-vue-router:data-loaders-auto-export', | ||
enforce: 'post', | ||
vite: { | ||
transform: { | ||
order: 'post', | ||
handler(code, id) { | ||
if (!filterVueComponents(id)) { | ||
return | ||
} | ||
|
||
const loadersToExports = extractLoadersToExport(code) | ||
|
||
if (loadersToExports.length <= 0) return | ||
|
||
const s = new MagicString(code) | ||
s.append( | ||
`\nexport const __loaders = [\n${loadersToExports.join(',\n')}\n];\n` | ||
) | ||
|
||
return { | ||
code: s.toString(), | ||
map: s.generateMap(), | ||
} | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.