Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(injectAtEnd): add new option #327

Merged
merged 1 commit into from
Mar 23, 2023
Merged

feat(injectAtEnd): add new option #327

merged 1 commit into from
Mar 23, 2023

Conversation

peerless-hero
Copy link
Contributor

@peerless-hero peerless-hero commented Feb 20, 2023

Description

add new option about unimport.

💡Inject the imports at the end of other imports

The default value is true for the current version, which has no effect on upgrades.

Linked Issues

no.

Additional context

My config is

AutoImport({
  imports: [
    'vue',
    'vue/macros',
    'vue-router',
    'vue-i18n',
    '@vueuse/core',
    'pinia',
    {
      vue: ['createVNode'],
      '@/i18n/use-locale': ['dayjs'],
      '@/utils/use-search': [['default', 'useSearch']],
      '@/utils/emitter': [['default', 'emitter']],
      '@/utils/page-tab-util': [
        'finishPageTab',
        'removePageTab',
        'setPageTab'
      ],
      '@/store/modules/theme': ['useThemeStore'],
      '@/store/modules/dict': ['useDictStore'],
      '@/store/modules/user': ['useUserStore'],
      'ant-design-vue': ['message', 'Modal'],
      '@ant-design/icons-vue': ['ExclamationCircleOutlined'],
      'ant-design-vue/es/form': ['useForm'],
      'ele-admin-pro': ['assignObject', 'emailReg', 'phoneReg'],
      'file-saver': ['saveAs'],
      qs: [['stringify', 'qsStringify']],
      '@vueuse/integrations/useCookies': ['useCookies']
    }
  ],
  dts: 'types/auto-imports.d.ts',
  dirs: [...API_DIR],
  vueTemplate: true,
  resolvers: [VueAmapResolver()]
})

In my project has some error,
image
image

I tried downgrading many versions and finally determined that 0.12.2 works fine, and 0.13.0 or later doesn't work.

By looking up the git logs, I found that the injectAtEnd: true attribute is the cause of this error.

For better compatibility, so I created this PR.

@default true
Inject the imports at the end of other imports
@antfu
Copy link
Member

antfu commented Feb 20, 2023

Thanks. I think we could have this - but to solve the root cause, I think we should fix the bug on unimport side. Can you create a minimal reproduction of how it cause your code to fail?

@peerless-hero
Copy link
Contributor Author

peerless-hero commented Mar 5, 2023

@antfu

In order to reproduce the problem and find out the reason, I tried many methods.
Finally, I found out if there is a superficial cause of this problem when using unplugin-vue-components in App.vue.

example

<!-- example 1 -->
<template>
  <a-config-provider>
    <router-view />
  </a-config-provider>
</template>

<script lang="ts" setup>
  import { ConfigProvider as AConfigProvider } from 'ant-design-vue/es'
</script>

It works without unplugin-vue-components in App.vue.

<!-- example 2 -->
<template>
  <a-config-provider>
    <router-view />
  </a-config-provider>
</template>

<script lang="ts" setup></script>

It doesn't works with unplugin-vue-components in App.vue.

solutions

There are only the following solutions:

  • Downgrade unplugin-auto-import to "<0.13.0" version.

By the way, no matter what version of unplugin-vue-components is used, the same will be true as long as the version of unplugin-auto-import is ">=0.13.0".

suggestion

While it is currently only found to be a problem in App.vue, I'm not sure of the inherent reasons why it occurs. I can't guarantee whether there will be similar problems in the future.
So I would like the "injectedAtEnd" option to be added as an optional insurance option for a temporary solution to a potential problem that we have not yet discovered.

@antfu
Copy link
Member

antfu commented Mar 6, 2023

@peerless-hero Thanks for investing. Can you create a minimal repo that contains the setup?

@peerless-hero
Copy link
Contributor Author

@antfu

OK!This is my summary of the cause of the error.
auto-import-error-demo

README.md is machine translated, please see the original README_CN.md.

@antfu
Copy link
Member

antfu commented Mar 23, 2023

That's quite a weird bug, and I am not sure what is causing that. Probably a bug of esbuild's pre-bundling, as the error does not occur on the build. Where you can fix it by explicit import createApp before importing ./App.vue.

Probably there is nothing we could fix here, so let's expose the option. Thanks

@antfu antfu merged commit 93a2f5c into unplugin:main Mar 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants