Skip to content

Commit 3dc78ec

Browse files
ErKeLostantfu
andauthored
fix(farm): resolve moduleType and sourcemap (#448)
* fix: resolve moduleType error and return sourcemap value * Update src/farm/utils.ts Co-authored-by: Anthony Fu <github@antfu.me> * chore: optimize code * chore: update code * Merge branch 'main' of github.com:ErKeLost/unplugin into fix/source_map_error --------- Co-authored-by: Anthony Fu <github@antfu.me>
1 parent 3b3acfa commit 3dc78ec

File tree

3 files changed

+71
-26
lines changed

3 files changed

+71
-26
lines changed

pnpm-lock.yaml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/farm/index.ts

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import type {
1616
} from '../types'
1717
import type { JsPluginExtended, WatchChangeEvents } from './utils'
1818

19-
import { existsSync } from 'node:fs'
2019
import path from 'node:path'
2120

2221
import { toArray } from '../utils/general'
@@ -28,8 +27,8 @@ import {
2827
customParseQueryString,
2928
decodeStr,
3029
encodeStr,
30+
formatTransformModuleType,
3131
getContentValue,
32-
guessIdLoader,
3332
isObject,
3433
isStartsWithSlash,
3534
isString,
@@ -88,13 +87,11 @@ export function toFarmPlugin(plugin: UnpluginOptions, options?: Record<string, a
8887
filters = options?.filters ?? []
8988

9089
farmPlugin.resolve = {
91-
filters: { sources: ['.*', ...filters], importers: ['.*'] },
90+
filters: { sources: filters.length ? filters : ['.*'], importers: ['.*'] },
9291
async executor(params: PluginResolveHookParam, context: CompilationContext) {
9392
const resolvedIdPath = path.resolve(
94-
process.cwd(),
9593
params.importer ?? '',
9694
)
97-
9895
let isEntry = false
9996
if (isObject(params.kind) && 'entry' in params.kind) {
10097
const kindWithEntry = params.kind as { entry: string }
@@ -126,23 +123,9 @@ export function toFarmPlugin(plugin: UnpluginOptions, options?: Record<string, a
126123
meta: {},
127124
}
128125
}
126+
129127
if (!isStartsWithSlash(params.source))
130128
return null
131-
132-
const rootAbsolutePath = path.resolve(
133-
params.source,
134-
)
135-
if (
136-
existsSync(rootAbsolutePath)
137-
) {
138-
return {
139-
resolvedPath: removeQuery(encodeStr(rootAbsolutePath)),
140-
query: customParseQueryString(rootAbsolutePath),
141-
sideEffects: false,
142-
external: false,
143-
meta: {},
144-
}
145-
}
146129
},
147130
} as unknown as JsPlugin['resolve']
148131
}
@@ -161,7 +144,7 @@ export function toFarmPlugin(plugin: UnpluginOptions, options?: Record<string, a
161144

162145
const id = appendQuery(resolvedPath, params.query)
163146

164-
const loader = guessIdLoader(resolvedPath)
147+
const loader = formatTransformModuleType(id)
165148

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

199182
const id = appendQuery(resolvedPath, params.query)
200183

201-
const loader = params.moduleType ?? guessIdLoader(params.resolvedPath)
184+
const loader = formatTransformModuleType(id)
202185

203186
const shouldTransformInclude
204187
= plugin.transformInclude?.(id)
@@ -216,7 +199,9 @@ export function toFarmPlugin(plugin: UnpluginOptions, options?: Record<string, a
216199
const transformFarmResult: PluginTransformHookResult = {
217200
content: getContentValue(resource),
218201
moduleType: loader,
219-
sourceMap: JSON.stringify(resource.map),
202+
sourceMap: typeof resource.map === 'object' && resource.map !== null
203+
? JSON.stringify(resource.map)
204+
: undefined,
220205
}
221206

222207
return transformFarmResult

src/farm/utils.ts

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const ExtToLoader: Record<string, string> = {
2222
'.node': 'napi',
2323
}
2424

25+
export const DEFAULT_PATTERN = '.*'
26+
2527
export function guessIdLoader(id: string): string {
2628
return ExtToLoader[path.extname(id).toLowerCase()] || 'js'
2729
}
@@ -38,8 +40,8 @@ export function transformQuery(context: any): void {
3840
export function convertEnforceToPriority(value: 'pre' | 'post' | undefined): number {
3941
const defaultPriority = 100
4042
const enforceToPriority = {
41-
pre: 101,
42-
post: 99,
43+
pre: 102,
44+
post: 98,
4345
}
4446

4547
return enforceToPriority[value!] !== undefined
@@ -213,3 +215,60 @@ export function stringifyQuery(query: [string, string][]): string {
213215
export interface JsPluginExtended extends JsPlugin {
214216
[key: string]: any
215217
}
218+
219+
export const CSS_LANGS_RES: [RegExp, string][] = [
220+
[/\.(less)(?:$|\?)/, 'less'],
221+
[/\.(scss|sass)(?:$|\?)/, 'sass'],
222+
[/\.(styl|stylus)(?:$|\?)/, 'stylus'],
223+
[/\.(css)(?:$|\?)/, 'css'],
224+
]
225+
226+
export const JS_LANGS_RES: [RegExp, string][] = [
227+
[/\.(js|mjs|cjs)(?:$|\?)/, 'js'],
228+
// jsx
229+
[/\.(jsx)(?:$|\?)/, 'jsx'],
230+
// ts
231+
[/\.(ts|cts|mts)(?:$|\?)/, 'ts'],
232+
// tsx
233+
[/\.(tsx)(?:$|\?)/, 'tsx'],
234+
]
235+
236+
export function getCssModuleType(id: string): string | null {
237+
for (const [reg, lang] of CSS_LANGS_RES) {
238+
if (reg.test(id)) {
239+
return lang
240+
}
241+
}
242+
243+
return null
244+
}
245+
246+
export function getJsModuleType(id: string): string | null {
247+
for (const [reg, lang] of JS_LANGS_RES) {
248+
if (reg.test(id)) {
249+
return lang
250+
}
251+
}
252+
253+
return null
254+
}
255+
256+
export function formatLoadModuleType(id: string): string {
257+
const cssModuleType = getCssModuleType(id)
258+
259+
if (cssModuleType) {
260+
return cssModuleType
261+
}
262+
263+
const jsModuleType = getJsModuleType(id)
264+
265+
if (jsModuleType) {
266+
return jsModuleType
267+
}
268+
269+
return 'js'
270+
}
271+
272+
export function formatTransformModuleType(id: string): string {
273+
return formatLoadModuleType(id)
274+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy