Skip to content

Commit

Permalink
fix(auto-import): alias import preset
Browse files Browse the repository at this point in the history
  • Loading branch information
CCherry07 committed Sep 9, 2024
1 parent 4e4678f commit ee32822
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 29 deletions.
6 changes: 6 additions & 0 deletions packages/auto-import/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @farmfe/plugin-auto-import

## 0.0.3

### Patch Changes

- fix(auto-import): alias import preset

## 0.0.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/auto-import/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@farmfe/plugin-auto-import",
"version": "0.0.2",
"version": "0.0.3",
"main": "scripts/index.js",
"types": "scripts/index.d.ts",
"type": "module",
Expand Down
5 changes: 0 additions & 5 deletions packages/auto-import/playground-vue/auto_import.d.ts

This file was deleted.

14 changes: 11 additions & 3 deletions packages/auto-import/playground-vue/farm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ export default defineConfig({
plugins: [
visualizer(),
farmAutoImport({
dirs: ["src/apis"],
dts: "./src/auto_import.d.ts",
})],
dts: "./src/auto_import.d.ts",
presets:[
"vue",
{
'@vueuse/core': [
'useMouse',
['useFetch', 'useMyFetch']
],
}
]
})],
});
3 changes: 2 additions & 1 deletion packages/auto-import/playground-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
"clean": "farm clean"
},
"dependencies": {
"@vueuse/core": "^11.0.3",
"vue": "^3.4.0"
},
"devDependencies": {
"@farmfe/cli": "^1.0.2",
"@farmfe/core": "^1.3.0",
"@vitejs/plugin-vue": "^5.0.4",
"@farmfe/plugin-auto-import": "workspace:*",
"@vitejs/plugin-vue": "^5.0.4",
"core-js": "^3.30.1"
}
}
7 changes: 7 additions & 0 deletions packages/auto-import/playground-vue/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<script setup lang="ts">
import HelloWorld from './components/HelloWorld.vue'
const s = useMyFetch("http:://baidu.com")
const { x, y } = useMouse()
watch(y, () => {
console.log(y)
}, {
deep: true
})
</script>

<template>
Expand Down
1 change: 1 addition & 0 deletions packages/auto-import/playground-vue/src/apis/name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default function name() {}
51 changes: 51 additions & 0 deletions packages/auto-import/playground-vue/src/auto_import.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
/* generated by farmfe_plugin_auto_import */
export {}
declare global {
const name: typeof import('./apis/name.ts')['default']
const getName: typeof import('./apis/index.ts')['getName']
const onActivated: typeof import('vue')['onActivated']
const onBeforeMount: typeof import('vue')['onBeforeMount']
const onBeforeUnmount: typeof import('vue')['onBeforeUnmount']
const onBeforeUpdate: typeof import('vue')['onBeforeUpdate']
const onErrorCaptured: typeof import('vue')['onErrorCaptured']
const onDeactivated: typeof import('vue')['onDeactivated']
const onMounted: typeof import('vue')['onMounted']
const onServerPrefetch: typeof import('vue')['onServerPrefetch']
const onUnmounted: typeof import('vue')['onUnmounted']
const onUpdated: typeof import('vue')['onUpdated']
const useAttrs: typeof import('vue')['useAttrs']
const useSlots: typeof import('vue')['useSlots']
const computed: typeof import('vue')['computed']
const customRef: typeof import('vue')['customRef']
const isReadonly: typeof import('vue')['isReadonly']
const isRef: typeof import('vue')['isRef']
const isProxy: typeof import('vue')['isProxy']
const isReactive: typeof import('vue')['isReactive']
const markRaw: typeof import('vue')['markRaw']
const reactive: typeof import('vue')['reactive']
const readonly: typeof import('vue')['readonly']
const ref: typeof import('vue')['ref']
const shallowReactive: typeof import('vue')['shallowReactive']
const shallowReadonly: typeof import('vue')['shallowReadonly']
const shallowRef: typeof import('vue')['shallowRef']
const triggerRef: typeof import('vue')['triggerRef']
const toRaw: typeof import('vue')['toRaw']
const toRef: typeof import('vue')['toRef']
const toRefs: typeof import('vue')['toRefs']
const toValue: typeof import('vue')['toValue']
const unref: typeof import('vue')['unref']
const watch: typeof import('vue')['watch']
const watchEffect: typeof import('vue')['watchEffect']
const watchPostEffect: typeof import('vue')['watchPostEffect']
const watchSyncEffect: typeof import('vue')['watchSyncEffect']
const defineComponent: typeof import('vue')['defineComponent']
const defineAsyncComponent: typeof import('vue')['defineAsyncComponent']
const getCurrentInstance: typeof import('vue')['getCurrentInstance']
const h: typeof import('vue')['h']
const inject: typeof import('vue')['inject']
const nextTick: typeof import('vue')['nextTick']
const provide: typeof import('vue')['provide']
const useCssModule: typeof import('vue')['useCssModule']
const createApp: typeof import('vue')['createApp']
const effectScope: typeof import('vue')['effectScope']
const EffectScope: typeof import('vue')['EffectScope']
const getCurrentScope: typeof import('vue')['getCurrentScope']
const onScopeDispose: typeof import('vue')['onScopeDispose']
const useMouse: typeof import('@vueuse/core')['useMouse']
const useMyFetch: typeof import('@vueuse/core')['useFetch']
}
53 changes: 53 additions & 0 deletions packages/auto-import/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions packages/auto-import/src/parser/generate_dts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,29 @@ pub fn remove_tsx_jsx_suffix(s: &str) -> String {
re.replace(s, "").into_owned()
}

pub fn stringify_resolver(item: &Import) -> String {
pub fn stringify_presets(item: &Import) -> String {
format!(
"\tconst {}: typeof import('{}')['{}']\n",
item.name, item.form, item.name
item.as_name.clone().unwrap_or(item.name.clone()), item.form, item.name
)
}

pub fn stringify_imports_dts(dts_file_path: &str, item: &Import) -> String {
let related = format!("./{}", relative(dts_file_path, &item.form));
let import_path = remove_tsx_jsx_suffix(&related);
let is_export_component = match item.export_type {
ExportType::Default => false,
let is_export_decl = match item.export_type {
ExportType::DefaultDecl => false,
_ => true,
};
let mut target = "default";
if is_export_component {
if is_export_decl {
target = &item.name;
}
format!(
"\tconst {}: typeof import('{}')['{}']\n",
item.name, import_path, target
item.as_name.clone().unwrap_or(item.name.clone()),
import_path,
target
)
}

Expand All @@ -61,7 +63,7 @@ pub fn generate_dts(option: GenerateDtsOption) {
&option
.presets_imports
.iter()
.map(|&item| stringify_resolver(item))
.map(|&item| stringify_presets(item))
.collect::<Vec<_>>()
.join(""),
);
Expand Down Expand Up @@ -98,9 +100,7 @@ mod tests {
let imports = scan_dir_exports(root_path);
println!("imports: {:#?}", imports);

let presets_imports = resolve_presets(&vec![PresetItem::String(
"react-router".to_string(),
)]);
let presets_imports = resolve_presets(&vec![PresetItem::String("react-router".to_string())]);
let generate_dts_option = GenerateDtsOption {
imports: &imports.iter().collect::<Vec<_>>(),
presets_imports: &presets_imports.iter().collect::<Vec<_>>(),
Expand Down
3 changes: 2 additions & 1 deletion packages/auto-import/src/parser/inject_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ fn get_exclude_imports(content: &str, imports: Vec<Import>) -> Vec<Import> {
imports
.into_iter()
.filter(|item| {
let name = &item.name.as_str();
let name = item.as_name.clone().unwrap_or(item.name.clone());
let name = &name.as_str();
!exclude_vars.contains(name) && include_vars.contains(name)
})
.collect()
Expand Down
Loading

0 comments on commit ee32822

Please sign in to comment.