| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 3b3acfa commit 3dc78ec
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,7 +16,6 @@ import type { | |||
| 16 | 16 | } from '../types' | |
| 17 | 17 | import type { JsPluginExtended, WatchChangeEvents } from './utils' | |
| 18 | 18 | ||
| 19 | - import { existsSync } from 'node:fs' | ||
| 20 | 19 | import path from 'node:path' | |
| 21 | 20 | ||
| 22 | 21 | import { toArray } from '../utils/general' | |
@@ -28,8 +27,8 @@ import { | |||
| 28 | 27 | customParseQueryString, | |
| 29 | 28 | decodeStr, | |
| 30 | 29 | encodeStr, | |
| 30 | + formatTransformModuleType, | ||
| 31 | 31 | getContentValue, | |
| 32 | - guessIdLoader, | ||
| 33 | 32 | isObject, | |
| 34 | 33 | isStartsWithSlash, | |
| 35 | 34 | isString, | |
@@ -88,13 +87,11 @@ export function toFarmPlugin(plugin: UnpluginOptions, options?: Record<string, a | |||
| 88 | 87 | filters = options?.filters ?? [] | |
| 89 | 88 | ||
| 90 | 89 | farmPlugin.resolve = { | |
| 91 | - filters: { sources: ['.*', ...filters], importers: ['.*'] }, | ||
| 90 | + filters: { sources: filters.length ? filters : ['.*'], importers: ['.*'] }, | ||
| 92 | 91 | async executor(params: PluginResolveHookParam, context: CompilationContext) { | |
| 93 | 92 | const resolvedIdPath = path.resolve( | |
| 94 | - process.cwd(), | ||
| 95 | 93 | params.importer ?? '', | |
| 96 | 94 | ) | |
| 97 | - | ||
| 98 | 95 | let isEntry = false | |
| 99 | 96 | if (isObject(params.kind) && 'entry' in params.kind) { | |
| 100 | 97 | const kindWithEntry = params.kind as { entry: string } | |
@@ -126,23 +123,9 @@ export function toFarmPlugin(plugin: UnpluginOptions, options?: Record<string, a | |||
| 126 | 123 | meta: {}, | |
| 127 | 124 | } | |
| 128 | 125 | } | |
| 126 | + | ||
| 129 | 127 | if (!isStartsWithSlash(params.source)) | |
| 130 | 128 | 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 | - } | ||
| 146 | 129 | }, | |
| 147 | 130 | } as unknown as JsPlugin['resolve'] | |
| 148 | 131 | } | |
@@ -161,7 +144,7 @@ export function toFarmPlugin(plugin: UnpluginOptions, options?: Record<string, a | |||
| 161 | 144 | ||
| 162 | 145 | const id = appendQuery(resolvedPath, params.query) | |
| 163 | 146 | ||
| 164 | - const loader = guessIdLoader(resolvedPath) | ||
| 147 | + const loader = formatTransformModuleType(id) | ||
| 165 | 148 | ||
| 166 | 149 | const shouldLoadInclude | |
| 167 | 150 | = plugin.loadInclude?.(id) | |
@@ -198,7 +181,7 @@ export function toFarmPlugin(plugin: UnpluginOptions, options?: Record<string, a | |||
| 198 | 181 | ||
| 199 | 182 | const id = appendQuery(resolvedPath, params.query) | |
| 200 | 183 | ||
| 201 | - const loader = params.moduleType ?? guessIdLoader(params.resolvedPath) | ||
| 184 | + const loader = formatTransformModuleType(id) | ||
| 202 | 185 | ||
| 203 | 186 | const shouldTransformInclude | |
| 204 | 187 | = plugin.transformInclude?.(id) | |
@@ -216,7 +199,9 @@ export function toFarmPlugin(plugin: UnpluginOptions, options?: Record<string, a | |||
| 216 | 199 | const transformFarmResult: PluginTransformHookResult = { | |
| 217 | 200 | content: getContentValue(resource), | |
| 218 | 201 | moduleType: loader, | |
| 219 | - sourceMap: JSON.stringify(resource.map), | ||
| 202 | + sourceMap: typeof resource.map === 'object' && resource.map !== null | ||
| 203 | + ? JSON.stringify(resource.map) | ||
| 204 | + : undefined, | ||
| 220 | 205 | } | |
| 221 | 206 | ||
| 222 | 207 | return transformFarmResult | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,6 +22,8 @@ const ExtToLoader: Record<string, string> = { | |||
| 22 | 22 | '.node': 'napi', | |
| 23 | 23 | } | |
| 24 | 24 | ||
| 25 | + export const DEFAULT_PATTERN = '.*' | ||
| 26 | + | ||
| 25 | 27 | export function guessIdLoader(id: string): string { | |
| 26 | 28 | return ExtToLoader[path.extname(id).toLowerCase()] || 'js' | |
| 27 | 29 | } | |
@@ -38,8 +40,8 @@ export function transformQuery(context: any): void { | |||
| 38 | 40 | export function convertEnforceToPriority(value: 'pre' | 'post' | undefined): number { | |
| 39 | 41 | const defaultPriority = 100 | |
| 40 | 42 | const enforceToPriority = { | |
| 41 | - pre: 101, | ||
| 42 | - post: 99, | ||
| 43 | + pre: 102, | ||
| 44 | + post: 98, | ||
| 43 | 45 | } | |
| 44 | 46 | ||
| 45 | 47 | return enforceToPriority[value!] !== undefined | |
@@ -213,3 +215,60 @@ export function stringifyQuery(query: [string, string][]): string { | |||
| 213 | 215 | export interface JsPluginExtended extends JsPlugin { | |
| 214 | 216 | [key: string]: any | |
| 215 | 217 | } | |
| 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 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments