enonic / xp

Enonic XP
https://enonic.com
GNU General Public License v3.0
202 stars 34 forks source link

Require resolution #6917

Closed GlennRicaud closed 5 years ago

GlennRicaud commented 5 years ago
GlennRicaud commented 5 years ago

The real resolution if different from what is documented. There is no recursive going to the parent until ''/resources'. But other capabilities are there and not documented.

NodeJS There are similarities If the file extension is not specified, it also looks for .js or .json with the same name or for an index.js or index.json under a folder of this name Because of the differences platform there is no searching at multiple location. And absolute paths have a different meaning.

PurpleJS Nearly identical. 2 differences:

More explanation on the exact behaviour of the require resolution in Enonic XP:

If path starts with '/'
  Resolve absolute
Else if path starts with './' or '../'
  Resolve relative
Else
  Resolve lib scan

Resolve absolute:
  key = baseKey.resolve(path) //The key path will be the path received as parameter (Base key path is ignored)
  doResolve(key)

Resolve relative
  key = baseKey.resolve('../' + path) //The key path will be as if the path received as parameter was resolved on the folder containing the script
  doResolve(key)

Resolve lib scan
  key = baseKey.resolve('/lib' + path) //The key path will be the path resolved under the '/lib' folder
  doResolve(key)
  If key does not exist
    Resolve relative /The key path will be the path resolved under the '/lib' folder

doResolve(key)
  paths = findAllSearchPaths(key)
  For each path
    if key exists -> return key
  return key

findAllSearchPaths(path)
  paths = findSearchPaths
  Duplicate all path with '/site' before the path (Site paths are added after)

findSearchPaths(path)
  If path has file extension -> return this path
  Else return a list of 4 possible paths:
    path + '.js'
    path + '/index.js'
    path + '.json'
    path + '/index.json'

Examples with current script being /folder/script.js:

For '/aaa':
0 = "/aaa.js"
1 = "/aaa/index.js"
2 = "/aaa.json"
3 = "/aaa/index.json"
4 = "/site/aaa.js"
5 = "/site/aaa/index.js"
6 = "/site/aaa.json"
7 = "/site/aaa/index.json"

For '/aaa.js':
0 = "/aaa.js"
1 = "/site/aaa.js"

For './aaa.js':
0 = "/folder/aaa.js"
1 = "/site/folder/aaa.js"

For '../aaa.js'
0 = "/aaa.js"
1 = "/site/aaa.js"

For '../../aaa.js'
0 = "/aaa.js"
1 = "/site/aaa.js"

For 'aaa.js'
0 = "/lib/aaa.js"
1 = "/site/lib/aaa.js"
+
0 = "/folder/aaa.js"
1 = "/site/folder/aaa.js"
sigdestad commented 5 years ago

Lets discuss this..