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: 增加 unplugin 插件配置导出 #2097

Merged
merged 2 commits into from
Feb 10, 2023
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
13 changes: 13 additions & 0 deletions jd/generate-types-taro.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ const getLocale = () => {
})
}

const getResolver = () => {
const source = path.join(sourceDir, 'resolver');
const to = path.resolve(__dirname, './../dist/resolver');
fs.cp(source, to, { recursive: true }, (err) => {
if(err) {
console.error(err);
return;
}
})
}

fs.cp(sourceDir, toDir, { recursive: true }, (err) => {
if(err) {
console.error(err);
Expand Down Expand Up @@ -99,4 +110,6 @@ declare module 'vue' {
//国际化处理
getLocale();

// resolver 类型文件
getResolver();
});
14 changes: 14 additions & 0 deletions jd/generate-types.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ const getLocale = () => {
})
}

const getResolver = () => {
const source = path.join(sourceDir, 'resolver');
const to = path.resolve(__dirname, './../dist/resolver');
fs.cp(source, to, { recursive: true }, (err) => {
if(err) {
console.error(err);
return;
}
})
}

fs.cp(sourceDir, toDir, { recursive: true }, (err) => {
if(err) {
console.error(err);
Expand Down Expand Up @@ -99,4 +110,7 @@ declare module 'vue' {

//国际化处理
getLocale();

// resolver 类型文件
getResolver();
});
10 changes: 5 additions & 5 deletions jd/generate-unplugin-deps.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ outputFileEntry += `\nexport { Locale } from "./packages/locale/lang";
export function install(app) {
const packages = [${components.join(',')}];
packages.forEach((item) => {
if (item.install) {
app.use(item);
} else if (item.name) {
app.component(item.name, item);
}
if (item.install) {
app.use(item);
} else if (item.name) {
app.component(item.name, item);
}
});
}
export const version = '${packageConfig.version}';
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
"build:site": "npm run checked && vite build",
"build:site-jdt": "npm run checked && vite build --config vite.config.jdt.ts ",
"build:site:oss": "npm run checked && vite build --base=/nutui/4x/",
"build": "npm run checked && vite build --config vite.config.build.ts && vite build --config vite.config.build.disperse.ts && npm run generate:themes && vite build --config vite.config.build.css.ts && vite build --config vite.config.build.locale.ts && npm run dts && npm run attrs && node jd/generate-unplugin-deps.cjs nutui && node jd/copy-dist.cjs nutui",
"build:taro:vue": "npm run checked:taro:vue && vite build --config vite.config.build.taro.vue.ts && vite build --config vite.config.build.taro.vue.disperse.ts && npm run generate:themes && vite build --config vite.config.build.css.ts && vite build --config vite.config.build.locale.ts && npm run dts:taro && npm run attrs:taro && node jd/generate-unplugin-deps.cjs nutui-taro && node jd/copy-dist.cjs nutui-taro",
"build": "npm run checked && vite build --config vite.config.build.ts && vite build --config vite.config.build.disperse.ts && npm run generate:themes && vite build --config vite.config.build.css.ts && vite build --config vite.config.build.locale.ts && vite build --config vite.config.build.resolver.ts && npm run dts && npm run attrs && node jd/generate-unplugin-deps.cjs nutui && node jd/copy-dist.cjs nutui",
"build:taro:vue": "npm run checked:taro:vue && vite build --config vite.config.build.taro.vue.ts && vite build --config vite.config.build.taro.vue.disperse.ts && npm run generate:themes && vite build --config vite.config.build.css.ts && vite build --config vite.config.build.locale.ts && vite build --config vite.config.build.resolver.ts && npm run dts:taro && npm run attrs:taro && node jd/generate-unplugin-deps.cjs nutui-taro && node jd/copy-dist.cjs nutui-taro",
"serve": "vite preview",
"upload": "yarn build:site:oss && node ./jd/upload.js",
"add": "node jd/createComponentMode.js",
Expand Down Expand Up @@ -132,6 +132,7 @@
"transliteration": "^2.2.0",
"ts-jest": "^26.5.5",
"typescript": "^4.9.3",
"unplugin-vue-components": "^0.23.0",
"vite": "^4.0.0",
"vite-plugin-md": "^0.21.5",
"vscode": "^1.1.37",
Expand Down
5 changes: 3 additions & 2 deletions publish/nutui-taro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@
"publish:latest": "npm publish"
},
"peerDependencies": {
"@nutui/icons-vue-taro": "^0.0.9"
"unplugin-vue-components": "^0.23.0"
},
"dependencies": {
"sass": "^1.50.0"
"sass": "^1.50.0",
"@nutui/icons-vue-taro": "^0.0.9"
},
"devDependencies": {},
"repository": {
Expand Down
5 changes: 3 additions & 2 deletions publish/nutui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@
"publish:latest": "npm publish"
},
"peerDependencies": {
"@nutui/icons-vue": "^0.0.25"
"unplugin-vue-components": "^0.23.0"
},
"dependencies": {
"sass": "^1.50.0"
"sass": "^1.50.0",
"@nutui/icons-vue": "^0.0.25"
},
"devDependencies": {},
"repository": {
Expand Down
53 changes: 53 additions & 0 deletions src/packages/resolver/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { ComponentResolveResult, ComponentResolver } from 'unplugin-vue-components/types';

export interface NutUIResolverOptions {
/**
* NutUI or NutUI-Taro
*
* @default false
*/
taro?: boolean;

/**
* compatible with unplugin-auto-import
*
* @default false
*/
autoImport?: boolean;
}

const nutFunctions = ['showToast', 'showNotify', 'showDialog', 'showImagePreview'];

function getNutResolved(name: string, options: NutUIResolverOptions): ComponentResolveResult {
const { taro = false, autoImport = false } = options;

const packageName = taro ? '@nutui/nutui-taro' : '@nutui/nutui';

const componentName = autoImport ? name.slice(4) : name;

const style = `${packageName}/dist/packages/${componentName.toLowerCase()}/style`;

return {
name,
from: packageName,
sideEffects: style
};
}

/**
* Resolver for NutUI 4.0+
*
* @link https://github.com/jdf2e/nutui
*/
export default function NutUIResolver(options: NutUIResolverOptions = {}): ComponentResolver {
return {
type: 'component',
resolve: (name) => {
const { autoImport = false } = options;

if (autoImport && nutFunctions.includes(name)) return getNutResolved(name, options);

if (name.startsWith('Nut')) return getNutResolved(name.slice(3), options);
}
};
}
1 change: 1 addition & 0 deletions tsconfig.declaration.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"src/packages/nutui.vue.build.ts",
"src/packages/locale",
"src/packages/utils",
"src/packages/resolver",
],
"exclude": [
"node_modules",
Expand Down
1 change: 1 addition & 0 deletions tsconfig.declaration.taro.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"src/packages/nutui.taro.vue.build.ts",
"src/packages/locale",
"src/packages/utils",
"src/packages/resolver",
],
"exclude": [
"node_modules",
Expand Down
28 changes: 28 additions & 0 deletions vite.config.build.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { defineConfig } from 'vite';
import path from 'path';
import vue from '@vitejs/plugin-vue';

export default defineConfig({
plugins: [vue()],
build: {
minify: true,
lib: {
entry: path.resolve(__dirname, './src/packages/resolver/index.ts'),
name: 'name',
fileName: (format) => {
if (format === 'es') {
return 'index.mjs';
} else {
return 'index.js';
}
},
formats: ['es', 'cjs']
},
rollupOptions: {
output: {
dir: path.resolve(__dirname, './dist/resolver')
}
},
emptyOutDir: false
}
});