@@ -21,7 +21,7 @@ import {
21
21
normalizePath ,
22
22
parseAstAsync ,
23
23
} from 'vite'
24
- import { crawlFrameworkPkgs , findClosestPkgJsonPath } from 'vitefu'
24
+ import { crawlFrameworkPkgs } from 'vitefu'
25
25
import vitePluginRscCore from './core/plugin'
26
26
import {
27
27
type TransformWrapExportFilter ,
@@ -33,6 +33,8 @@ import {
33
33
import { generateEncryptionKey , toBase64 } from './utils/encryption-utils'
34
34
import { createRpcServer } from './utils/rpc'
35
35
import { normalizeViteImportAnalysisUrl , prepareError } from './vite-utils'
36
+ import { cjsModuleRunnerPlugin } from './plugins/cjs'
37
+ import { evalValue , parseIdQuery } from './plugins/utils'
36
38
37
39
// state for build orchestration
38
40
let serverReferences : Record < string , string > = { }
@@ -882,42 +884,10 @@ globalThis.AsyncLocalStorage = __viteRscAyncHooks.AsyncLocalStorage;
882
884
...( rscPluginOptions . validateImports !== false
883
885
? [ validateImportPlugin ( ) ]
884
886
: [ ] ) ,
885
- ...vendorUseSyncExternalStorePlugin ( ) ,
886
887
scanBuildStripPlugin ( ) ,
887
- detectNonOptimizedCjsPlugin ( ) ,
888
+ ... cjsModuleRunnerPlugin ( ) ,
888
889
]
889
890
}
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
- }
921
891
922
892
function scanBuildStripPlugin ( ) : Plugin {
923
893
return {
@@ -1993,23 +1963,6 @@ function generateResourcesCode(depsCode: string) {
1993
1963
`
1994
1964
}
1995
1965
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
-
2013
1966
export async function transformRscCssExport ( options : {
2014
1967
ast : Awaited < ReturnType < typeof parseAstAsync > >
2015
1968
code : string
@@ -2125,41 +2078,6 @@ function validateImportPlugin(): Plugin {
2125
2078
}
2126
2079
}
2127
2080
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
-
2163
2081
function sortObject < T extends object > ( o : T ) {
2164
2082
return Object . fromEntries (
2165
2083
Object . entries ( o ) . sort ( ( [ a ] , [ b ] ) => a . localeCompare ( b ) ) ,
0 commit comments