Skip to content

Commit

Permalink
Fix(Cross Import): The cache is not deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
1aron committed Jun 12, 2023
1 parent 8e7fe2c commit c5a5e56
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
**/*node_modules
**/*.turbo
**/*out
**/*config-tmp.*
**/*tmp.*

# Only exists if Bazel was run
/bazel-out
Expand Down
9 changes: 2 additions & 7 deletions packages/cross-import/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
"name": "cross-import",
"scripts": {
"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",
"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",
"build:type": "tsc --emitDeclarationOnly --preserveWatchOutput",
"build": "npm run build:cjs && npm run build:esm && npm run build:type",
"build": "npm run build:cjs && npm run build:type",
"dev": "conc 'npm:build:* -- --watch'",
"test": "node -r sucrase/register ./tests/real && jest",
"type-check": "tsc --noEmit",
Expand Down Expand Up @@ -35,7 +34,6 @@
"read",
"cross",
"environments",
"esm",
"commonjs",
"ts",
"typescript"
Expand All @@ -45,14 +43,11 @@
"access": "public"
},
"main": "./dist/index.js",
"jsnext:main": "./dist/index.esm.mjs",
"esnext": "./dist/index.esm.mjs",
"module": "./dist/index.esm.mjs",
"types": "./dist/index.d.ts",
"exports": {
".": {
"require": "./dist/index.js",
"import": "./dist/index.esm.mjs",
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
}
},
Expand Down
37 changes: 0 additions & 37 deletions packages/cross-import/src/index.esm.ts

This file was deleted.

21 changes: 19 additions & 2 deletions packages/cross-import/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,31 @@ export default function crossImport(
if (!filePath) return
const resolvedFilePath = path.resolve(options.cwd, filePath)
if (process.env.DEBUG) {
console.log('[crossImport] resolvedFilePath:', resolvedFilePath)
console.log('[DEBUG: Cross Import] resolvedFilePath:', resolvedFilePath)
}

/** try to delete cache first */
try {
if (require.cache[resolvedFilePath]) {
delete require.cache[resolvedFilePath]
if (process.env.DEBUG) {
console.log('[DEBUG: Cross Import] delete cache')
}
}
} catch { /* empty */ }

try {
delete require.cache[resolvedFilePath]
if (process.env.DEBUG) {
console.log('[DEBUG: Cross Import] require')
}
return require(resolvedFilePath)
} catch {
if (process.env.DEBUG) {
console.log('[Cross Import] JITI')
}
return jiti(__filename, {
interopDefault: true,
cache: false,
transform: (options) => {
return transform(options.source, {
transforms: ['imports', 'typescript'],
Expand Down
20 changes: 18 additions & 2 deletions packages/cross-import/tests/real.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from 'path'
/**
* 測試 require cache 在真實 Node 環境運作時是否有被正確的清除
*/
const configPath = path.join(__dirname, 'config-tmp.js')
const configPath = path.join(__dirname, 'config.tmp.js')

fs.writeFileSync(configPath, 'module.exports = { a: 0 }')
const module1 = crossImport(configPath)
Expand All @@ -17,4 +17,20 @@ fs.writeFileSync(configPath, 'module.exports = { b: 0 }')
const module2 = crossImport(configPath)
if (module2.b !== 0) {
throw new Error(`crossImport(${module1}) module2.b !== 0`)
}
}

// ts
const tsConfigPath = path.join(__dirname, 'config.tmp.ts')

fs.writeFileSync(tsConfigPath, 'module.exports = { a: 0 }')
const tsModule1 = crossImport(tsConfigPath)
if (tsModule1.a !== 0) {
throw new Error(`crossImport(${tsModule1}) module1.a !== 0`)
}

fs.writeFileSync(tsConfigPath, 'module.exports = { b: 0 }')
const tsModule2 = crossImport(tsConfigPath)
if (tsModule2.b !== 0) {
throw new Error(`crossImport(${module1}) tsModule2.b !== 0`)
}

6 changes: 5 additions & 1 deletion packages/cross-import/tests/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import crossImport from '../src'
import fs from 'fs'
import path from 'path'

it('import .ts in .js', () => {
expect(
Expand Down Expand Up @@ -31,4 +33,6 @@ it('read non-existent file', () => {
crossImport('idontexist.ts', { cwd: __dirname })
)
.toBeUndefined()
})
})

// Do not test secondary imports in a test environment, inaccurate.

0 comments on commit c5a5e56

Please sign in to comment.