How does javascript import work in Moodle

How does javascript import work in Moodle

by Howard Miller -
Number of replies: 2
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

I'm not sure if this is a crazy question...

When creating javascript in Moodle, I can do something like...

import {get_string as getString} from core/str;

Now get_string is clearly exported from lib/amd/src/str.js

My question... how does this work? How does the import get "translated" to know where core/str actually lives in the running application? Just curious. 

Average of ratings: Useful (1)
In reply to Howard Miller

Re: How does javascript import work in Moodle

by lior gil -
Picture of Core developers
The paths are mapped with RequireJS that sets the root path and also resolves the full path of the module. Without it you'd need to find the proper path yourself, so this method relieves you from this headache.
Average of ratings: Useful (1)
In reply to lior gil

Re: How does javascript import work in Moodle

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Makes sense. RequireJS is mostly dark magic to me smile