File tree 6 files changed +35
-1
lines changed
6 files changed +35
-1
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,9 @@ const ESM_SYNTAX_RE
11
11
const ESM_EXT_RE = / \. ( e s | e s m | e s m - b r o w s e r | e s m - b u n d l e r | e s 6 | m o d u l e ) \. j s $ /
12
12
const ESM_FOLDER_RE = / \/ ( e s | e s m ) \/ ( .* \. j s ) $ /
13
13
14
+ // https://stackoverflow.com/a/15123777
15
+ const COMMENT_RE = / \/ \* [ \s \S ] * ?\* \/ | ( [ ^ \\ : ] | ^ ) \/ \/ .* $ / gm
16
+
14
17
const defaultInline = [
15
18
/ v i r t u a l : / ,
16
19
/ \. [ m c ] ? t s $ / ,
@@ -79,7 +82,7 @@ async function isValidNodeImport(id: string) {
79
82
80
83
const code = await fsp . readFile ( id , 'utf8' ) . catch ( ( ) => '' )
81
84
82
- return ! ESM_SYNTAX_RE . test ( code )
85
+ return ! ESM_SYNTAX_RE . test ( code . replace ( COMMENT_RE , '' ) )
83
86
}
84
87
85
88
const _defaultExternalizeCache = new Map < string , Promise < string | false > > ( )
Original file line number Diff line number Diff line change
1
+ // import x from "x"
2
+ /** import x from "x" */
3
+ /**
4
+ * import x from "x"
5
+ */
6
+ module . exports = { test : 'ok' }
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " @vitest/test-dep-esm-comment" ,
3
+ "type" : " commonjs" ,
4
+ "exports" : " ./index.js"
5
+ }
Original file line number Diff line number Diff line change 16
16
"@vitest/expect" : " workspace:*" ,
17
17
"@vitest/mocker" : " workspace:*" ,
18
18
"@vitest/runner" : " workspace:*" ,
19
+ "@vitest/test-dep-esm-comment" : " file:./deps/dep-esm-comment" ,
19
20
"@vitest/test-dep1" : " file:./deps/dep1" ,
20
21
"@vitest/test-dep2" : " file:./deps/dep2" ,
21
22
"@vitest/utils" : " workspace:*" ,
Original file line number Diff line number Diff line change
1
+ import { createRequire } from 'node:module'
1
2
import { expect , test } from 'vitest'
2
3
3
4
// @ts -expect-error no ts
@@ -6,7 +7,17 @@ import * as dep1 from '@vitest/test-dep1'
6
7
// @ts -expect-error no ts
7
8
import * as dep2 from '@vitest/test-dep2'
8
9
10
+ // @ts -expect-error no ts
11
+ import depEsmComment from '@vitest/test-dep-esm-comment'
12
+
13
+ const require = createRequire ( import . meta. url )
14
+
9
15
test ( 'no dual package hazard by externalizing esm deps by default' , async ( ) => {
10
16
dep1 . data . hello = 'world'
11
17
expect ( dep2 . data . hello ) . toBe ( 'world' )
12
18
} )
19
+
20
+ test ( 'externalize cjs with esm comment' , async ( ) => {
21
+ const depEsmCommentRequire = require ( '@vitest/test-dep-esm-comment' )
22
+ expect ( depEsmComment ) . toBe ( depEsmCommentRequire )
23
+ } )
You can’t perform that action at this time.
0 commit comments