@@ -2,18 +2,38 @@ const { createFilter } = require('@rollup/pluginutils');
22
33const SCRIPT_LANG_RE = / \. ( c | m ) ? ( t | j ) s x ? $ / ;
44let babel ;
5+ let babelLoadAttempted = false ;
56let prefreshRolldownPromise ;
67let viteSupportsHookFilters ;
78let viteVersionParts ;
89
9- function loadBabel ( ) {
10- babel ||= {
10+ function tryLoadBabel ( ) {
11+ if ( babelLoadAttempted ) return babel ;
12+ babelLoadAttempted = true ;
13+ try {
14+ require . resolve ( '@babel/core' ) ;
15+ require . resolve ( '@prefresh/babel-plugin' ) ;
16+ } catch ( err ) {
17+ if ( err && err . code === 'MODULE_NOT_FOUND' ) return babel ;
18+ throw err ;
19+ }
20+ babel = {
1121 transformSync : require ( '@babel/core' ) . transformSync ,
1222 prefreshBabelPlugin : require ( '@prefresh/babel-plugin' ) ,
1323 } ;
1424 return babel ;
1525}
1626
27+ function loadBabel ( ) {
28+ const loaded = tryLoadBabel ( ) ;
29+ if ( loaded ) return loaded ;
30+ throw new Error (
31+ '@prefresh/vite needs @babel/core and @prefresh/babel-plugin to use the Babel transform path ' +
32+ '(triggered by `parserPlugins` or by Vite < 8 without rolldown support). ' +
33+ 'Install them as dev dependencies, or upgrade to a Vite version with rolldown so the Oxc path is used instead.'
34+ ) ;
35+ }
36+
1737function loadPrefreshRolldown ( ) {
1838 prefreshRolldownPromise ||= import ( '@prefresh/rolldown' ) . then (
1939 ( { default : prefreshRolldown } ) => prefreshRolldown
@@ -213,6 +233,10 @@ function prefreshBabelTransformPlugin(options = {}, forceBabel) {
213233 const hasSig = / \$ R e f r e s h S i g \$ \( / . test ( code ) ;
214234
215235 if ( hasReg || hasSig ) return ;
236+
237+ // Babel is an optional fallback on the Oxc path; if it's not
238+ // installed we silently skip rather than failing the request.
239+ if ( ! tryLoadBabel ( ) ) return ;
216240 }
217241
218242 const result = transform ( code , id , parserPlugins ) ;
0 commit comments