Skip to content
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

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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