Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: Farm resolve moduleType error and return sourcemap value error #448

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

Merged
merged 10 commits into from
Feb 14, 2025
Merged
3 changes: 2 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 8 additions & 23 deletions src/farm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -28,8 +27,8 @@ import {
customParseQueryString,
decodeStr,
encodeStr,
formatTransformModuleType,
getContentValue,
guessIdLoader,
isObject,
isStartsWithSlash,
isString,
Expand Down Expand Up @@ -88,13 +87,11 @@ export function toFarmPlugin(plugin: UnpluginOptions, options?: Record<string, a
filters = options?.filters ?? []

farmPlugin.resolve = {
filters: { sources: ['.*', ...filters], importers: ['.*'] },
filters: { sources: filters.length ? filters : ['.*'], importers: ['.*'] },
async executor(params: PluginResolveHookParam, context: CompilationContext) {
const resolvedIdPath = path.resolve(
process.cwd(),
params.importer ?? '',
)

let isEntry = false
if (isObject(params.kind) && 'entry' in params.kind) {
const kindWithEntry = params.kind as { entry: string }
Expand Down Expand Up @@ -126,23 +123,9 @@ export function toFarmPlugin(plugin: UnpluginOptions, options?: Record<string, a
meta: {},
}
}

if (!isStartsWithSlash(params.source))
return null

const rootAbsolutePath = path.resolve(
params.source,
)
if (
existsSync(rootAbsolutePath)
) {
return {
resolvedPath: removeQuery(encodeStr(rootAbsolutePath)),
query: customParseQueryString(rootAbsolutePath),
sideEffects: false,
external: false,
meta: {},
}
}
},
} as unknown as JsPlugin['resolve']
}
Expand All @@ -161,7 +144,7 @@ export function toFarmPlugin(plugin: UnpluginOptions, options?: Record<string, a

const id = appendQuery(resolvedPath, params.query)

const loader = guessIdLoader(resolvedPath)
const loader = formatTransformModuleType(id)

const shouldLoadInclude
= plugin.loadInclude?.(id)
Expand Down Expand Up @@ -198,7 +181,7 @@ export function toFarmPlugin(plugin: UnpluginOptions, options?: Record<string, a

const id = appendQuery(resolvedPath, params.query)

const loader = params.moduleType ?? guessIdLoader(params.resolvedPath)
const loader = formatTransformModuleType(id)

const shouldTransformInclude
= plugin.transformInclude?.(id)
Expand All @@ -216,7 +199,9 @@ export function toFarmPlugin(plugin: UnpluginOptions, options?: Record<string, a
const transformFarmResult: PluginTransformHookResult = {
content: getContentValue(resource),
moduleType: loader,
sourceMap: JSON.stringify(resource.map),
sourceMap: typeof resource.map === 'object' && resource.map !== null
? JSON.stringify(resource.map)
: undefined,
}

return transformFarmResult
Expand Down
63 changes: 61 additions & 2 deletions src/farm/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ const ExtToLoader: Record<string, string> = {
'.node': 'napi',
}

export const DEFAULT_PATTERN = '.*'

export function guessIdLoader(id: string): string {
return ExtToLoader[path.extname(id).toLowerCase()] || 'js'
}
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Loading