{"id":20780221,"url":"https://github.com/le0developer/luapack","last_synced_at":"2025-05-11T11:35:38.755Z","repository":{"id":43394460,"uuid":"351469635","full_name":"Le0Developer/luapack","owner":"Le0Developer","description":"Pack a lua file with many `require`d dependencies into a standalone lua!","archived":false,"fork":false,"pushed_at":"2022-10-29T22:12:05.000Z","size":82,"stargazers_count":5,"open_issues_count":2,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2023-03-06T13:01:47.469Z","etag":null,"topics":["lua","lua-pack"],"latest_commit_sha":null,"homepage":"","language":"Lua","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Le0Developer.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-03-25T14:39:55.000Z","updated_at":"2023-03-04T02:15:38.000Z","dependencies_parsed_at":"2023-01-20T03:20:16.675Z","dependency_job_id":null,"html_url":"https://github.com/Le0Developer/luapack","commit_stats":null,"previous_names":[],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Le0Developer%2Fluapack","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Le0Developer%2Fluapack/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Le0Developer%2Fluapack/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Le0Developer%2Fluapack/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Le0Developer","download_url":"https://codeload.github.com/Le0Developer/luapack/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225043499,"owners_count":17411997,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["lua","lua-pack"],"created_at":"2024-11-17T13:32:55.994Z","updated_at":"2024-11-17T13:32:56.061Z","avatar_url":"https://github.com/Le0Developer.png","language":"Lua","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# luapack\n\nPack a lua file with many `require`d dependencies into a standalone lua!\n\n\n## Installation\n\nHead to [releases](https://github.com/Le0Developer/luapack/releases) and get the latest `luapack.lua`.\n\n### Installation from source\n\nInstall [yuescript](http://yuescript.org): `luarocks install yuescript`\n\nAnd then run `yue -e luapack.yue luapack.yue`.\n\nYou can use `luapack.packed.lua` afterwards.\n\n## Usage\n\nYou can use luapack directly in the CLI:\n\n`lua luapack.lua test.lua` generates `test.packed.lua`\n\nOr by using the API:\n\n```lua\nlocal luapack = require(\"luapack\")\n\nlocal packer = luapack.Packer()\n-- or with options:\n-- local packer = luapack.Packer({minify = false})\n\npacker:pack(\"test.lua\")\nprint(packer:export())\n```\n\nMake a new packer instance for each file you want to pack!\n\n\nOutput:\n```lua\npackage.preload[\"__luapack_entry__\"] = function()\n    print(\"hello world\") -- test.lua\nend\npackage.loaded[\"__luapack_entry__\"] = nil\ndo\n    local _result = package.preload[\"__luapack_entry__\"](...)\n    return _result\nend\n```\n\n## How does it work?\n\nLuapack extracts all `require` calls statically using a lua pattern, so if you require a dependency dynamically it won't be included.\n\n```lua\nrequire(\"test\") -- works\nrequire \"test\" -- works\nrequire'test' -- works\nrequire(\"someone's lib\") -- works\nrequire ( \"test\"      )  -- works\nrequire                     \"test\" -- works\n\nmyrequire(\"test\") -- also works!\n-- require \"test\" -- also works!\n[[require \"test\"]] -- also works!\n\nlocal name = \"name\"\nrequire(name) -- doesnt work\n\nrequire((\"test\")) -- doesnt work\nrequire([[test]]) -- doesnt work\n\nrequire(\"te\" .. \"st\") -- includes \"te\"\n```\n\nTo add a dynamic require, you can add the name explicitly in comments:\n\n```lua\nlocal name = \"test\"\nrequire(name) -- require(\"test\")\n```\n\nOr use the `packer:include` api.\n\n## Using the API\n\n- Using `packer:include`:\n\n```lua\nlocal packer = luapack.Packer()\n\npacker:include(\"dependency\")\n\npacker:pack(\"test.lua\")\nprint(packed:export())\n```\n\nOutput:\n```lua\npackage.preload[\"__luapack_entry__\"] = function(...)\n    local name = \"dependency\" -- dynamic require\n    local print_ = require(name)\n    print_(\"hello world\") -- test.lua\nend\npackage.loaded[\"__luapack_entry__\"] = nil\npackage.preload[\"dependency\"] = function(...) -- added by packer:include\n    return print\nend\npackage.loaded[\"dependency\"] = nil\ndo\n    local _result = package.preload[\"__luapack_entry__\"](...)\n    return _result\nend\n```\n\n- Using `package_polyfill` option to polyfill the require function and the package table for enviroments that don't support it:\n\n```lua\n-- package_polyfill to create package and require\nlocal packer = luapack.Packer({package_polyfill = true})\n\npacker:pack(\"test.lua\")\nprint(packed:export())\n```\n\nOr use the `-yes-package-polyfill` CLI option.\n\nOutput:\n```lua\nif not package then _G.package = { ... } end\nif not package.preload then ... end\nif not require then\n    _G.require = ...\nend\n...\n```\n\n- Using `packer:bootstrap` without entry script:\n\n```lua\nlocal packer = luapack.Packer()\n\npacker:include(\"dependecy\")\n\nlocal packed = packer:bootstrap()\nprint(packed) -- only has the package.preload sets\n```\n\nOutput:\n```lua\npackage.preload[\"dependency\"] = function(...)\n    return print\nend\npackage.loaded[\"dependency\"] = nil\n```\n\n## Full API\n\n- `luapack.__author__` string\n- `luapack.__url__` string\n- `luapack.__license__` string\n- `luapack.__version__` string\n- `luapack.Packer` class\n- `luapack.default_plugins` array\n\n### luapack.Packer\n\nAll options:\n\n- `minify = true` minifies the output\n- `package_polyfill = false` add polyfill for require and package\n- `with_header = true` adds the luapack header to the output\n- `clear_loaded = true` clears `package.loaded` cache\n- `plugins = default_plugins` plugins\n- `compat_arg = true` compatibility for arg (cmd line arguments) passing in lua5.1 and 5.2\n- `include_entry = true` whether to include `__luapack_entry__` or not (useful for standalone packages)\n- `entry_name = \"__luapack_entry__\"` name of the entry function\n- `package_preload_name = \"package.preload\"` name of the package preload table\n- `package_loaded_name = \"package.loaded\"` name of the package loaded table\n- `alias = nil` a function that allows you to rename package names (experimental)\n\nMethods:\n\n- **Packer:searchpath_compat(name)**\n\nUses `package.searchpath` (Lua 5.2+).\nIf `package.searchpath` does not exist, a janky search using `io.open` is done as fallback. (for Lua 5.1 support)\n\n- **Packer:extract_packages(source)**\n\nExtracts all `require` calls found in the source.\n\n- **Packer:include(package_name, filename=nil)**\n\nIncludes the package and its dependencies.\n\n- **Packer:pack(entry)**\n\nIncludes the file as entry.\n\n- **Packer:export()**\n\nReturns the bootstrapped lua code, with header and minification.\n\n- **Packer:bootstrap()**\n\nGenerates the script and returns it.\nThis **does not** minify it or add the luapack header.\n\n## Plugins\n\nluapack supports plugins to allow more formats than just Lua or\ndiscovery of packages in different places.\n\n### default_plugins\n\n- lua\n\nThis plugin allows discovery of lua packages that are on `package.path`.\n\n- yue\n\nThis plugin allows discovery and usage of `.yue` files. (requires `yue` to be installed)\n\n- moonscript\n\nThis plugin allows discovery and usage of `.moon` files. (requires `moonscript` to be installed)\n\n\n### Plugin API\n\nA plugin is just a table with the following fields:\n\n- `name`: string\n\nNot used but recommended.\n\n- `searchpath`(`packer`: Packer, `name`: string) -\u003e `?string` (optional)\n\nA function that searches for the package and returns its path.\n\n- `loader`(`packer`: Packer, `name`: string, `filename`: string, `content`: string) -\u003e `?string`, `?string` (optional)\n\nA function that returns the modified content of the file and a new filename (both can be falsy).\nThis is for post-processing or compilation of eg yuescript code.\n\n### Example plugin\n\n```lua\nlocal luapack = require(\"luapack\")\n\nluapack.helpers.fast_push(luapack.default_plugins, {\n    name = \"Add comment\",\n    loader = function(packer, name, filename, content)\n        if not luapack.helpers.check_file_extension(filename, \".lua\") then\n            return false -- dont do any changes if its not a lua file\n        end\n        -- add package name and filename as comment at the start\n        return \"-- \" .. name .. \" at \" .. filename .. \"\\n\" .. content\n    end\n})\n\nlocal packer = luapack.Packer()\npacker:pack(\"test.lua\")\nprint(packed:export())\n```\n\nOutput:\n```lua\npackage.preload[\"__luapack_entry__\"] = function(...)\n\t-- __luapack_entry__ at test.lua\n\tprint(\"hello world\")\nend\npackage.loaded[\"__luapack_entry__\"] = nil\ndo\n\tlocal _result = package.preload[\"__luapack_entry__\"](...)\n\treturn _result\nend\n```\n\n\n## Acknowledgements\n\nThis lua uses modified versions in `lib` directory of\n- [SquidDev's lua minifier](https://github.com/SquidDev-CC/Howl/tree/master/howl/lexer)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fle0developer%2Fluapack","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fle0developer%2Fluapack","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fle0developer%2Fluapack/lists"}