1
- import fs from 'fs'
2
1
import path from 'path'
3
2
import type { InternalResolveOptions } from '../plugins/resolve'
4
3
import { tryNodeResolve } from '../plugins/resolve'
5
- import {
6
- createDebugger ,
7
- isDefined ,
8
- lookupFile ,
9
- normalizePath ,
10
- resolveFrom
11
- } from '../utils'
4
+ import { createDebugger , isDefined , lookupFile , resolveFrom } from '../utils'
12
5
import type { Logger , ResolvedConfig } from '..'
13
6
import { createFilter } from '@rollup/pluginutils'
14
7
@@ -79,9 +72,6 @@ export function resolveSSRExternal(
79
72
return externals
80
73
}
81
74
82
- const CJS_CONTENT_RE =
83
- / \b m o d u l e \. e x p o r t s \b | \b e x p o r t s [ . \[ ] | \b r e q u i r e \s * \( | \b O b j e c t \. ( d e f i n e P r o p e r t y | d e f i n e P r o p e r t i e s | a s s i g n ) \s * \( \s * e x p o r t s \b /
84
-
85
75
// do we need to do this ahead of time or could we do it lazily?
86
76
function collectExternals (
87
77
root : string ,
@@ -115,7 +105,6 @@ function collectExternals(
115
105
seen . add ( id )
116
106
117
107
let esmEntry : string | undefined
118
- let requireEntry : string
119
108
try {
120
109
esmEntry = tryNodeResolve (
121
110
id ,
@@ -125,9 +114,6 @@ function collectExternals(
125
114
undefined ,
126
115
true
127
116
) ?. id
128
- // normalizePath required for windows. tryNodeResolve uses normalizePath
129
- // which returns with '/', require.resolve returns with '\\'
130
- requireEntry = normalizePath ( require . resolve ( id , { paths : [ root ] } ) )
131
117
} catch ( e ) {
132
118
try {
133
119
// no main entry, but deep imports may be allowed
@@ -152,38 +138,9 @@ function collectExternals(
152
138
else if ( ! esmEntry . includes ( 'node_modules' ) ) {
153
139
const pkgPath = resolveFrom ( `${ id } /package.json` , root )
154
140
depsToTrace . add ( path . dirname ( pkgPath ) )
155
- }
156
- // has separate esm/require entry, assume require entry is cjs
157
- else if ( esmEntry !== requireEntry ) {
141
+ } else {
158
142
ssrExternals . add ( id )
159
143
}
160
- // if we're externalizing ESM and CJS should basically just always do it?
161
- // or are there others like SystemJS / AMD that we'd need to handle?
162
- // for now, we'll just leave this as is
163
- else if ( / \. m ? j s $ / . test ( esmEntry ) ) {
164
- const pkgPath = resolveFrom ( `${ id } /package.json` , root )
165
- const pkgContent = fs . readFileSync ( pkgPath , 'utf-8' )
166
-
167
- if ( ! pkgContent ) {
168
- continue
169
- }
170
- const pkg = JSON . parse ( pkgContent )
171
-
172
- if ( pkg . type === 'module' || esmEntry . endsWith ( '.mjs' ) ) {
173
- ssrExternals . add ( id )
174
- continue
175
- }
176
- // check if the entry is cjs
177
- const content = fs . readFileSync ( esmEntry , 'utf-8' )
178
- if ( CJS_CONTENT_RE . test ( content ) ) {
179
- ssrExternals . add ( id )
180
- continue
181
- }
182
-
183
- logger . warn (
184
- `${ id } doesn't appear to be written in CJS, but also doesn't appear to be a valid ES module (i.e. it doesn't have "type": "module" or an .mjs extension for the entry point). Please contact the package author to fix.`
185
- )
186
- }
187
144
}
188
145
189
146
for ( const depRoot of depsToTrace ) {
0 commit comments