import fse from 'fs-extra' ; import { sep , join } from 'path' ; import { SRC_DIR , getVantConfig } from './constant.js' ; import type { InlineConfig } 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 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)$/ ; export const JSX_REGEXP = /\.(j|t)sx$/ ; export const ENTRY_EXTS = [ 'js' , 'ts' , 'tsx' , 'jsx' , 'vue' ] ; export function removeExt ( path ) { return path . replace ( '.js' , '' ) ; } export function replaceExt ( path , ext ) { return path . replace ( EXT_REGEXP , ext ) ; } export function hasDefaultExport ( code ) { return code . includes ( 'export default' ) || code . includes ( 'export { default }' ) ; } export function getComponents ( ) { const EXCLUDES = [ '.DS_Store' ] ; const dirs = readdirSync ( SRC_DIR ) ; return dirs . filter ( ( dir ) => ! EXCLUDES . includes ( dir ) ) . filter ( ( dir ) => ENTRY_EXTS . some ( ( ext ) => { const path = join ( SRC_DIR , dir , `index.${ ext }` ) ; if ( existsSync ( path ) ) { return hasDefaultExport ( readFileSync ( path , 'utf-8' ) ) ; } return false ; } ) ) ; } export const isDir = ( dir ) => lstatSync ( dir ) . isDirectory ( ) ; export const isDemoDir = ( dir ) => DEMO_REGEXP . test ( dir ) ; export const isTestDir = ( dir ) => TEST_REGEXP . test ( dir ) ; export const isAsset = ( path ) => ASSET_REGEXP . test ( path ) ; export const isSfc = ( path ) => SFC_REGEXP . test ( path ) ; export const isStyle = ( path ) => STYLE_REGEXP . test ( path ) ; export const isScript = ( path ) => SCRIPT_REGEXP . test ( path ) ; export const isJsx = ( path ) => JSX_REGEXP . test ( path ) ; const camelizeRE = /-(\w)/g ; const pascalizeRE = /(\w)(\w*)/g ; export function camelize ( str ) { return str . replace ( camelizeRE , ( _ , c ) => c . toUpperCase ( ) ) ; } export function pascalize ( str ) { return camelize ( str ) . replace ( pascalizeRE , ( _ , c1 , c2 ) => c1 . toUpperCase ( ) + c2 ) ; } export function decamelize ( str , 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' ) . toLowerCase ( ) ; } export function normalizePath ( path ) { return path . replace ( /\\/g , '/' ) ; } export type ModuleEnv = 'esmodule' | 'commonjs' ; export type NodeEnv = 'production' | 'development' | 'test' ; export type BuildTarget = 'site' | 'package' ; export function setModuleEnv ( value ) { process . env . BABEL_MODULE = value ; } export function setNodeEnv ( value ) { process . env . NODE_ENV = value ; } export function setBuildTarget ( value ) { process . env . BUILD_TARGET = value ; } export function isDev ( ) { return process . env . NODE_ENV === 'development' ; } export function smartOutputFile ( filePath , content ) { if ( existsSync ( filePath ) ) { const previousContent = readFileSync ( filePath , 'utf-8' ) ; if ( previousContent === content ) { return ; } } outputFileSync ( filePath , content ) ; } export function mergeCustomViteConfig ( config ) { const vantConfig = getVantConfig ( ) ; const configureVite = vantConfig . build ?. configureVite ; if ( configureVite ) { return configureVite ( config ) ; } return config ; } export { getVantConfig } ;