Skip to content

Commit

Permalink
fix(vant-cli): support defineOptions named (youzan#12734)
Browse files Browse the repository at this point in the history
  • Loading branch information
wChenonly authored and bluesky335 committed Apr 1, 2024
1 parent af23726 commit a883a48
Showing 1 changed file with 12 additions and 8 deletions.
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

0 comments on commit a883a48

Please sign in to comment.