Skip to content

Resolving Require Paths

Spencer edited this page Dec 17, 2015 · 3 revisions

With Kibana 4.2 we started using WebPack to bundle Kibana's dependencies.

Along with the change came some new semantics around how require statements resolve was introduced. It's based on the node.js module lookup algorithm, but we have extended it slightly. Here is how require statements are resolved to a file:

if you're familiar with the node.js algorithm, the changes are in 2.ii and 3.i.f to 3.i.g

  1. pick an algorithm 1. if the path starts with a '.'

    1. append it the directory of the current file
    2. proceed to 3 1. if the path starts with a '/'
    3. search for this exact path
    4. proceed to 3 1. proceed to 2
  2. search for a named module 1. moduleName = dirname(require path) 1. match if moduleName is or starts with one of these aliases

    alias directory
    ui src/ui/public
    testHarness src/testHarness/public
    plugins/${plugin.id} ${plugin.directory}/public
    1. replace the alias with the match and continue to 3 1. match when any of these conditions are met:
    2. ./webpackShims/${moduleName} is a directory
    3. ./node_modules/${moduleName} is a directory 1. if no match was found
    4. move to the parent directory
    5. start again at 2.iii until reaching the root directory or a match is found 1. if a match was found
    6. replace the moduleName prefix from the require statement with the full path of the match and proceed to 3
  3. search for a file 1. the first of the following paths that resolves to a file is our match

    1. path + '.js'
    2. path + '.json'
    3. path + '.jsx'
    4. path + '.less'
    5. path
    6. path/${basename(path)} + '.js'
    7. path/${basename(path)} + '.json'
    8. path/${basename(path)} + '.jsx'
    9. path/${basename(path)} + '.less'
    10. path/${basename(path)}
    11. path/index + '.js'
    12. path/index + '.json'
    13. path/index + '.jsx'
    14. path/index + '.less'
    15. path/index 1. if none of the above paths matches then an error is thrown