Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Merged
merged 10 commits into from
Feb 14, 2025
10 changes: 6 additions & 4 deletions src/farm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import {
customParseQueryString,
decodeStr,
encodeStr,
formatTransformModuleType,
getContentValue,
guessIdLoader,
isObject,
isStartsWithSlash,
isString,
Expand Down Expand Up @@ -161,7 +161,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 +198,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 +216,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
57 changes: 57 additions & 0 deletions src/farm/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,60 @@
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'],

Check warning on line 225 in src/farm/utils.ts

View workflow job for this annotation

GitHub Actions / lint

This empty alternative might be a mistake. If not, use a quantifier instead
// 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