Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
fix: do not cache require, require is already cached in NodeJS by des…
Browse files Browse the repository at this point in the history
…ign (#194)

* NodeJS caches the result of require calls in require.cache, building an additional caching layer (deps.ts) on top of that has no value and only breaks JS packaging tools such as webpack/rollup.

* Ignore node/no-missing-require for deps.ts; the error is caused by eslint not detecting ts files as valid js modules.
  • Loading branch information
Codeneos committed Aug 7, 2020
1 parent a924068 commit 99b9466
Showing 1 changed file with 16 additions and 27 deletions.
43 changes: 16 additions & 27 deletions src/deps.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,45 @@
const cache: any = {}

function fetch(s: string) {
if (!cache[s]) {
cache[s] = require(s)
}
return cache[s]
}

export const deps = {
/* eslint-disable node/no-missing-require */
export default {
get stripAnsi(): (string: string) => string {
return fetch('strip-ansi')
return require('strip-ansi')
},
get ansiStyles(): typeof import('ansi-styles') {
return fetch('ansi-styles')
return require('ansi-styles')
},
get ansiEscapes(): any {
return fetch('ansi-escapes')
return require('ansi-escapes')
},
get passwordPrompt(): any {
return fetch('password-prompt')
return require('password-prompt')
},
get screen(): typeof import('@oclif/screen') {
return fetch('@oclif/screen')
return require('@oclif/screen')
},

get open(): typeof import('./open').default {
return fetch('./open').default
return require('./open').default
},
get prompt(): typeof import('./prompt') {
return fetch('./prompt')
return require('./prompt')
},
get styledObject(): typeof import('./styled/object').default {
return fetch('./styled/object').default
return require('./styled/object').default
},
get styledHeader(): typeof import('./styled/header').default {
return fetch('./styled/header').default
return require('./styled/header').default
},
get styledJSON(): typeof import('./styled/json').default {
return fetch('./styled/json').default
return require('./styled/json').default
},
get table(): typeof import('./styled/table').table {
return fetch('./styled/table').table
return require('./styled/table').table
},
get tree(): typeof import('./styled/tree').default {
return fetch('./styled/tree').default
return require('./styled/tree').default
},
get wait(): typeof import('./wait').default {
return fetch('./wait').default
return require('./wait').default
},
get progress(): typeof import ('./styled/progress').default {
return fetch('./styled/progress').default
return require('./styled/progress').default
},
}

export default deps

0 comments on commit 99b9466

Please sign in to comment.