Skip to content

Commit c5a5e56

Browse files
committed
Fix(Cross Import): The cache is not deleted
1 parent 8e7fe2c commit c5a5e56

File tree

6 files changed

+45
-50
lines changed

6 files changed

+45
-50
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
**/*node_modules
1111
**/*.turbo
1212
**/*out
13-
**/*config-tmp.*
13+
**/*tmp.*
1414

1515
# Only exists if Bazel was run
1616
/bazel-out

packages/cross-import/package.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
"name": "cross-import",
33
"scripts": {
44
"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",
65
"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",
87
"dev": "conc 'npm:build:* -- --watch'",
98
"test": "node -r sucrase/register ./tests/real && jest",
109
"type-check": "tsc --noEmit",
@@ -35,7 +34,6 @@
3534
"read",
3635
"cross",
3736
"environments",
38-
"esm",
3937
"commonjs",
4038
"ts",
4139
"typescript"
@@ -45,14 +43,11 @@
4543
"access": "public"
4644
},
4745
"main": "./dist/index.js",
48-
"jsnext:main": "./dist/index.esm.mjs",
49-
"esnext": "./dist/index.esm.mjs",
50-
"module": "./dist/index.esm.mjs",
5146
"types": "./dist/index.d.ts",
5247
"exports": {
5348
".": {
5449
"require": "./dist/index.js",
55-
"import": "./dist/index.esm.mjs",
50+
"import": "./dist/index.js",
5651
"types": "./dist/index.d.ts"
5752
}
5853
},

packages/cross-import/src/index.esm.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

packages/cross-import/src/index.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,31 @@ export default function crossImport(
1414
if (!filePath) return
1515
const resolvedFilePath = path.resolve(options.cwd, filePath)
1616
if (process.env.DEBUG) {
17-
console.log('[crossImport] resolvedFilePath:', resolvedFilePath)
17+
console.log('[DEBUG: Cross Import] resolvedFilePath:', resolvedFilePath)
1818
}
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+
1930
try {
20-
delete require.cache[resolvedFilePath]
31+
if (process.env.DEBUG) {
32+
console.log('[DEBUG: Cross Import] require')
33+
}
2134
return require(resolvedFilePath)
2235
} catch {
36+
if (process.env.DEBUG) {
37+
console.log('[Cross Import] JITI')
38+
}
2339
return jiti(__filename, {
2440
interopDefault: true,
41+
cache: false,
2542
transform: (options) => {
2643
return transform(options.source, {
2744
transforms: ['imports', 'typescript'],

packages/cross-import/tests/real.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import path from 'path'
55
/**
66
* 測試 require cache 在真實 Node 環境運作時是否有被正確的清除
77
*/
8-
const configPath = path.join(__dirname, 'config-tmp.js')
8+
const configPath = path.join(__dirname, 'config.tmp.js')
99

1010
fs.writeFileSync(configPath, 'module.exports = { a: 0 }')
1111
const module1 = crossImport(configPath)
@@ -17,4 +17,20 @@ fs.writeFileSync(configPath, 'module.exports = { b: 0 }')
1717
const module2 = crossImport(configPath)
1818
if (module2.b !== 0) {
1919
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+

packages/cross-import/tests/test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import crossImport from '../src'
2+
import fs from 'fs'
3+
import path from 'path'
24

35
it('import .ts in .js', () => {
46
expect(
@@ -31,4 +33,6 @@ it('read non-existent file', () => {
3133
crossImport('idontexist.ts', { cwd: __dirname })
3234
)
3335
.toBeUndefined()
34-
})
36+
})
37+
38+
// Do not test secondary imports in a test environment, inaccurate.

0 commit comments

Comments
 (0)