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: supports imports option for auto-import #12861

Merged
merged 1 commit into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions packages/vant-auto-import-resolver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ bun add @vant/auto-import-resolver unplugin-vue-components unplugin-auto-import
// vite.config.ts
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from '@vant/auto-import-resolver';
import { VantResolver, VantImports } from '@vant/auto-import-resolver';

export default defineConfig({
plugins: [
AutoImport({
imports: [VantImports()],
resolvers: [VantResolver()],
}),
Components({
Expand All @@ -54,11 +55,12 @@ export default defineConfig({
// rollup.config.js
import AutoImport from 'unplugin-auto-import/rollup';
import Components from 'unplugin-vue-components/rollup';
import { VantResolver } from '@vant/auto-import-resolver';
import { VantResolver, VantImports } from '@vant/auto-import-resolver';

export default {
plugins: [
AutoImport({
imports: [VantImports()],
resolvers: [VantResolver()],
}),
Components({
Expand All @@ -74,11 +76,12 @@ export default {
// webpack.config.js
import AutoImport from 'unplugin-auto-import/webpack';
import Components from 'unplugin-vue-components/webpack';
import { VantResolver } from '@vant/auto-import-resolver';
import { VantResolver, VantImports } from '@vant/auto-import-resolver';

module.exports = {
plugins: [
AutoImport({
imports: [VantImports()],
resolvers: [VantResolver()],
}),
Components({
Expand All @@ -94,11 +97,12 @@ module.exports = {
// rspack.config.js
import AutoImport from 'unplugin-auto-import/rspack';
import Components from 'unplugin-vue-components/rspack';
import { VantResolver } from '@vant/auto-import-resolver';
import { VantResolver, VantImports } from '@vant/auto-import-resolver';

module.exports = {
plugins: [
AutoImport({
imports: [VantImports()],
resolvers: [VantResolver()],
}),
Components({
Expand All @@ -114,12 +118,13 @@ module.exports = {
// vue.config.js
import AutoImport from 'unplugin-auto-import/webpack';
import Components from 'unplugin-vue-components/webpack';
import { VantResolver } from '@vant/auto-import-resolver';
import { VantResolver, VantImports } from '@vant/auto-import-resolver';

module.exports = {
configureWebpack: {
plugins: [
AutoImport({
imports: [VantImports()],
resolvers: [VantResolver()],
}),
Components({
Expand All @@ -137,11 +142,12 @@ module.exports = {
import { build } from 'esbuild';
import AutoImport from 'unplugin-auto-import/esbuild';
import Components from 'unplugin-vue-components/esbuild';
import { VantResolver } from '@vant/auto-import-resolver';
import { VantResolver, VantImports } from '@vant/auto-import-resolver';

build({
plugins: [
AutoImport({
imports: [VantImports()],
resolvers: [VantResolver()],
}),
Components({
Expand Down
18 changes: 12 additions & 6 deletions packages/vant-auto-import-resolver/README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ bun add @vant/auto-import-resolver unplugin-vue-components unplugin-auto-import
// vite.config.ts
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from '@vant/auto-import-resolver';
import { VantResolver, VantImports } from '@vant/auto-import-resolver';

export default defineConfig({
plugins: [
AutoImport({
imports: [VantImports()],
resolvers: [VantResolver()],
}),
Components({
Expand All @@ -54,11 +55,12 @@ export default defineConfig({
// rollup.config.js
import AutoImport from 'unplugin-auto-import/rollup';
import Components from 'unplugin-vue-components/rollup';
import { VantResolver } from '@vant/auto-import-resolver';
import { VantResolver, VantImports } from '@vant/auto-import-resolver';

export default {
plugins: [
AutoImport({
imports: [VantImports()],
resolvers: [VantResolver()],
}),
Components({
Expand All @@ -74,11 +76,12 @@ export default {
// webpack.config.js
import AutoImport from 'unplugin-auto-import/webpack';
import Components from 'unplugin-vue-components/webpack';
import { VantResolver } from '@vant/auto-import-resolver';
import { VantResolver, VantImports } from '@vant/auto-import-resolver';

module.exports = {
plugins: [
AutoImport({
imports: [VantImports()],
resolvers: [VantResolver()],
}),
Components({
Expand All @@ -94,11 +97,12 @@ module.exports = {
// rspack.config.js
import AutoImport from 'unplugin-auto-import/rspack';
import Components from 'unplugin-vue-components/rspack';
import { VantResolver } from '@vant/auto-import-resolver';
import { VantResolver, VantImports } from '@vant/auto-import-resolver';

module.exports = {
plugins: [
AutoImport({
imports: [VantImports()],
resolvers: [VantResolver()],
}),
Components({
Expand All @@ -114,12 +118,13 @@ module.exports = {
// vue.config.js
import AutoImport from 'unplugin-auto-import/webpack';
import Components from 'unplugin-vue-components/webpack';
import { VantResolver } from '@vant/auto-import-resolver';
import { VantResolver, VantImports } from '@vant/auto-import-resolver';

module.exports = {
configureWebpack: {
plugins: [
AutoImport({
imports: [VantImports()],
resolvers: [VantResolver()],
}),
Components({
Expand All @@ -137,11 +142,12 @@ module.exports = {
import { build } from 'esbuild';
import AutoImport from 'unplugin-auto-import/esbuild';
import Components from 'unplugin-vue-components/esbuild';
import { VantResolver } from '@vant/auto-import-resolver';
import { VantResolver, VantImports } from '@vant/auto-import-resolver';

build({
plugins: [
AutoImport({
imports: [VantImports()],
resolvers: [VantResolver()],
}),
Components({
Expand Down
10 changes: 10 additions & 0 deletions packages/vant-auto-import-resolver/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export interface VantResolverOptions {
ssr?: boolean;
}

export type VantImportsOptions = Pick<VantResolverOptions, 'module' | 'ssr'>;

/**
* Button->button; ButtonGroup->button-group
*/
Expand Down Expand Up @@ -124,3 +126,11 @@ export function VantResolver(options: VantResolverOptions = {}) {
},
};
}

export function VantImports(options: VantImportsOptions = {}) {
const moduleType = getModuleType(options);

return {
[`vant/${moduleType}`]: getAPIMap().keys(),
};
}
26 changes: 17 additions & 9 deletions packages/vant/docs/markdown/quickstart.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,15 @@ import { defineConfig } from '@rsbuild/core';
import { pluginVue } from '@rsbuild/plugin-vue';
import AutoImport from 'unplugin-auto-import/rspack';
import Components from 'unplugin-vue-components/rspack';
import { VantResolver } from '@vant/auto-import-resolver';
import { VantResolver, VantImports } from '@vant/auto-import-resolver';

export default defineConfig({
plugins: [pluginVue()],
tools: {
rspack: {
plugins: [
AutoImport({
imports: [VantImports()],
resolvers: [VantResolver()],
}),
Components({
Expand All @@ -177,12 +178,13 @@ For Vite based project,configure the plugin in the `vite.config.js` file:
import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from '@vant/auto-import-resolver';
import { VantResolver, VantImports } from '@vant/auto-import-resolver';

export default {
plugins: [
vue(),
AutoImport({
imports: [VantImports()],
resolvers: [VantResolver()],
}),
Components({
Expand All @@ -195,18 +197,21 @@ export default {
For vue-cli based project,configure the plugin in the `vue.config.js` file:

```js
const { VantResolver } = require('@vant/auto-import-resolver');
const { VantResolver, VantImports } = require('@vant/auto-import-resolver');
const AutoImport = require('unplugin-auto-import/webpack');
const Components = require('unplugin-vue-components/webpack');

module.exports = {
configureWebpack: {
plugins: [
// When the version of unplugin-vue-components is less than 0.26.0:
AutoImport({ resolvers: [VantResolver()] }),
AutoImport({ imports: [VantImports()], resolvers: [VantResolver()] }),
Components({ resolvers: [VantResolver()] }),
// when the unplugin-vue-components version is greater than or equal to 0.26.0:
AutoImport.default({ resolvers: [VantResolver()] }),
AutoImport.default({
imports: [VantImports()],
resolvers: [VantResolver()],
}),
Components.default({ resolvers: [VantResolver()] }),
],
},
Expand All @@ -216,17 +221,20 @@ module.exports = {
For webpack based project,configure the plugin in the `webpack.config.js` file:

```js
const { VantResolver } = require('@vant/auto-import-resolver');
const { VantResolver, VantImports } = require('@vant/auto-import-resolver');
const AutoImport = require('unplugin-auto-import/webpack');
const Components = require('unplugin-vue-components/webpack');

module.exports = {
plugins: [
// When the version of unplugin-vue-components is less than 0.26.0:
AutoImport({ resolvers: [VantResolver()] }),
AutoImport({ imports: [VantImports()], resolvers: [VantResolver()] }),
Components({ resolvers: [VantResolver()] }),
// when the unplugin-vue-components version is greater than or equal to 0.26.0:
AutoImport.default({ resolvers: [VantResolver()] }),
AutoImport.default({
imports: [VantImports()],
resolvers: [VantResolver()],
}),
Components.default({ resolvers: [VantResolver()] }),
],
};
Expand All @@ -246,7 +254,7 @@ Then you can using components from Vant in the template, the `unplugin-vue-compo

```html
<script>
showToast('No need to import showToast')
showToast('No need to import showToast');
</script>
```

Expand Down
26 changes: 17 additions & 9 deletions packages/vant/docs/markdown/quickstart.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,15 @@ import { defineConfig } from '@rsbuild/core';
import { pluginVue } from '@rsbuild/plugin-vue';
import AutoImport from 'unplugin-auto-import/rspack';
import Components from 'unplugin-vue-components/rspack';
import { VantResolver } from '@vant/auto-import-resolver';
import { VantResolver, VantImports } from '@vant/auto-import-resolver';

export default defineConfig({
plugins: [pluginVue()],
tools: {
rspack: {
plugins: [
AutoImport({
imports: [VantImports()],
resolvers: [VantResolver()],
}),
Components({
Expand All @@ -199,12 +200,13 @@ export default defineConfig({
import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { VantResolver } from '@vant/auto-import-resolver';
import { VantResolver, VantImports } from '@vant/auto-import-resolver';

export default {
plugins: [
vue(),
AutoImport({
imports: [VantImports()],
resolvers: [VantResolver()],
}),
Components({
Expand All @@ -217,18 +219,21 @@ export default {
如果是基于 vue-cli 的项目,在 `vue.config.js` 文件中配置插件:

```js
const { VantResolver } = require('@vant/auto-import-resolver');
const { VantResolver, VantImports } = require('@vant/auto-import-resolver');
const AutoImport = require('unplugin-auto-import/webpack');
const Components = require('unplugin-vue-components/webpack');

module.exports = {
configureWebpack: {
plugins: [
// 当 unplugin-vue-components 版本小于 0.26.0 时,使用以下写法
AutoImport({ resolvers: [VantResolver()] }),
AutoImport({ imports: [VantImports()], resolvers: [VantResolver()] }),
Components({ resolvers: [VantResolver()] }),
//当 unplugin-vue-components 版本大于等于 0.26.0 时,使用以下写法
AutoImport.default({ resolvers: [VantResolver()] }),
AutoImport.default({
imports: [VantImports()],
resolvers: [VantResolver()],
}),
Components.default({ resolvers: [VantResolver()] }),
],
},
Expand All @@ -238,17 +243,20 @@ module.exports = {
如果是基于 webpack 的项目,在 `webpack.config.js` 文件中配置插件:

```js
const { VantResolver } = require('@vant/auto-import-resolver');
const { VantResolver, VantImports } = require('@vant/auto-import-resolver');
const AutoImport = require('unplugin-auto-import/webpack');
const Components = require('unplugin-vue-components/webpack');

module.exports = {
plugins: [
// 当 unplugin-vue-components 版本小于 0.26.0 时,使用以下写法
AutoImport({ resolvers: [VantResolver()] }),
AutoImport({ imports: [VantImports()], resolvers: [VantResolver()] }),
Components({ resolvers: [VantResolver()] }),
//当 unplugin-vue-components 版本大于等于 0.26.0 时,使用以下写法
AutoImport.default({ resolvers: [VantResolver()] }),
AutoImport.default({
imports: [VantImports()],
resolvers: [VantResolver()],
}),
Components.default({ resolvers: [VantResolver()] }),
],
};
Expand All @@ -268,7 +276,7 @@ module.exports = {

```html
<script>
showToast('No need to import showToast')
showToast('No need to import showToast');
</script>
```

Expand Down
Loading