File tree Expand file tree Collapse file tree 6 files changed +45
-50
lines changed Expand file tree Collapse file tree 6 files changed +45
-50
lines changed Original file line number Diff line number Diff line change 10
10
** /* node_modules
11
11
** /* .turbo
12
12
** /* out
13
- ** /* config- tmp.*
13
+ ** /* tmp. *
14
14
15
15
# Only exists if Bazel was run
16
16
/bazel-out
Original file line number Diff line number Diff line change 2
2
"name" : " cross-import" ,
3
3
"scripts" : {
4
4
"build:cjs" : " esbuild src/index.ts --bundle --outfile=dist/index.js --format=cjs --minify --sourcemap --platform=node --external:esbuild --external:fast-glob --external:jiti --external:@techor/extend --external:sucrase --external:upath" ,
5
- "build:esm" : " esbuild src/index.esm.ts --bundle --outfile=dist/index.esm.mjs --format=esm --minify --sourcemap --platform=node --external:esbuild --external:fast-glob --external:jiti --external:@techor/extend --external:sucrase --external:upath" ,
6
5
"build:type" : " tsc --emitDeclarationOnly --preserveWatchOutput" ,
7
- "build" : " npm run build:cjs && npm run build:esm && npm run build: type" ,
6
+ "build" : " npm run build:cjs && npm run build:type" ,
8
7
"dev" : " conc 'npm:build:* -- --watch'" ,
9
8
"test" : " node -r sucrase/register ./tests/real && jest" ,
10
9
"type-check" : " tsc --noEmit" ,
35
34
" read" ,
36
35
" cross" ,
37
36
" environments" ,
38
- " esm" ,
39
37
" commonjs" ,
40
38
" ts" ,
41
39
" typescript"
45
43
"access" : " public"
46
44
},
47
45
"main" : " ./dist/index.js" ,
48
- "jsnext:main" : " ./dist/index.esm.mjs" ,
49
- "esnext" : " ./dist/index.esm.mjs" ,
50
- "module" : " ./dist/index.esm.mjs" ,
51
46
"types" : " ./dist/index.d.ts" ,
52
47
"exports" : {
53
48
"." : {
54
49
"require" : " ./dist/index.js" ,
55
- "import" : " ./dist/index.esm.mjs " ,
50
+ "import" : " ./dist/index.js " ,
56
51
"types" : " ./dist/index.d.ts"
57
52
}
58
53
},
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -14,14 +14,31 @@ export default function crossImport(
14
14
if ( ! filePath ) return
15
15
const resolvedFilePath = path . resolve ( options . cwd , filePath )
16
16
if ( process . env . DEBUG ) {
17
- console . log ( '[crossImport ] resolvedFilePath:' , resolvedFilePath )
17
+ console . log ( '[DEBUG: Cross Import ] resolvedFilePath:' , resolvedFilePath )
18
18
}
19
+
20
+ /** try to delete cache first */
21
+ try {
22
+ if ( require . cache [ resolvedFilePath ] ) {
23
+ delete require . cache [ resolvedFilePath ]
24
+ if ( process . env . DEBUG ) {
25
+ console . log ( '[DEBUG: Cross Import] delete cache' )
26
+ }
27
+ }
28
+ } catch { /* empty */ }
29
+
19
30
try {
20
- delete require . cache [ resolvedFilePath ]
31
+ if ( process . env . DEBUG ) {
32
+ console . log ( '[DEBUG: Cross Import] require' )
33
+ }
21
34
return require ( resolvedFilePath )
22
35
} catch {
36
+ if ( process . env . DEBUG ) {
37
+ console . log ( '[Cross Import] JITI' )
38
+ }
23
39
return jiti ( __filename , {
24
40
interopDefault : true ,
41
+ cache : false ,
25
42
transform : ( options ) => {
26
43
return transform ( options . source , {
27
44
transforms : [ 'imports' , 'typescript' ] ,
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import path from 'path'
5
5
/**
6
6
* 測試 require cache 在真實 Node 環境運作時是否有被正確的清除
7
7
*/
8
- const configPath = path . join ( __dirname , 'config- tmp.js' )
8
+ const configPath = path . join ( __dirname , 'config. tmp.js' )
9
9
10
10
fs . writeFileSync ( configPath , 'module.exports = { a: 0 }' )
11
11
const module1 = crossImport ( configPath )
@@ -17,4 +17,20 @@ fs.writeFileSync(configPath, 'module.exports = { b: 0 }')
17
17
const module2 = crossImport ( configPath )
18
18
if ( module2 . b !== 0 ) {
19
19
throw new Error ( `crossImport(${ module1 } ) module2.b !== 0` )
20
- }
20
+ }
21
+
22
+ // ts
23
+ const tsConfigPath = path . join ( __dirname , 'config.tmp.ts' )
24
+
25
+ fs . writeFileSync ( tsConfigPath , 'module.exports = { a: 0 }' )
26
+ const tsModule1 = crossImport ( tsConfigPath )
27
+ if ( tsModule1 . a !== 0 ) {
28
+ throw new Error ( `crossImport(${ tsModule1 } ) module1.a !== 0` )
29
+ }
30
+
31
+ fs . writeFileSync ( tsConfigPath , 'module.exports = { b: 0 }' )
32
+ const tsModule2 = crossImport ( tsConfigPath )
33
+ if ( tsModule2 . b !== 0 ) {
34
+ throw new Error ( `crossImport(${ module1 } ) tsModule2.b !== 0` )
35
+ }
36
+
Original file line number Diff line number Diff line change 1
1
import crossImport from '../src'
2
+ import fs from 'fs'
3
+ import path from 'path'
2
4
3
5
it ( 'import .ts in .js' , ( ) => {
4
6
expect (
@@ -31,4 +33,6 @@ it('read non-existent file', () => {
31
33
crossImport ( 'idontexist.ts' , { cwd : __dirname } )
32
34
)
33
35
. toBeUndefined ( )
34
- } )
36
+ } )
37
+
38
+ // Do not test secondary imports in a test environment, inaccurate.
You can’t perform that action at this time.
0 commit comments