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

fix(vant-cli): support defineOptions named #12734

Merged
merged 12 commits into from
Mar 26, 2024
20 changes: 12 additions & 8 deletions packages/vant-cli/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import fse from 'fs-extra';
import { sep, join } from 'node:path';
import { SRC_DIR, getVantConfig } from './constant.js';
import { InlineConfig, loadConfigFromFile, mergeConfig } from 'vite';
import { type InlineConfig, loadConfigFromFile, mergeConfig } from 'vite';

const { lstatSync, existsSync, readdirSync, readFileSync, outputFileSync } =
fse;

export const EXT_REGEXP = /\.\w+$/;
export const SFC_REGEXP = /\.(vue)$/;
export const DEMO_REGEXP = new RegExp('\\' + sep + 'demo$');
export const TEST_REGEXP = new RegExp('\\' + sep + 'test$');
export const DEMO_REGEXP = new RegExp(`\\${sep}demo$`);
export const TEST_REGEXP = new RegExp(`\\${sep}test$`);
export const ASSET_REGEXP = /\.(png|jpe?g|gif|webp|ico|jfif|svg|woff2?|ttf)$/i;
export const STYLE_REGEXP = /\.(css|less|scss)$/;
export const SCRIPT_REGEXP = /\.(js|ts|jsx|tsx)$/;
Expand All @@ -24,8 +24,12 @@ export function replaceExt(path: string, ext: string) {
return path.replace(EXT_REGEXP, ext);
}

export function hasDefaultExport(code: string) {
return code.includes('export default') || code.includes('export { default }');
export function hasExportOrDefineOptions(code: string) {
return (
code.includes('export default') ||
code.includes('export { default }') ||
code.includes('defineOptions')
);
}

export function getComponents() {
Expand All @@ -38,7 +42,7 @@ export function getComponents() {
ENTRY_EXTS.some((ext) => {
const path = join(SRC_DIR, dir, `index.${ext}`);
if (existsSync(path)) {
return hasDefaultExport(readFileSync(path, 'utf-8'));
return hasExportOrDefineOptions(readFileSync(path, 'utf-8'));
}

return false;
Expand Down Expand Up @@ -71,8 +75,8 @@ export function pascalize(str: string): string {

export function decamelize(str: string, sep = '-') {
return str
.replace(/([a-z\d])([A-Z])/g, '$1' + sep + '$2')
.replace(/([A-Z])([A-Z][a-z\d]+)/g, '$1' + sep + '$2')
.replace(/([a-z\d])([A-Z])/g, `$1${sep}$2`)
.replace(/([A-Z])([A-Z][a-z\d]+)/g, `$1${sep}$2`)
.toLowerCase();
}

Expand Down