docs(auto-import-resolver): update README and folder name (#12241)

* docs(auto-import-resolver): update README and folder name

* chore: update lock

* docs: fix path
This commit is contained in:
neverland
2023-09-03 09:28:13 +08:00
committed by GitHub
parent d1538c3489
commit 80f600af93
11 changed files with 38 additions and 84 deletions
@@ -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.
@@ -0,0 +1,112 @@
# Vant Auto Import Resolver
[English](./README.md) | 简体中文
`@vant/auto-import-resolver` is a resolver for [unplugin-vue-components](https://github.com/unplugin/unplugin-vue-components) that enables on-demand importing of Vant components.
### Features
- Supports `Vite`, `Webpack`, `Rspack`, `Vue CLI`, `Rollup`, `esbuild`, and more.
- Automatically imports the corresponding CSS styles for the components.
- Supports SSR (Server-Side Rendering).
### Installation
```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
```
## Usage
### Vite
```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()],
}),
],
});
```
### Rollup
```ts
// rollup.config.js
import Components from 'unplugin-vue-components/rollup';
import { VantResolver } from '@vant/auto-import-resolver';
export default {
plugins: [
Components({
resolvers: [VantResolver()],
}),
],
};
```
### Webpack
```ts
// webpack.config.js
import Components from 'unplugin-vue-components/webpack';
import { VantResolver } from '@vant/auto-import-resolver';
module.exports = {
plugins: [
Components({
resolvers: [VantResolver()],
}),
],
};
```
### Vue CLI
```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()],
}),
],
},
};
```
### esbuild
```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()],
}),
],
});
```
@@ -0,0 +1,112 @@
# Vant Auto Import Resolver
English | [简体中文](./README.zh-CN.md)
`@vant/auto-import-resolver` 是 [unplugin-vue-components](https://github.com/unplugin/unplugin-vue-components) 的一个解析器,用于实现 Vant 按需引入。
### 特性
- 支持 `Vite`, `Webpack`, `Rspack`, `Vue CLI`, `Rollup`, `esbuild`
- 支持自动引入组件对应的 CSS 样式
- 支持 SSR(服务端渲染)
### 安装
```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
```
## 使用
### Vite
```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()],
}),
],
});
```
### Rollup
```ts
// rollup.config.js
import Components from 'unplugin-vue-components/rollup';
import { VantResolver } from '@vant/auto-import-resolver';
export default {
plugins: [
Components({
resolvers: [VantResolver()],
}),
],
};
```
### Webpack
```ts
// webpack.config.js
import Components from 'unplugin-vue-components/webpack';
import { VantResolver } from '@vant/auto-import-resolver';
module.exports = {
plugins: [
Components({
resolvers: [VantResolver()],
}),
],
};
```
### Vue CLI
```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()],
}),
],
},
};
```
### esbuild
```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()],
}),
],
});
```
@@ -0,0 +1 @@
require('../vant-use/build');
@@ -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/vant-auto-import-resolver"
},
"bugs": "https://github.com/vant-ui/vant/issues",
"author": "chenjiahan",
"license": "MIT",
"devDependencies": {
"rimraf": "^5.0.0",
"typescript": "^5.0.4"
}
}
@@ -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),
};
}
},
};
}
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig",
"compilerOptions": {
"outDir": "./dist",
"declaration": true
},
"include": ["src/**/*"],
"exclude": ["**/node_modules", "**/.*/"]
}
@@ -0,0 +1,5 @@
export default {
build: {
packageManager: 'pnpm',
},
};