File tree Expand file tree Collapse file tree 3 files changed +24
-16
lines changed
code/core/src/core-server/presets Expand file tree Collapse file tree 3 files changed +24
-16
lines changed Original file line number Diff line number Diff line change @@ -101,17 +101,27 @@ export function viteMockPlugin(options: MockPluginOptions): Plugin[] {
101101 server . watcher . on ( 'unlink' , invalidateAffectedFiles ) ;
102102 } ,
103103
104- async load ( id ) {
105- for ( const call of mockCalls ) {
106- if ( call . absolutePath !== id ) {
107- continue ;
108- }
104+ load : {
105+ order : 'pre' ,
106+ handler ( id ) {
107+ const preserveSymlinks = viteConfig . resolve . preserveSymlinks ;
108+
109+ const idNorm = normalizePathForComparison ( id , preserveSymlinks ) ;
110+ const cleanId = getCleanId ( idNorm ) ;
111+
112+ for ( const call of mockCalls ) {
113+ const callNorm = normalizePathForComparison ( call . absolutePath , preserveSymlinks ) ;
114+
115+ if ( callNorm !== idNorm && call . path !== cleanId ) {
116+ continue ;
117+ }
109118
110- if ( call . redirectPath ) {
111- return readFileSync ( call . redirectPath , 'utf-8' ) ;
119+ if ( call . redirectPath ) {
120+ return readFileSync ( call . redirectPath , 'utf-8' ) ;
121+ }
112122 }
113- }
114- return null ;
123+ return null ;
124+ } ,
115125 } ,
116126 transform : {
117127 order : 'pre' ,
@@ -140,8 +150,6 @@ export function viteMockPlugin(options: MockPluginOptions): Plugin[] {
140150 code : automockedCode . toString ( ) ,
141151 map : automockedCode . generateMap ( ) ,
142152 } ;
143- } else {
144- return readFileSync ( call . redirectPath , 'utf-8' ) ;
145153 }
146154 } catch ( e ) {
147155 logger . error ( `Error automocking ${ id } : ${ e } ` ) ;
Original file line number Diff line number Diff line change @@ -10,5 +10,6 @@ import { rewriteSbMockImportCalls } from '../../../mocking-utils/extract';
1010 */
1111export default function storybookMockTransformLoader ( this : LoaderContext < { } > , source : string ) {
1212 const result = rewriteSbMockImportCalls ( source ) ;
13- return result . code ;
13+ const callback = this . async ( ) ;
14+ callback ( null , result . code , result . map || undefined ) ;
1415}
Original file line number Diff line number Diff line change 1- import { parse } from '@babel/parser' ;
2- import type { ParserOptions } from '@babel/parser' ;
31import type { LoaderContext } from 'webpack' ;
42
53import { getAutomockCode } from '../../../mocking-utils/automock' ;
@@ -28,14 +26,15 @@ interface AutomockLoaderOptions {
2826export default function webpackAutomockLoader (
2927 this : LoaderContext < AutomockLoaderOptions > ,
3028 source : string
31- ) : string {
29+ ) {
3230 // Retrieve the options passed in the resource query string (e.g., `?spy=true`).
3331 const options = this . getOptions ( ) ;
32+ const callback = this . async ( ) ;
3433 const isSpy = options . spy === 'true' ;
3534
3635 // Generate the mocked source code using the utility from @vitest /mocker.
3736 const mocked = getAutomockCode ( source , isSpy , babelParser as any ) ;
3837
3938 // Return the transformed code to Webpack for further processing.
40- return mocked . toString ( ) ;
39+ callback ( null , mocked . toString ( ) , mocked . generateMap ( ) ) ;
4140}
You can’t perform that action at this time.
0 commit comments