Skip to content

Commit

Permalink
perf: defer requiring most external libs until needed (#584)
Browse files Browse the repository at this point in the history
* perf: defer requiring most external libs until needed

* perf: do not require string-width more than once
  • Loading branch information
nexdrew authored and bcoe committed Aug 14, 2016
1 parent 59474dc commit f9b0ed4
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
6 changes: 2 additions & 4 deletions lib/command.js
@@ -1,7 +1,5 @@
const path = require('path')
const inspect = require('util').inspect
const requireDirectory = require('require-directory')
const whichModule = require('which-module')

// handles parsing positional arguments,
// and populating argv with said positional
Expand Down Expand Up @@ -62,13 +60,13 @@ module.exports = function (yargs, usage, validation) {
}
return visited
}
requireDirectory({ require: req, filename: callerFile }, dir, opts)
require('require-directory')({ require: req, filename: callerFile }, dir, opts)
}

// lookup module object from require()d command and derive name
// if module was not require()d and no name given, throw error
function moduleName (obj) {
const mod = whichModule(obj)
const mod = require('which-module')(obj)
if (!mod) throw new Error('No command name given for module: ' + inspect(obj))
return commandFromFilename(mod.filename)
}
Expand Down
8 changes: 3 additions & 5 deletions lib/usage.js
@@ -1,9 +1,6 @@
// this file handles outputting usage instructions,
// failures, etc. keeps logging in one place.
const cliui = require('cliui')
const decamelize = require('decamelize')
const stringWidth = require('string-width')
const wsize = require('window-size')
const objFilter = require('./obj-filter')
const setBlocking = require('set-blocking')

Expand Down Expand Up @@ -125,7 +122,7 @@ module.exports = function (yargs, y18n) {
return acc
}, {})
)
var ui = cliui({
var ui = require('cliui')({
width: wrap,
wrap: !!wrap
})
Expand Down Expand Up @@ -329,7 +326,7 @@ module.exports = function (yargs, y18n) {
}

self.functionDescription = function (fn) {
var description = fn.name ? decamelize(fn.name, '-') : __('generated-value')
var description = fn.name ? require('decamelize')(fn.name, '-') : __('generated-value')
return ['(', description, ')'].join('')
}

Expand Down Expand Up @@ -375,6 +372,7 @@ module.exports = function (yargs, y18n) {

// guess the width of the console window, max-width 80.
function windowWidth () {
const wsize = require('window-size')
return wsize.width ? Math.min(80, wsize.width) : null
}

Expand Down
6 changes: 2 additions & 4 deletions yargs.js
@@ -1,13 +1,11 @@
const assert = require('assert')
const assign = require('lodash.assign')
const Command = require('./lib/command')
const Completion = require('./lib/completion')
const Parser = require('yargs-parser')
const path = require('path')
const Usage = require('./lib/usage')
const Validation = require('./lib/validation')
const Y18n = require('y18n')
const requireMainFilename = require('require-main-filename')
const objFilter = require('./lib/obj-filter')
const setBlocking = require('set-blocking')

Expand Down Expand Up @@ -377,7 +375,7 @@ function Yargs (processArgs, cwd, parentRequire) {
var obj = {}
try {
obj = readPkgUp.sync({
cwd: path || requireMainFilename(parentRequire || require)
cwd: path || require('require-main-filename')(parentRequire || require)
})
} catch (noop) {}

Expand Down Expand Up @@ -475,7 +473,7 @@ function Yargs (processArgs, cwd, parentRequire) {
}
self.getGroups = function () {
// combine explicit and preserved groups. explicit groups should be first
return assign({}, groups, preservedGroups)
return require('lodash.assign')({}, groups, preservedGroups)
}

// as long as options.envPrefix is not undefined,
Expand Down

0 comments on commit f9b0ed4

Please sign in to comment.