11import fs from 'node:fs'
22import fsp from 'node:fs/promises'
33import path from 'node:path'
4- import { createRequire } from 'node:module'
54import { fileURLToPath , pathToFileURL } from 'node:url'
65import postcssrc from 'postcss-load-config'
76import type {
@@ -57,6 +56,7 @@ import type { ResolvedConfig } from '../config'
5756import type { Plugin } from '../plugin'
5857import { checkPublicFile } from '../publicDir'
5958import {
59+ _dirname ,
6060 arraify ,
6161 asyncReplace ,
6262 combineSourcemaps ,
@@ -79,7 +79,6 @@ import {
7979 processSrcSet ,
8080 removeDirectQuery ,
8181 removeUrlQuery ,
82- requireResolveFromRootWithFallback ,
8382 stripBomTag ,
8483 urlRE ,
8584} from '../utils'
@@ -94,6 +93,7 @@ import { searchForWorkspaceRoot } from '../server/searchRoot'
9493import { type DevEnvironment } from '..'
9594import type { PackageCache } from '../packages'
9695import { findNearestMainPackageData } from '../packages'
96+ import { nodeResolveWithVite } from '../nodeResolve'
9797import { addToHTMLProxyTransformResult } from './html'
9898import {
9999 assetUrlRE ,
@@ -1564,7 +1564,7 @@ async function compilePostCSS(
15641564
15651565 const postcssOptions = postcssConfig ?. options ?? { }
15661566 const postcssParser =
1567- lang === 'sss' ? loadSss ( config . root ) : postcssOptions . parser
1567+ lang === 'sss' ? await loadSss ( config . root ) : postcssOptions . parser
15681568
15691569 if ( ! postcssPlugins . length && ! postcssParser ) {
15701570 return
@@ -1590,11 +1590,12 @@ async function transformSugarSS(
15901590 const { config } = environment
15911591 const { devSourcemap } = config . css
15921592
1593+ const sssParser = await loadSss ( config . root )
15931594 const result = await runPostCSS (
15941595 id ,
15951596 code ,
15961597 [ ] ,
1597- { parser : loadSss ( config . root ) } ,
1598+ { parser : sssParser } ,
15981599 undefined ,
15991600 environment . logger ,
16001601 devSourcemap ,
@@ -2325,23 +2326,18 @@ function loadPreprocessorPath(
23252326 if ( cached ) {
23262327 return cached
23272328 }
2328- try {
2329- const resolved = requireResolveFromRootWithFallback ( root , lang )
2330- return ( loadedPreprocessorPath [ lang ] = resolved )
2331- } catch ( e ) {
2332- if ( e . code === 'MODULE_NOT_FOUND' ) {
2333- const installCommand = getPackageManagerCommand ( 'install' )
2334- throw new Error (
2335- `Preprocessor dependency "${ lang } " not found. Did you install it? Try \`${ installCommand } -D ${ lang } \`.` ,
2336- )
2337- } else {
2338- const message = new Error (
2339- `Preprocessor dependency "${ lang } " failed to load:\n${ e . message } ` ,
2340- )
2341- message . stack = e . stack + '\n' + message . stack
2342- throw message
2343- }
2344- }
2329+
2330+ // Try resolve from project root first, then the current vite installation path
2331+ const resolved =
2332+ nodeResolveWithVite ( lang , undefined , { root } ) ??
2333+ nodeResolveWithVite ( lang , _dirname , { root } )
2334+ if ( resolved ) return ( loadedPreprocessorPath [ lang ] = resolved )
2335+
2336+ // Error if we can't find the preprocessor
2337+ const installCommand = getPackageManagerCommand ( 'install' )
2338+ throw new Error (
2339+ `Preprocessor dependency "${ lang } " not found. Did you install it? Try \`${ installCommand } -D ${ lang } \`.` ,
2340+ )
23452341}
23462342
23472343function loadSassPackage ( root : string ) : {
@@ -2362,12 +2358,15 @@ function loadSassPackage(root: string): {
23622358 }
23632359}
23642360
2365- let cachedSss : PostCSS . Syntax
2366- function loadSss ( root : string ) : PostCSS . Syntax {
2367- if ( cachedSss ) return cachedSss
2368-
2369- const sssPath = loadPreprocessorPath ( PostCssDialectLang . sss , root )
2370- cachedSss = createRequire ( /** #__KEEP__ */ import . meta. url ) ( sssPath )
2361+ let cachedSss : PostCSS . Syntax | Promise < PostCSS . Syntax >
2362+ async function loadSss ( root : string ) : Promise < PostCSS . Syntax > {
2363+ if ( ! cachedSss ) {
2364+ cachedSss = ( async ( ) => {
2365+ const sssPath = loadPreprocessorPath ( PostCssDialectLang . sss , root )
2366+ const resolved = ( await import ( pathToFileURL ( sssPath ) . href ) ) . default
2367+ return ( cachedSss = resolved )
2368+ } ) ( )
2369+ }
23712370 return cachedSss
23722371}
23732372
@@ -2417,10 +2416,7 @@ const makeScssWorker = (
24172416
24182417 const worker : WorkerType = {
24192418 async run ( sassPath , data , options ) {
2420- // need pathToFileURL for windows since import("D:...") fails
2421- // https://github.com/nodejs/node/issues/31710
2422- const sass : typeof Sass = ( await import ( pathToFileURL ( sassPath ) . href ) )
2423- . default
2419+ const sass : typeof Sass = await import ( sassPath )
24242420 compilerPromise ??= sass . initAsyncCompiler ( )
24252421 const compiler = await compilerPromise
24262422
@@ -2537,7 +2533,7 @@ const scssProcessor = (
25372533 }
25382534 try {
25392535 const result = await worker . run (
2540- sassPackage . path ,
2536+ pathToFileURL ( sassPackage . path ) . href ,
25412537 data ,
25422538 optionsWithoutAdditionalData ,
25432539 )
@@ -2798,9 +2794,7 @@ const lessProcessor = (
27982794 worker ?. stop ( )
27992795 } ,
28002796 async process ( environment , source , root , options , resolvers ) {
2801- const lessPath = pathToFileURL (
2802- loadPreprocessorPath ( PreprocessLang . less , root ) ,
2803- ) . href
2797+ const lessPath = loadPreprocessorPath ( PreprocessLang . less , root )
28042798 worker ??= makeLessWorker ( environment , resolvers , maxWorkers )
28052799
28062800 const { content, map : additionalMap } = await getSource (
@@ -2817,7 +2811,7 @@ const lessProcessor = (
28172811 }
28182812 try {
28192813 result = await worker . run (
2820- lessPath ,
2814+ pathToFileURL ( lessPath ) . href ,
28212815 content ,
28222816 optionsWithoutAdditionalData ,
28232817 )
@@ -2866,9 +2860,9 @@ const makeStylWorker = (maxWorkers: number | undefined) => {
28662860 additionalData : undefined
28672861 } ,
28682862 ) => {
2869- const nodeStylus : typeof Stylus = ( await import ( stylusPath ) ) . default
2863+ const stylus : typeof Stylus = ( await import ( stylusPath ) ) . default
28702864
2871- const ref = nodeStylus ( content , {
2865+ const ref = stylus ( content , {
28722866 // support @import from node dependencies by default
28732867 paths : [ 'node_modules' ] ,
28742868 ...options ,
@@ -2919,9 +2913,7 @@ const stylProcessor = (
29192913 worker ?. stop ( )
29202914 } ,
29212915 async process ( _environment , source , root , options , _resolvers ) {
2922- const stylusPath = pathToFileURL (
2923- loadPreprocessorPath ( PreprocessLang . stylus , root ) ,
2924- ) . href
2916+ const stylusPath = loadPreprocessorPath ( PreprocessLang . stylus , root )
29252917 worker ??= makeStylWorker ( maxWorkers )
29262918
29272919 // Get source with preprocessor options.additionalData. Make sure a new line separator
@@ -2944,7 +2936,7 @@ const stylProcessor = (
29442936 }
29452937 try {
29462938 const { code, map, deps } = await worker . run (
2947- stylusPath ,
2939+ pathToFileURL ( stylusPath ) . href ,
29482940 content ,
29492941 root ,
29502942 optionsWithoutAdditionalData ,
0 commit comments