Skip to content

Commit bac4c43

Browse files
committed
refactor: remove patching deps based on version checker
As a code transformer, we don't need to be too smart about deps, we can just use the deps users install
1 parent 89458fc commit bac4c43

File tree

2 files changed

+3
-46
lines changed

2 files changed

+3
-46
lines changed

src/utils/importer.spec.ts

-21
Original file line numberDiff line numberDiff line change
@@ -88,27 +88,6 @@ describe('tryTheseOr', () => {
8888
})
8989
})
9090

91-
describe('patcher', () => {
92-
const patch1 = jest.fn((mod: object) => ({ ...mod, p1: true }))
93-
const patch2 = jest.fn((mod: object) => ({ ...mod, p2: true }))
94-
95-
it('should apply patches correctly', () => {
96-
const imp = new Importer({ foo: [patch1, patch2] })
97-
modules = {
98-
foo: () => ({ foo: true }),
99-
bar: () => ({ bar: true }),
100-
}
101-
expect(imp.tryTheseOr('foo')).toEqual({ foo: true, p1: true, p2: true })
102-
expect(imp.tryTheseOr('foo')).toEqual({ foo: true, p1: true, p2: true })
103-
104-
expect(imp.tryTheseOr('bar')).toEqual({ bar: true })
105-
106-
// ensure cache has been used
107-
expect(patch1).toHaveBeenCalledTimes(1)
108-
expect(patch2).toHaveBeenCalledTimes(1)
109-
})
110-
})
111-
11291
describe('babelCore', () => {
11392
it('should be @babel/core', () => {
11493
modules = {

src/utils/importer.ts

+3-25
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import type { TBabelCore, TBabelJest, TTypeScript, TEsBuild } from '../types'
1+
import type { TBabelCore, TBabelJest, TEsBuild, TTypeScript } from '../types'
22

33
import { rootLogger } from './logger'
44
import { Memoize } from './memoize'
55
import { Errors, Helps, ImportReasons, interpolate } from './messages'
66

7-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
8-
type ModulePatcher<T = any> = (module: T) => T
9-
107
const logger = rootLogger.child({ namespace: 'Importer' })
118

129
// When adding an optional dependency which has another reason, add the reason in ImportReasons, and
@@ -26,14 +23,9 @@ export class Importer {
2623
static get instance(): Importer {
2724
logger.debug('creating Importer singleton')
2825

29-
// here we can define patches to apply to modules.
30-
// it could be fixes that are not deployed, or
31-
// abstractions so that multiple versions work the same
3226
return new Importer()
3327
}
3428

35-
constructor(protected _patches: { [moduleName: string]: ModulePatcher[] } = {}) {}
36-
3729
babelJest(why: ImportReasons): TBabelJest {
3830
return this._import(why, 'babel-jest')
3931
}
@@ -66,13 +58,10 @@ export class Importer {
6658
if (req.exists) {
6759
// module exists
6860
loaded = req as RequireResult<true>
69-
if (loaded.error) {
70-
// require-ing it failed
71-
logger.error({ requireResult: contextReq }, `failed loading module '${name}'`, loaded.error.message)
61+
if (req.error) {
62+
logger.error({ requireResult: contextReq }, `failed loading module '${name}'`, req.error.message)
7263
} else {
73-
// it has been loaded, let's patch it
7464
logger.debug({ requireResult: contextReq }, 'loaded module', name)
75-
loaded.exports = this._patch(name, loaded.exports)
7665
}
7766
break
7867
} else {
@@ -99,17 +88,6 @@ export class Importer {
9988
throw result.error
10089
}
10190

102-
@Memoize((name) => name)
103-
protected _patch<T>(name: string, unpatched: T): T {
104-
if (name in this._patches) {
105-
logger.debug('patching', name)
106-
107-
return this._patches[name].reduce((mod, patcher) => patcher(mod), unpatched)
108-
}
109-
110-
return unpatched
111-
}
112-
11391
protected _import<T>(
11492
why: string,
11593
moduleName: string,

0 commit comments

Comments
 (0)