diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1012cd33..34d2686a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8334,6 +8334,7 @@ snapshots: http-proxy-middleware@2.0.7(@types/express@4.17.21): dependencies: + '@types/http-proxy': 1.17.16 http-proxy: 1.18.1(debug@4.4.0) is-glob: 4.0.3 @@ -8654,7 +8655,7 @@ snapshots: escape-html: 1.0.3 fresh: 0.5.2 http-assert: 1.5.0 - http-errors: 1.8.1 + http-errors: 1.6.3 is-generator-function: 1.1.0 koa-compose: 4.1.0 koa-convert: 2.0.0 diff --git a/src/farm/index.ts b/src/farm/index.ts index f7e7855f..9b887e63 100644 --- a/src/farm/index.ts +++ b/src/farm/index.ts @@ -16,7 +16,6 @@ import type { } from '../types' import type { JsPluginExtended, WatchChangeEvents } from './utils' -import { existsSync } from 'node:fs' import path from 'node:path' import { toArray } from '../utils/general' @@ -28,8 +27,8 @@ import { customParseQueryString, decodeStr, encodeStr, + formatTransformModuleType, getContentValue, - guessIdLoader, isObject, isStartsWithSlash, isString, @@ -88,13 +87,11 @@ export function toFarmPlugin(plugin: UnpluginOptions, options?: Record = { '.node': 'napi', } +export const DEFAULT_PATTERN = '.*' + export function guessIdLoader(id: string): string { return ExtToLoader[path.extname(id).toLowerCase()] || 'js' } @@ -38,8 +40,8 @@ export function transformQuery(context: any): void { export function convertEnforceToPriority(value: 'pre' | 'post' | undefined): number { const defaultPriority = 100 const enforceToPriority = { - pre: 101, - post: 99, + pre: 102, + post: 98, } return enforceToPriority[value!] !== undefined @@ -213,3 +215,60 @@ export function stringifyQuery(query: [string, string][]): string { export interface JsPluginExtended extends JsPlugin { [key: string]: any } + +export const CSS_LANGS_RES: [RegExp, string][] = [ + [/\.(less)(?:$|\?)/, 'less'], + [/\.(scss|sass)(?:$|\?)/, 'sass'], + [/\.(styl|stylus)(?:$|\?)/, 'stylus'], + [/\.(css)(?:$|\?)/, 'css'], +] + +export const JS_LANGS_RES: [RegExp, string][] = [ + [/\.(js|mjs|cjs)(?:$|\?)/, 'js'], + // jsx + [/\.(jsx)(?:$|\?)/, 'jsx'], + // ts + [/\.(ts|cts|mts)(?:$|\?)/, 'ts'], + // tsx + [/\.(tsx)(?:$|\?)/, 'tsx'], +] + +export function getCssModuleType(id: string): string | null { + for (const [reg, lang] of CSS_LANGS_RES) { + if (reg.test(id)) { + return lang + } + } + + return null +} + +export function getJsModuleType(id: string): string | null { + for (const [reg, lang] of JS_LANGS_RES) { + if (reg.test(id)) { + return lang + } + } + + return null +} + +export function formatLoadModuleType(id: string): string { + const cssModuleType = getCssModuleType(id) + + if (cssModuleType) { + return cssModuleType + } + + const jsModuleType = getJsModuleType(id) + + if (jsModuleType) { + return jsModuleType + } + + return 'js' +} + +export function formatTransformModuleType(id: string): string { + return formatLoadModuleType(id) +}