The lua-import module provides a function. The function takes single string argument same as require, but the argument can be a relative path to the required module. The return value is the module referred by the path argument.
luarocks install lua-import
Copy import.lua to lua/
directory in your project.
Add below line to init.lua or entry point of your project
require('import')
I use this package in my neovim configuration which was the main inspiration for building this package. As neovim configuration directories get significantly nested, the require statements on top of some files start looking like horizontal bar chart.
To simplify require statements in neovim configurations:
- Copy import.lua to
lua/
directory in your neovim configuration. - Add
require('import')
toinit.lua
.
Then you can use import
function in lua files anywhere in your configurations.
Below is the directory structure of the tests in this package, all examples are based on it.
spec
├── fixture_three.lua
└── unit
├── fixture_one.lua
├── fixture_two
│ ├── init.lua
│ └── two_dot_one.lua
└── import_spec.lua
3 directories, 5 files
-- will import same as require
local m = import('spec.unit.fixture_one')
-- will same as require with filepath separator
local m = import('spec/unit/fixture_one')
-- will import relative to current directory
local m = import('./fixture_one')
-- will import relative to current directory without filepath separator
local m = import('fixture_one')
-- will import relative to current directory with init.lua
local m = import('./fixture_two')
-- will import relative to current directory with init.lua withouth ./
local m = import('fixture_two/two_dot_one')
-- will import relative to parent directory
local m = import('../fixture_three')
-- will import relative to parent 2 up directories
local m = import('../../import')
To setup project for development, run the following commands.
luarocks install --deps-mode all --only-deps lua-import-0.1.0-1.rockspec
eval "$(luarocks path --bin)"
To run the tests
luarocks test
# or
busted .