File tree Expand file tree Collapse file tree 6 files changed +35
-1
lines changed Expand file tree Collapse file tree 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
1111const 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 $ /
1212const ESM_FOLDER_RE = / \/ ( e s | e s m ) \/ ( .* \. j s ) $ /
1313
14+ // https://stackoverflow.com/a/15123777
15+ const COMMENT_RE = / \/ \* [ \s \S ] * ?\* \/ | ( [ ^ \\ : ] | ^ ) \/ \/ .* $ / gm
16+
1417const defaultInline = [
1518 / v i r t u a l : / ,
1619 / \. [ m c ] ? t s $ / ,
@@ -79,7 +82,7 @@ async function isValidNodeImport(id: string) {
7982
8083 const code = await fsp . readFile ( id , 'utf8' ) . catch ( ( ) => '' )
8184
82- return ! ESM_SYNTAX_RE . test ( code )
85+ return ! ESM_SYNTAX_RE . test ( code . replace ( COMMENT_RE , '' ) )
8386}
8487
8588const _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 1616 "@vitest/expect" : " workspace:*" ,
1717 "@vitest/mocker" : " workspace:*" ,
1818 "@vitest/runner" : " workspace:*" ,
19+ "@vitest/test-dep-esm-comment" : " file:./deps/dep-esm-comment" ,
1920 "@vitest/test-dep1" : " file:./deps/dep1" ,
2021 "@vitest/test-dep2" : " file:./deps/dep2" ,
2122 "@vitest/utils" : " workspace:*" ,
Original file line number Diff line number Diff line change 1+ import { createRequire } from 'node:module'
12import { expect , test } from 'vitest'
23
34// @ts -expect-error no ts
@@ -6,7 +7,17 @@ import * as dep1 from '@vitest/test-dep1'
67// @ts -expect-error no ts
78import * as dep2 from '@vitest/test-dep2'
89
10+ // @ts -expect-error no ts
11+ import depEsmComment from '@vitest/test-dep-esm-comment'
12+
13+ const require = createRequire ( import . meta. url )
14+
915test ( 'no dual package hazard by externalizing esm deps by default' , async ( ) => {
1016 dep1 . data . hello = 'world'
1117 expect ( dep2 . data . hello ) . toBe ( 'world' )
1218} )
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