Core constants, regular expressions, file extensions, build configurations, and TypeScript interfaces used throughout the uni-app ecosystem for consistent development and platform compatibility.
Standardized file extension definitions for different file types in uni-app projects.
/**
* JavaScript and TypeScript file extensions supported by uni-app
*/
const EXTNAME_JS: string[];
/**
* TypeScript-specific file extensions
*/
const EXTNAME_TS: string[];
/**
* Vue component file extensions including NVUE and UVUE
*/
const EXTNAME_VUE: string[];
/**
* UniApp X Vue component extensions with priority order
*/
const X_EXTNAME_VUE: string[];
/**
* Template file extensions that support Vue template compilation
*/
const EXTNAME_VUE_TEMPLATE: string[];Values:
EXTNAME_JS: ['.js', '.ts', '.jsx', '.tsx', '.uts']EXTNAME_TS: ['.ts', '.tsx']EXTNAME_VUE: ['.vue', '.nvue', '.uvue']X_EXTNAME_VUE: ['.uvue', '.vue']EXTNAME_VUE_TEMPLATE: ['.vue', '.nvue', '.uvue', '.jsx', '.tsx']Usage Example:
import {
EXTNAME_JS,
EXTNAME_TS,
EXTNAME_VUE,
EXTNAME_VUE_TEMPLATE
} from "@dcloudio/uni-cli-shared";
// Check file types in build process
function isJavaScriptFile(filename: string): boolean {
return EXTNAME_JS.some(ext => filename.endsWith(ext));
}
function isTypeScriptFile(filename: string): boolean {
return EXTNAME_TS.some(ext => filename.endsWith(ext));
}
function isVueFile(filename: string): boolean {
return EXTNAME_VUE.some(ext => filename.endsWith(ext));
}
function isTemplateFile(filename: string): boolean {
return EXTNAME_VUE_TEMPLATE.some(ext => filename.endsWith(ext));
}
// Build system file filtering
const jsFiles = files.filter(isJavaScriptFile);
const vueFiles = files.filter(isVueFile);
const templateFiles = files.filter(isTemplateFile);
// Vite plugin configuration
export default {
plugins: [
{
name: 'uni-file-filter',
buildStart() {
// Process different file types
this.addWatchFile(files.filter(file =>
EXTNAME_VUE.some(ext => file.endsWith(ext))
));
}
}
]
};Pre-compiled regular expressions for efficient file type matching.
/**
* Regular expression for Vue component files (vue, nvue, uvue)
*/
const EXTNAME_VUE_RE: RegExp;
/**
* Regular expression for JavaScript and TypeScript files
*/
const EXTNAME_JS_RE: RegExp;
/**
* Regular expression for TypeScript files only
*/
const EXTNAME_TS_RE: RegExp;Values:
EXTNAME_VUE_RE: /\.(vue|nvue|uvue)$/EXTNAME_JS_RE: /\.(js|jsx|ts|uts|tsx|mjs)$/EXTNAME_TS_RE: /\.tsx?$/Usage Example:
import {
EXTNAME_VUE_RE,
EXTNAME_JS_RE,
EXTNAME_TS_RE
} from "@dcloudio/uni-cli-shared";
// Efficient file type checking with regex
function categorizeFiles(filePaths: string[]) {
const categories = {
vue: [] as string[],
javascript: [] as string[],
typescript: [] as string[]
};
filePaths.forEach(file => {
if (EXTNAME_VUE_RE.test(file)) {
categories.vue.push(file);
} else if (EXTNAME_TS_RE.test(file)) {
categories.typescript.push(file);
} else if (EXTNAME_JS_RE.test(file)) {
categories.javascript.push(file);
}
});
return categories;
}
// Rollup plugin file filtering
export default {
plugins: [
{
name: 'file-processor',
transform(code: string, id: string) {
if (EXTNAME_VUE_RE.test(id)) {
return processVueFile(code, id);
} else if (EXTNAME_TS_RE.test(id)) {
return processTypeScript(code, id);
} else if (EXTNAME_JS_RE.test(id)) {
return processJavaScript(code, id);
}
}
}
]
};
// Watch patterns for development
const watchPatterns = [
'src/**/*.{' + EXTNAME_JS.join(',').replace(/\./g, '') + '}',
'src/**/*.{' + EXTNAME_VUE.join(',').replace(/\./g, '') + '}'
];Unicode characters for special processing and error handling.
/**
* Special character constants for internal processing
*/
const SPECIAL_CHARS: {
/** Warning block identifier using Zero Width No-Break Space */
WARN_BLOCK: '\uFEFF';
/** Error block identifier using Word Joiner */
ERROR_BLOCK: '\u2060';
};Usage Example:
import { SPECIAL_CHARS } from "@dcloudio/uni-cli-shared";
// Mark code sections with special processing needs
function wrapWithWarningBlock(code: string): string {
return `${SPECIAL_CHARS.WARN_BLOCK}${code}${SPECIAL_CHARS.WARN_BLOCK}`;
}
function wrapWithErrorBlock(code: string): string {
return `${SPECIAL_CHARS.ERROR_BLOCK}${code}${SPECIAL_CHARS.ERROR_BLOCK}`;
}
// Process marked code sections
function processSpecialBlocks(source: string): string {
// Handle warning blocks
source = source.replace(
new RegExp(`${SPECIAL_CHARS.WARN_BLOCK}([^${SPECIAL_CHARS.WARN_BLOCK}]+)${SPECIAL_CHARS.WARN_BLOCK}`, 'g'),
(match, content) => processWarningContent(content)
);
// Handle error blocks
source = source.replace(
new RegExp(`${SPECIAL_CHARS.ERROR_BLOCK}([^${SPECIAL_CHARS.ERROR_BLOCK}]+)${SPECIAL_CHARS.ERROR_BLOCK}`, 'g'),
(match, content) => processErrorContent(content)
);
return source;
}
function processWarningContent(content: string): string {
console.warn('Processing warning block:', content);
return `/* WARNING: ${content} */`;
}
function processErrorContent(content: string): string {
console.error('Processing error block:', content);
return `/* ERROR: ${content} */`;
}Essential constants for uni-app build process and asset management.
/**
* Default public directory name for static assets
*/
const PUBLIC_DIR: 'static';
/**
* Build-related JSON processing identifiers
*/
const PAGES_JSON_JS: 'pages-json-js';
const PAGES_JSON_UTS: 'pages-json-uts';
const MANIFEST_JSON_JS: 'manifest-json-js';
const MANIFEST_JSON_UTS: 'manifest-json-uts';
/**
* Application service and configuration file names
*/
const APP_SERVICE_FILENAME: 'app-service.js';
const APP_CONFIG: 'app-config.js';
const APP_CONFIG_SERVICE: 'app-config-service.js';
/**
* Component binding identifier for runtime
*/
const BINDING_COMPONENTS: '__BINDING_COMPONENTS__';
/**
* Asset inlining threshold in bytes (40KB)
*/
const ASSETS_INLINE_LIMIT: 40960;Usage Example:
import {
PUBLIC_DIR,
ASSETS_INLINE_LIMIT,
APP_SERVICE_FILENAME,
APP_CONFIG,
BINDING_COMPONENTS
} from "@dcloudio/uni-cli-shared";
// Vite configuration using constants
export default defineConfig({
publicDir: PUBLIC_DIR,
build: {
assetsInlineLimit: ASSETS_INLINE_LIMIT,
rollupOptions: {
input: {
'app-service': `./${APP_SERVICE_FILENAME}`,
'app-config': `./${APP_CONFIG}`
},
output: {
entryFileNames: '[name].js',
chunkFileNames: 'chunks/[name].[hash].js'
}
}
}
});
// Asset processing
function shouldInlineAsset(assetSize: number): boolean {
return assetSize <= ASSETS_INLINE_LIMIT;
}
// Component binding in runtime
function bindComponents(components: any[]): string {
return `
const ${BINDING_COMPONENTS} = ${JSON.stringify(components)};
// Component registration logic
`;
}
// Directory structure setup
function setupProjectDirectories(projectRoot: string): void {
const publicPath = path.join(projectRoot, PUBLIC_DIR);
if (!fs.existsSync(publicPath)) {
fs.mkdirSync(publicPath, { recursive: true });
console.log(`Created public directory: ${publicPath}`);
}
}File extension resolution priority for different uni-app platforms.
/**
* Page file extension priority for App platforms (NVUE first)
*/
const PAGE_EXTNAME_APP: string[];
/**
* Page file extension priority for other platforms (Vue first)
*/
const PAGE_EXTNAME: string[];
/**
* Page file extension priority for UniApp X platforms (UVUE first)
*/
const X_PAGE_EXTNAME: string[];Values:
PAGE_EXTNAME_APP: ['.nvue', '.vue', '.tsx', '.jsx', '.js']PAGE_EXTNAME: ['.vue', '.nvue', '.tsx', '.jsx', '.js']X_PAGE_EXTNAME: ['.uvue', '.vue', '.tsx', '.jsx', '.js']Usage Example:
import {
PAGE_EXTNAME_APP,
PAGE_EXTNAME,
X_PAGE_EXTNAME
} from "@dcloudio/uni-cli-shared";
// Platform-specific page resolution
function resolvePageFile(
basePath: string,
platform: 'app' | 'web' | 'mp' | 'x'
): string | null {
let extensions: string[];
switch (platform) {
case 'app':
extensions = PAGE_EXTNAME_APP;
break;
case 'x':
extensions = X_PAGE_EXTNAME;
break;
default:
extensions = PAGE_EXTNAME;
}
// Try extensions in priority order
for (const ext of extensions) {
const fullPath = basePath + ext;
if (fs.existsSync(fullPath)) {
return fullPath;
}
}
return null;
}
// Build system page discovery
function discoverPages(pagesDir: string, platform: string) {
const pageConfigs = [];
const extensions = getPlatformExtensions(platform);
// Scan for page files with platform-specific priority
const pageFiles = glob.sync(`${pagesDir}/**/index.{${extensions.join(',')}}`);
return pageFiles.map(file => ({
path: path.dirname(file),
component: file,
platform
}));
}
function getPlatformExtensions(platform: string): string[] {
switch (platform) {
case 'app-plus':
case 'app-android':
case 'app-ios':
return PAGE_EXTNAME_APP;
case 'app-x':
case 'app-harmony':
return X_PAGE_EXTNAME;
default:
return PAGE_EXTNAME;
}
}
// Router configuration generation
function generateRouterConfig(pages: any[], platform: string) {
const routes = pages.map(page => {
const pagePath = resolvePageFile(page.basePath, platform);
return {
path: page.path,
component: pagePath,
meta: {
platform,
type: getPageType(pagePath)
}
};
});
return { routes };
}
function getPageType(filePath: string): 'vue' | 'nvue' | 'uvue' | 'jsx' {
if (filePath.endsWith('.nvue')) return 'nvue';
if (filePath.endsWith('.uvue')) return 'uvue';
if (filePath.endsWith('.jsx') || filePath.endsWith('.tsx')) return 'jsx';
return 'vue';
}Using constants for consistent build and runtime configuration.
import {
PUBLIC_DIR,
ASSETS_INLINE_LIMIT,
EXTNAME_VUE_RE,
EXTNAME_JS_RE,
PAGE_EXTNAME,
BINDING_COMPONENTS
} from "@dcloudio/uni-cli-shared";
// Comprehensive build configuration
export default defineConfig({
// Public assets directory
publicDir: PUBLIC_DIR,
// Asset handling
build: {
assetsInlineLimit: ASSETS_INLINE_LIMIT,
rollupOptions: {
plugins: [
{
name: 'uni-constants',
generateBundle(options, bundle) {
// Inject constants into bundle
const constantsCode = `
export const UNI_CONSTANTS = {
PUBLIC_DIR: "${PUBLIC_DIR}",
ASSETS_INLINE_LIMIT: ${ASSETS_INLINE_LIMIT},
BINDING_COMPONENTS: "${BINDING_COMPONENTS}"
};
`;
this.emitFile({
type: 'asset',
fileName: 'constants.js',
source: constantsCode
});
}
}
]
}
},
// Plugin configuration using regex constants
plugins: [
{
name: 'file-processor',
transform(code: string, id: string) {
if (EXTNAME_VUE_RE.test(id)) {
return transformVueComponent(code);
} else if (EXTNAME_JS_RE.test(id)) {
return transformJavaScript(code);
}
}
}
]
});The constants and types system provides a comprehensive foundation for consistent development across the uni-app ecosystem, ensuring standardized file handling, build processes, and platform compatibility.