forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 17
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
-
pick an algorithm 1. if the path starts with a '.'
- append it the directory of the current file
- proceed to 3 1. if the path starts with a '/'
- search for this exact path
- proceed to 3 1. proceed to 2
-
search for a named module 1.
moduleName
=dirname
(require path)
1. match ifmoduleName
is or starts with one of these aliasesalias directory ui
src/ui/public testHarness
src/testHarness/public plugins/${plugin.id}
${plugin.directory}/public - replace the alias with the match and continue to 3 1. match when any of these conditions are met:
-
./webpackShims/${moduleName}
is a directory -
./node_modules/${moduleName}
is a directory 1. if no match was found - move to the parent directory
- start again at 2.iii until reaching the root directory or a match is found 1. if a match was found
- replace the
moduleName
prefix from the require statement with the full path of the match and proceed to 3
-
search for a file 1. the first of the following paths that resolves to a file is our match
- path + '.js'
- path + '.json'
- path + '.jsx'
- path + '.less'
- path
- path/${basename(path)} + '.js'
- path/${basename(path)} + '.json'
- path/${basename(path)} + '.jsx'
- path/${basename(path)} + '.less'
- path/${basename(path)}
- path/index + '.js'
- path/index + '.json'
- path/index + '.jsx'
- path/index + '.less'
- path/index 1. if none of the above paths matches then an error is thrown