@@ -21,7 +21,7 @@ import {
2121 normalizePath ,
2222 parseAstAsync ,
2323} from 'vite'
24- import { crawlFrameworkPkgs , findClosestPkgJsonPath } from 'vitefu'
24+ import { crawlFrameworkPkgs } from 'vitefu'
2525import vitePluginRscCore from './core/plugin'
2626import {
2727 type TransformWrapExportFilter ,
@@ -33,6 +33,8 @@ import {
3333import { generateEncryptionKey , toBase64 } from './utils/encryption-utils'
3434import { createRpcServer } from './utils/rpc'
3535import { normalizeViteImportAnalysisUrl , prepareError } from './vite-utils'
36+ import { cjsModuleRunnerPlugin } from './plugins/cjs'
37+ import { evalValue , parseIdQuery } from './plugins/utils'
3638
3739// state for build orchestration
3840let serverReferences : Record < string , string > = { }
@@ -882,42 +884,10 @@ globalThis.AsyncLocalStorage = __viteRscAyncHooks.AsyncLocalStorage;
882884 ...( rscPluginOptions . validateImports !== false
883885 ? [ validateImportPlugin ( ) ]
884886 : [ ] ) ,
885- ...vendorUseSyncExternalStorePlugin ( ) ,
886887 scanBuildStripPlugin ( ) ,
887- detectNonOptimizedCjsPlugin ( ) ,
888+ ... cjsModuleRunnerPlugin ( ) ,
888889 ]
889890}
890- function detectNonOptimizedCjsPlugin ( ) : Plugin {
891- return {
892- name : 'rsc:detect-non-optimized-cjs' ,
893- apply : 'serve' ,
894- async transform ( code , id ) {
895- if (
896- id . includes ( '/node_modules/' ) &&
897- ! id . startsWith ( this . environment . config . cacheDir ) &&
898- / \b ( r e q u i r e | e x p o r t s ) \b / . test ( code )
899- ) {
900- id = parseIdQuery ( id ) . filename
901- let isEsm = id . endsWith ( '.mjs' )
902- if ( id . endsWith ( '.js' ) ) {
903- const pkgJsonPath = await findClosestPkgJsonPath ( path . dirname ( id ) )
904- if ( pkgJsonPath ) {
905- const pkgJson = JSON . parse (
906- fs . readFileSync ( pkgJsonPath , 'utf-8' ) ,
907- ) as { type ?: string }
908- isEsm = pkgJson . type === 'module'
909- }
910- }
911- if ( ! isEsm ) {
912- this . warn (
913- `[vite-rsc] found non-optimized CJS dependency in '${ this . environment . name } ' environment. ` +
914- `It is recommended to manually add the dependency to 'environments.${ this . environment . name } .optimizeDeps.include'.` ,
915- )
916- }
917- }
918- } ,
919- }
920- }
921891
922892function scanBuildStripPlugin ( ) : Plugin {
923893 return {
@@ -1993,23 +1963,6 @@ function generateResourcesCode(depsCode: string) {
19931963 `
19941964}
19951965
1996- // https://github.com/vitejs/vite/blob/ea9aed7ebcb7f4be542bd2a384cbcb5a1e7b31bd/packages/vite/src/node/utils.ts#L1469-L1475
1997- function evalValue < T = any > ( rawValue : string ) : T {
1998- const fn = new Function ( `
1999- var console, exports, global, module, process, require
2000- return (\n${ rawValue } \n)
2001- ` )
2002- return fn ( )
2003- }
2004-
2005- // https://github.com/vitejs/vite-plugin-vue/blob/06931b1ea2b9299267374cb8eb4db27c0626774a/packages/plugin-vue/src/utils/query.ts#L13
2006- function parseIdQuery ( id : string ) {
2007- if ( ! id . includes ( '?' ) ) return { filename : id , query : { } }
2008- const [ filename , rawQuery ] = id . split ( `?` , 2 ) as [ string , string ]
2009- const query = Object . fromEntries ( new URLSearchParams ( rawQuery ) )
2010- return { filename, query }
2011- }
2012-
20131966export async function transformRscCssExport ( options : {
20141967 ast : Awaited < ReturnType < typeof parseAstAsync > >
20151968 code : string
@@ -2125,41 +2078,6 @@ function validateImportPlugin(): Plugin {
21252078 }
21262079}
21272080
2128- function vendorUseSyncExternalStorePlugin ( ) : Plugin [ ] {
2129- // vendor and optimize use-sync-external-store out of the box
2130- // since this is a common enough cjs, which tends to break
2131- // other packages (e.g. swr, @tanstack/react-store)
2132-
2133- // https://github.com/facebook/react/blob/c499adf8c89bbfd884f4d3a58c4e510001383525/packages/use-sync-external-store/package.json#L5-L20
2134- const exports = [
2135- 'use-sync-external-store' ,
2136- 'use-sync-external-store/with-selector' ,
2137- 'use-sync-external-store/with-selector.js' ,
2138- 'use-sync-external-store/shim' ,
2139- 'use-sync-external-store/shim/index.js' ,
2140- 'use-sync-external-store/shim/with-selector' ,
2141- 'use-sync-external-store/shim/with-selector.js' ,
2142- ]
2143-
2144- return [
2145- {
2146- name : 'rsc:vendor-use-sync-external-store' ,
2147- apply : 'serve' ,
2148- config ( ) {
2149- return {
2150- environments : {
2151- ssr : {
2152- optimizeDeps : {
2153- include : exports . map ( ( e ) => `${ PKG_NAME } > ${ e } ` ) ,
2154- } ,
2155- } ,
2156- } ,
2157- }
2158- } ,
2159- } ,
2160- ]
2161- }
2162-
21632081function sortObject < T extends object > ( o : T ) {
21642082 return Object . fromEntries (
21652083 Object . entries ( o ) . sort ( ( [ a ] , [ b ] ) => a . localeCompare ( b ) ) ,
0 commit comments