@@ -15,19 +15,25 @@ import type {
15
15
UnpluginOptions ,
16
16
} from '../types'
17
17
import type { JsPluginExtended , WatchChangeEvents } from './utils'
18
- import path from 'path'
18
+
19
+ import { existsSync } from 'node:fs'
20
+ import path from 'node:path'
21
+
19
22
import { toArray } from '../utils/general'
20
23
import { createFarmContext , unpluginContext } from './context'
21
-
22
24
import {
25
+ appendQuery ,
23
26
convertEnforceToPriority ,
24
27
convertWatchEventChange ,
25
28
customParseQueryString ,
29
+ decodeStr ,
30
+ encodeStr ,
26
31
getContentValue ,
27
32
guessIdLoader ,
28
33
isObject ,
34
+ isStartsWithSlash ,
29
35
isString ,
30
- transformQuery ,
36
+ removeQuery ,
31
37
} from './utils'
32
38
33
39
export function getFarmPlugin <
@@ -97,30 +103,46 @@ export function toFarmPlugin(plugin: UnpluginOptions, options?: Record<string, a
97
103
const farmContext = createFarmContext ( context ! , resolvedIdPath )
98
104
const resolveIdResult = await _resolveId . call (
99
105
Object . assign ( unpluginContext ( context ) , farmContext ) ,
100
- params . source ,
106
+ decodeStr ( params . source ) ,
101
107
resolvedIdPath ?? null ,
102
108
{ isEntry } ,
103
109
)
104
110
105
111
if ( isString ( resolveIdResult ) ) {
106
112
return {
107
- resolvedPath : resolveIdResult ,
113
+ resolvedPath : removeQuery ( encodeStr ( resolveIdResult ) ) ,
108
114
query : customParseQueryString ( resolveIdResult ) ,
109
- sideEffects : false ,
115
+ sideEffects : true ,
110
116
external : false ,
111
117
meta : { } ,
112
118
}
113
119
}
114
- else if ( isObject ( resolveIdResult ) ) {
120
+ if ( isObject ( resolveIdResult ) ) {
121
+ return {
122
+ resolvedPath : removeQuery ( encodeStr ( resolveIdResult ?. id ) ) ,
123
+ query : customParseQueryString ( resolveIdResult ?. id ) ,
124
+ sideEffects : false ,
125
+ external : Boolean ( resolveIdResult ?. external ) ,
126
+ meta : { } ,
127
+ }
128
+ }
129
+ if ( ! isStartsWithSlash ( params . source ) )
130
+ return null
131
+
132
+ const rootAbsolutePath = path . resolve (
133
+ params . source ,
134
+ )
135
+ if (
136
+ existsSync ( rootAbsolutePath )
137
+ ) {
115
138
return {
116
- resolvedPath : resolveIdResult ?. id ,
117
- query : customParseQueryString ( resolveIdResult ! . id ) ,
139
+ resolvedPath : removeQuery ( encodeStr ( rootAbsolutePath ) ) ,
140
+ query : customParseQueryString ( rootAbsolutePath ) ,
118
141
sideEffects : false ,
119
- external : resolveIdResult ?. external ,
142
+ external : false ,
120
143
meta : { } ,
121
144
}
122
145
}
123
- return null
124
146
} ,
125
147
} as unknown as JsPlugin [ 'resolve' ]
126
148
}
@@ -132,27 +154,34 @@ export function toFarmPlugin(plugin: UnpluginOptions, options?: Record<string, a
132
154
resolvedPaths : [ '.*' ] ,
133
155
} ,
134
156
async executor (
135
- id : PluginLoadHookParam ,
157
+ params : PluginLoadHookParam ,
136
158
context ,
137
159
) : Promise < PluginLoadHookResult | null > {
138
- if ( plugin . loadInclude && ! plugin . loadInclude ( id . resolvedPath ) )
139
- return null
140
- const loader = guessIdLoader ( id . resolvedPath )
160
+ const resolvedPath = decodeStr ( params . resolvedPath )
161
+
162
+ const id = appendQuery ( resolvedPath , params . query )
163
+
164
+ const loader = guessIdLoader ( resolvedPath )
165
+
141
166
const shouldLoadInclude
142
- = plugin . loadInclude && plugin . loadInclude ( id . resolvedPath )
143
- const farmContext = createFarmContext ( context ! , id . resolvedPath )
167
+ = plugin . loadInclude ?.( id )
168
+
169
+ if ( ! shouldLoadInclude )
170
+ return null
171
+
172
+ const farmContext = createFarmContext ( context ! , id )
173
+
144
174
const content : TransformResult = await _load . call (
145
175
Object . assign ( unpluginContext ( context ! ) , farmContext ) ,
146
- id . resolvedPath ,
176
+ id ,
147
177
)
178
+
148
179
const loadFarmResult : PluginLoadHookResult = {
149
180
content : getContentValue ( content ) ,
150
181
moduleType : loader ,
151
182
}
152
- if ( shouldLoadInclude )
153
- return loadFarmResult
154
183
155
- return null
184
+ return loadFarmResult
156
185
} ,
157
186
} as JsPlugin [ 'load' ]
158
187
}
@@ -165,35 +194,30 @@ export function toFarmPlugin(plugin: UnpluginOptions, options?: Record<string, a
165
194
params : PluginTransformHookParam ,
166
195
context : CompilationContext ,
167
196
) {
168
- if ( params . query . length )
169
- transformQuery ( params )
197
+ const resolvedPath = decodeStr ( params . resolvedPath )
170
198
171
- if (
172
- plugin . transformInclude
173
- && ! plugin . transformInclude ( params . resolvedPath )
174
- ) {
175
- return null
176
- }
199
+ const id = appendQuery ( resolvedPath , params . query )
177
200
178
201
const loader = params . moduleType ?? guessIdLoader ( params . resolvedPath )
202
+
179
203
const shouldTransformInclude
180
- = plugin . transformInclude
181
- && plugin . transformInclude ( params . resolvedPath )
182
- const farmContext = createFarmContext ( context , params . resolvedPath )
204
+ = plugin . transformInclude ?.( id )
205
+ const farmContext = createFarmContext ( context , id )
206
+
207
+ if ( ! shouldTransformInclude )
208
+ return null
209
+
183
210
const resource : TransformResult = await _transform . call (
184
211
Object . assign ( unpluginContext ( context ) , farmContext ) ,
185
212
params . content ,
186
- params . resolvedPath ,
213
+ id ,
187
214
)
188
-
189
215
if ( resource && typeof resource !== 'string' ) {
190
216
const transformFarmResult : PluginTransformHookResult = {
191
217
content : getContentValue ( resource ) ,
192
218
moduleType : loader ,
193
219
sourceMap : JSON . stringify ( resource . map ) ,
194
220
}
195
- if ( shouldTransformInclude )
196
- return transformFarmResult
197
221
198
222
return transformFarmResult
199
223
}
0 commit comments