File tree Expand file tree Collapse file tree 5 files changed +37
-10
lines changed
packages/vite/src/node/server
playground/transform-plugin Expand file tree Collapse file tree 5 files changed +37
-10
lines changed Original file line number Diff line number Diff line change @@ -303,11 +303,6 @@ async function loadAndTransform(
303303 timestamp : true ,
304304 } )
305305 }
306-
307- const guessedModuleType = getModuleTypeFromId ( id )
308- if ( guessedModuleType && guessedModuleType !== 'js' ) {
309- moduleType = guessedModuleType
310- }
311306 }
312307 } else {
313308 debugLoad ?.( `${ timeFrom ( loadStart ) } [plugin] ${ prettyUrl } ` )
@@ -340,6 +335,12 @@ async function loadAndTransform(
340335 err . code = isPublicFile ? ERR_LOAD_PUBLIC_URL : ERR_LOAD_URL
341336 throw err
342337 }
338+ if ( moduleType === undefined ) {
339+ const guessedModuleType = getModuleTypeFromId ( id )
340+ if ( guessedModuleType && guessedModuleType !== 'js' ) {
341+ moduleType = guessedModuleType
342+ }
343+ }
343344
344345 if ( environment . _closing && environment . config . dev . recoverable )
345346 throwClosedServerError ( )
Original file line number Diff line number Diff line change @@ -5,6 +5,8 @@ import { page } from '~utils'
55test ( 'module type should be supported' , async ( ) => {
66 expect ( await page . textContent ( '#module-type-json-pre' ) ) . toBe ( 'json' )
77 expect ( await page . textContent ( '#module-type-json-post' ) ) . toBe ( 'js' )
8+ expect ( await page . textContent ( '#module-type-json-virtual-pre' ) ) . toBe ( 'json' )
9+ expect ( await page . textContent ( '#module-type-json-virtual-post' ) ) . toBe ( 'js' )
810} )
911
1012tests ( )
Original file line number Diff line number Diff line change @@ -6,5 +6,7 @@ <h2>addWatchFile</h2>
66< h2 > Module Type</ h2 >
77< div id ="module-type-json-pre "> </ div >
88< div id ="module-type-json-post "> </ div >
9+ < div id ="module-type-json-virtual-pre "> </ div >
10+ < div id ="module-type-json-virtual-post "> </ div >
911
1012< script type ="module " src ="./index.js "> </ script >
Original file line number Diff line number Diff line change 1+ import barJson from 'virtual:/bar.json'
12import fooJson from './foo.json'
23
34// 'TRANSFORM_COUNT' is injected by the transform plugin
@@ -7,3 +8,8 @@ document.getElementById('module-type-json-pre').innerHTML =
78 fooJson . moduleTypePre
89document . getElementById ( 'module-type-json-post' ) . innerHTML =
910 fooJson . moduleTypePost
11+
12+ document . getElementById ( 'module-type-json-virtual-pre' ) . innerHTML =
13+ barJson . moduleTypePre
14+ document . getElementById ( 'module-type-json-virtual-post' ) . innerHTML =
15+ barJson . moduleTypePost
Original file line number Diff line number Diff line change @@ -27,21 +27,37 @@ const transformPlugin = {
2727 } ,
2828}
2929
30- const moduleTypePlugins = /** @type { const } */ ( [ 'pre' , 'post' ] ) . map (
31- ( enforce ) => ( {
30+ const moduleTypePlugins = [
31+ /** @type { const } */ ... [ 'pre' , 'post' ] . map ( ( enforce ) => ( {
3232 name : `module-type-${ enforce } ` ,
3333 enforce,
3434 transform ( code , id , opts ) {
35- if ( id . endsWith ( '/foo.json' ) ) {
35+ if ( id . endsWith ( '/foo.json' ) || id . endsWith ( '\0/bar.json' ) ) {
3636 code = code . replace (
3737 `MODULE_TYPE_${ enforce . toUpperCase ( ) } ` ,
3838 opts . moduleType ,
3939 )
4040 return code
4141 }
4242 } ,
43- } ) ,
44- )
43+ } ) ) ,
44+ {
45+ name : `module-type-load` ,
46+ resolveId ( id ) {
47+ if ( id === 'virtual:/bar.json' ) {
48+ return '\0/bar.json'
49+ }
50+ } ,
51+ load ( id ) {
52+ if ( id . endsWith ( '\0/bar.json' ) ) {
53+ return JSON . stringify ( {
54+ moduleTypePre : 'MODULE_TYPE_PRE' ,
55+ moduleTypePost : 'MODULE_TYPE_POST' ,
56+ } )
57+ }
58+ } ,
59+ } ,
60+ ]
4561
4662export default defineConfig ( {
4763 plugins : [ transformPlugin , moduleTypePlugins ] ,
You can’t perform that action at this time.
0 commit comments