mirror of
https://github.com/youzan/vant.git
synced 2025-10-22 03:44:48 +00:00
feat(auto-import-resolver): add auto-import-resolver package (#12227)
This commit is contained in:
10
packages/auto-import-resolver/LICENSE
Normal file
10
packages/auto-import-resolver/LICENSE
Normal file
@@ -0,0 +1,10 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) Youzan
|
||||
Copyright (c) Chen Jiahan and other contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
133
packages/auto-import-resolver/README.md
Normal file
133
packages/auto-import-resolver/README.md
Normal file
@@ -0,0 +1,133 @@
|
||||
# Vant Auto Import Resolver
|
||||
|
||||
@vant/auto-import-resolver For Vant on-demand introduction, parser based on `unplugin-vue-components`
|
||||
|
||||
🇨🇳 <a href="./README.zh-CN.md">查看中文版介绍</a>
|
||||
|
||||
---
|
||||
|
||||
### Features
|
||||
|
||||
- Supports `Vite`, `Webpack`, `Rspack`, `Vue CLI`, `Rollup`, `esbuild` and more, powered by <a href="https://github.com/antfu/unplugin-vue-components">unplugin-vue-components</a>
|
||||
|
||||
- Support css style `VantResolver()` default
|
||||
|
||||
- Support less style `VantResolver({ importStyle: false | 'less' })`
|
||||
|
||||
> Tip: Vant 4.0 the less file has been removed from the above npm package
|
||||
|
||||
- Support ssr `VantResolver({ ssr: true })`
|
||||
|
||||
### Install Manually
|
||||
|
||||
```shell
|
||||
# via npm
|
||||
npm i @vant/auto-import-resolver unplugin-vue-components -D
|
||||
|
||||
# via yarn
|
||||
yarn add @vant/auto-import-resolver unplugin-vue-components -D
|
||||
|
||||
# via pnpm
|
||||
pnpm add @vant/auto-import-resolver unplugin-vue-components -D
|
||||
|
||||
# via Bun
|
||||
bun add @vant/auto-import-resolver unplugin-vue-components -D
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Vite</summary><br>
|
||||
|
||||
```ts
|
||||
// vite.config.ts
|
||||
import Components from 'unplugin-vue-components/vite';
|
||||
import { VantResolver } from '@vant/auto-import-resolver';
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
Components({
|
||||
resolvers: [VantResolver()],
|
||||
}),
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
<br></details>
|
||||
|
||||
<details>
|
||||
<summary>Rollup</summary><br>
|
||||
|
||||
```ts
|
||||
// rollup.config.js
|
||||
import Components from 'unplugin-vue-components/rollup';
|
||||
import { VantResolver } from '@vant/auto-import-resolver';
|
||||
|
||||
export default {
|
||||
plugins: [
|
||||
Components({
|
||||
resolvers: [VantResolver()],
|
||||
}),
|
||||
],
|
||||
};
|
||||
```
|
||||
|
||||
<br></details>
|
||||
|
||||
<details>
|
||||
<summary>Webpack</summary><br>
|
||||
|
||||
```ts
|
||||
// webpack.config.js
|
||||
import Components from 'unplugin-vue-components/webpack';
|
||||
import { VantResolver } from '@vant/auto-import-resolver';
|
||||
|
||||
module.exports = {
|
||||
plugins: [
|
||||
Components({
|
||||
resolvers: [VantResolver()],
|
||||
}),
|
||||
],
|
||||
};
|
||||
```
|
||||
|
||||
<br></details>
|
||||
|
||||
<details>
|
||||
<summary>Vue CLI</summary><br>
|
||||
|
||||
```ts
|
||||
// vue.config.js
|
||||
import Components from 'unplugin-vue-components/webpack';
|
||||
import { VantResolver } from '@vant/auto-import-resolver';
|
||||
|
||||
module.exports = {
|
||||
configureWebpack: {
|
||||
plugins: [
|
||||
Components({
|
||||
resolvers: [VantResolver()],
|
||||
}),
|
||||
],
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
<br></details>
|
||||
|
||||
<details>
|
||||
<summary>esbuild</summary><br>
|
||||
|
||||
```ts
|
||||
// esbuild.config.js
|
||||
import { build } from 'esbuild';
|
||||
import Components from 'unplugin-vue-components/esbuild';
|
||||
import { VantResolver } from '@vant/auto-import-resolver';
|
||||
|
||||
build({
|
||||
plugins: [
|
||||
Components({
|
||||
resolvers: [VantResolver()],
|
||||
}),
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
<br></details>
|
129
packages/auto-import-resolver/README.zh-CN.md
Normal file
129
packages/auto-import-resolver/README.zh-CN.md
Normal file
@@ -0,0 +1,129 @@
|
||||
# Vant Auto Import Resolver
|
||||
|
||||
@vant/auto-import-resolver 用于 Vant 按需引入,基于 unplugin-vue-components 的解析器
|
||||
|
||||
### 特性
|
||||
|
||||
- 支持 `Vite`, `Webpack`, `Rspack`, `Vue CLI`, `Rollup`, `esbuild` 等, 基于 <a href="https://github.com/antfu/unplugin-vue-components">unplugin-vue-components</a>
|
||||
|
||||
- 支持自动引入 css 样式 `VantResolver()` 默认
|
||||
|
||||
- 支持自动引入 less 样式 `VantResolver({ importStyle: false | 'less' })`
|
||||
|
||||
> 提示:Vant 4.0 以上 npm 包内移除了 less 文件
|
||||
|
||||
- 支持 ssr `VantResolver({ ssr: true })`
|
||||
|
||||
### 安装
|
||||
|
||||
```shell
|
||||
# via npm
|
||||
npm i @vant/auto-import-resolver unplugin-vue-components -D
|
||||
|
||||
# via yarn
|
||||
yarn add @vant/auto-import-resolver unplugin-vue-components -D
|
||||
|
||||
# via pnpm
|
||||
pnpm add @vant/auto-import-resolver unplugin-vue-components -D
|
||||
|
||||
# via Bun
|
||||
bun add @vant/auto-import-resolver unplugin-vue-components -D
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Vite</summary><br>
|
||||
|
||||
```ts
|
||||
// vite.config.ts
|
||||
import Components from 'unplugin-vue-components/vite';
|
||||
import { VantResolver } from '@vant/auto-import-resolver';
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
Components({
|
||||
resolvers: [VantResolver()],
|
||||
}),
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
<br></details>
|
||||
|
||||
<details>
|
||||
<summary>Rollup</summary><br>
|
||||
|
||||
```ts
|
||||
// rollup.config.js
|
||||
import Components from 'unplugin-vue-components/rollup';
|
||||
import { VantResolver } from '@vant/auto-import-resolver';
|
||||
|
||||
export default {
|
||||
plugins: [
|
||||
Components({
|
||||
resolvers: [VantResolver()],
|
||||
}),
|
||||
],
|
||||
};
|
||||
```
|
||||
|
||||
<br></details>
|
||||
|
||||
<details>
|
||||
<summary>Webpack</summary><br>
|
||||
|
||||
```ts
|
||||
// webpack.config.js
|
||||
import Components from 'unplugin-vue-components/webpack';
|
||||
import { VantResolver } from '@vant/auto-import-resolver';
|
||||
|
||||
module.exports = {
|
||||
plugins: [
|
||||
Components({
|
||||
resolvers: [VantResolver()],
|
||||
}),
|
||||
],
|
||||
};
|
||||
```
|
||||
|
||||
<br></details>
|
||||
|
||||
<details>
|
||||
<summary>Vue CLI</summary><br>
|
||||
|
||||
```ts
|
||||
// vue.config.js
|
||||
import Components from 'unplugin-vue-components/webpack';
|
||||
import { VantResolver } from '@vant/auto-import-resolver';
|
||||
|
||||
module.exports = {
|
||||
configureWebpack: {
|
||||
plugins: [
|
||||
Components({
|
||||
resolvers: [VantResolver()],
|
||||
}),
|
||||
],
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
<br></details>
|
||||
|
||||
<details>
|
||||
<summary>esbuild</summary><br>
|
||||
|
||||
```ts
|
||||
// esbuild.config.js
|
||||
import { build } from 'esbuild';
|
||||
import Components from 'unplugin-vue-components/esbuild';
|
||||
import { VantResolver } from '@vant/auto-import-resolver';
|
||||
|
||||
build({
|
||||
plugins: [
|
||||
Components({
|
||||
resolvers: [VantResolver()],
|
||||
}),
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
<br></details>
|
1
packages/auto-import-resolver/build.js
Normal file
1
packages/auto-import-resolver/build.js
Normal file
@@ -0,0 +1 @@
|
||||
require('../vant-use/build');
|
44
packages/auto-import-resolver/package.json
Normal file
44
packages/auto-import-resolver/package.json
Normal file
@@ -0,0 +1,44 @@
|
||||
{
|
||||
"name": "@vant/auto-import-resolver",
|
||||
"version": "1.0.0",
|
||||
"description": "Vant auto import resolver base unplugin-vue-components",
|
||||
"main": "dist/index.cjs.js",
|
||||
"module": "dist/index.esm.mjs",
|
||||
"typings": "dist/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/index.d.ts",
|
||||
"import": "./dist/index.esm.mjs",
|
||||
"require": "./dist/index.cjs.js"
|
||||
}
|
||||
},
|
||||
"sideEffects": false,
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"clean": "rimraf ./dist",
|
||||
"dev": "node ./build.js -w",
|
||||
"build:types": "tsc -p ./tsconfig.json --emitDeclarationOnly",
|
||||
"build:bundle": "node ./build.js",
|
||||
"build": "pnpm clean && pnpm build:bundle && pnpm build:types",
|
||||
"release": "vant-cli release",
|
||||
"prepare": "pnpm build"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public",
|
||||
"registry": "https://registry.npmjs.org/"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/vant-ui/vant.git",
|
||||
"directory": "packages/auto-import-resolver"
|
||||
},
|
||||
"bugs": "https://github.com/vant-ui/vant/issues",
|
||||
"author": "chenjiahan",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"rimraf": "^5.0.0",
|
||||
"typescript": "^5.0.4"
|
||||
}
|
||||
}
|
58
packages/auto-import-resolver/src/index.ts
Normal file
58
packages/auto-import-resolver/src/index.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
export interface VantResolverOptions {
|
||||
/**
|
||||
* import style css or less along with components
|
||||
*
|
||||
* @default true
|
||||
*/
|
||||
importStyle?: boolean | 'css' | 'less';
|
||||
|
||||
/**
|
||||
* use lib
|
||||
*
|
||||
* @default false
|
||||
*/
|
||||
ssr?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Button->button; ButtonGroup->button-group
|
||||
*/
|
||||
function kebabCase(key: string) {
|
||||
const result = key.replace(/([A-Z])/g, ' $1').trim();
|
||||
return result.split(' ').join('-').toLowerCase();
|
||||
}
|
||||
|
||||
function getModuleType(ssr: boolean): string {
|
||||
return ssr ? 'lib' : 'es';
|
||||
}
|
||||
|
||||
function getSideEffects(dirName: string, options: VantResolverOptions) {
|
||||
const { importStyle = true, ssr = false } = options;
|
||||
if (!importStyle) return;
|
||||
|
||||
const moduleType = getModuleType(ssr);
|
||||
|
||||
if (importStyle === 'less') return `vant/${moduleType}/${dirName}/style/less`;
|
||||
|
||||
return `vant/${moduleType}/${dirName}/style/index`;
|
||||
}
|
||||
|
||||
export function VantResolver(options: VantResolverOptions = {}) {
|
||||
const { ssr = false } = options;
|
||||
|
||||
const moduleType = getModuleType(ssr);
|
||||
|
||||
return {
|
||||
type: 'component',
|
||||
resolve: (name: string) => {
|
||||
if (name.startsWith('Van')) {
|
||||
const partialName = name.slice(3);
|
||||
return {
|
||||
name: partialName,
|
||||
from: `vant/${moduleType}`,
|
||||
sideEffects: getSideEffects(kebabCase(partialName), options),
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
9
packages/auto-import-resolver/tsconfig.json
Normal file
9
packages/auto-import-resolver/tsconfig.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"declaration": true
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["**/node_modules", "**/.*/"]
|
||||
}
|
5
packages/auto-import-resolver/vant.config.mjs
Normal file
5
packages/auto-import-resolver/vant.config.mjs
Normal file
@@ -0,0 +1,5 @@
|
||||
export default {
|
||||
build: {
|
||||
packageManager: 'pnpm',
|
||||
},
|
||||
};
|
Reference in New Issue
Block a user