Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Changelog

- support `lune` target ([#2](https://github.com/seaofvoices/luaubox/pull/2))
- add `bundle` and `roblox-plugin` targets ([#1](https://github.com/seaofvoices/luaubox/pull/1))

## 0.1.0
Expand Down
5 changes: 2 additions & 3 deletions src/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local fs = require('@lune/fs')

local currentVersion = require('./currentVersion')
local processBundle = require('./processBundle')
local processLune = require('./processLune')
local processRoblox = require('./processRoblox')
local utils = require('./utils')

Expand Down Expand Up @@ -177,9 +178,7 @@ local function runWithArgs(args: { string })
options.environment = normalizedEnvironment :: processBundle.Environment
processBundle(options)
elseif normalizedTarget == 'lune' then
error(
`the lune target is currently unimplemented!\n\nNote that luaubox can bundle using the lune environment, re-run with the 'luaubox --target bundle --env lune'`
)
processLune(options)
end
end

Expand Down
29 changes: 26 additions & 3 deletions src/prepareDarkluaConfig.luau
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Options = {
compact: boolean,
noTypes: boolean,
baseDarkluaConfigPath: AsPath?,
aliases: { [string]: AsPath }?,
}

local function findRule(rules: { any }, name: string): number?
Expand All @@ -35,6 +36,7 @@ local function prepareDarkluaConfig(configPath: AsPath, options: Options)
local compact = options.compact
local noTypes = options.noTypes
local baseDarkluaConfigPath = options.baseDarkluaConfigPath
local aliases = options.aliases

if environment == nil and not bundle then
error('environment is required when bundling is not enabled')
Expand Down Expand Up @@ -117,6 +119,15 @@ local function prepareDarkluaConfig(configPath: AsPath, options: Options)
end
end

local sources = nil

if aliases then
sources = {}
for aliasName, aliasPath in aliases do
sources[aliasName] = tostring(aliasPath)
end
end

local convertRequireIndex = findRule(darkluaConfig.rules, 'convert_require')
if bundle then
if convertRequireIndex then
Expand All @@ -127,7 +138,11 @@ local function prepareDarkluaConfig(configPath: AsPath, options: Options)
local requireRule = if environment == 'roblox'
then {
rule = 'convert_require',
current = 'path',
current = {
name = 'path',
sources = sources,
use_luau_configuration = aliases == nil,
},
target = {
name = 'roblox',
rojo_sourcemap = 'sourcemap.json',
Expand All @@ -136,8 +151,16 @@ local function prepareDarkluaConfig(configPath: AsPath, options: Options)
}
elseif environment == 'lune' then {
rule = 'convert_require',
current = 'path',
target = 'luau',
current = {
name = 'path',
sources = sources,
use_luau_configuration = aliases == nil,
},
target = {
name = 'luau',
sources = sources,
use_luau_configuration = aliases == nil,
},
}
else error(`unexpected Lua environment '{environment}'`)

Expand Down
117 changes: 117 additions & 0 deletions src/processLune.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
local PathModule = require('@pkg/luau-path')

local BaseOptions = require('./BaseOptions')
local awaitAll = require('./awaitAll')
local displayCreatedMessage = require('./displayCreatedMessage')
local getNameWithoutScope = require('./getNameWithoutScope')
local prepareDarkluaConfig = require('./prepareDarkluaConfig')
local processSources = require('./processSources')
local utils = require('./utils')

type AsPath = PathModule.AsPath
type Path = PathModule.Path

local Path = PathModule.Path

local DEFAULT_SOURCE_PATH = 'src'

export type Options = BaseOptions.BaseOptions & {
testMode: boolean?,
}

local function processLune(options: Options)
local tempPath = Path.from('temp')
local buildPath = Path.from('build')
local luneBuildPath = buildPath:join('lune')

utils.writeDir(luneBuildPath)
utils.writeDir(tempPath)

local projectName = options.projectName
local startPackageJsonPath = Path.from(options.packageJsonPath)
local parallelMode = options.parallelMode == true
local showCommands = options.showCommands == true
local compact = options.compact == true
local noTypes = options.noTypes == true
local testMode = options.testMode == true
local devMode = options.devMode == true
local tempDarkluaConfigName = options.tempDarkluaConfigName or 'temp-darklua-config.json'

local nameWithoutScope = getNameWithoutScope(projectName)

local isNodeModuleProject = startPackageJsonPath:startsWith('node_modules')

local tempDarkluaConfigPath = tempPath:join(tempDarkluaConfigName)

local modulesFolderName = `{nameWithoutScope}-dependencies`

local aliases: { [string]: AsPath } = {
['@pkg'] = `{modulesFolderName}/.luau-aliases`,
}

prepareDarkluaConfig(tempDarkluaConfigPath, {
environment = 'lune',
bundle = false,
testMode = testMode,
devMode = devMode,
compact = compact,
noTypes = noTypes,
baseDarkluaConfigPath = options.baseDarkluaConfigPath,
aliases = aliases,
})

local tempNodeModulesPath = tempPath:join(modulesFolderName)

utils.copy('node_modules', tempNodeModulesPath)

if not isNodeModuleProject then
awaitAll(function()
utils.copy(DEFAULT_SOURCE_PATH, tempPath:join(DEFAULT_SOURCE_PATH))
end, function()
utils.copy(startPackageJsonPath, tempPath:join(startPackageJsonPath))
end)

startPackageJsonPath = tempPath:join('package.json')
end

processSources({
startPath = startPackageJsonPath,
testMode = testMode,
parallelMode = parallelMode,
showCommands = showCommands,
tempPath = tempPath,
tempDarkluaConfigName = tempDarkluaConfigPath,
nodeModulesPath = tempNodeModulesPath,
workingDir = tempPath,
})

local modulesOutput = luneBuildPath:join(modulesFolderName)

local packageOutput = luneBuildPath:join(nameWithoutScope)

awaitAll(function()
utils.move(tempPath:join(tempNodeModulesPath), modulesOutput)
end, function()
utils.move(tempPath:join('temp'):join(DEFAULT_SOURCE_PATH), packageOutput)
end)

awaitAll(function()
utils.writeJsonFile(modulesOutput:join('.luaurc'), {
aliases = {
pkg = './.luau-aliases',
},
})
end, function()
utils.writeJsonFile(packageOutput:join('.luaurc'), {
aliases = {
pkg = `../{modulesFolderName}/.luau-aliases`,
},
})
end)

displayCreatedMessage(packageOutput)

utils.removeDirAll(tempPath)
end

return processLune
18 changes: 11 additions & 7 deletions src/processSources.luau
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export type Options = {
showCommands: boolean,
tempDarkluaConfigName: string,
tempPath: Path,
nodeModulesPath: AsPath?,
workingDir: AsPath?,
}

local SKIP_PACKAGES = {
Expand All @@ -28,11 +30,14 @@ local SKIP_PACKAGES = {

local DIM = stdio.style('dim')
local RESET = stdio.style('reset')
local GREEN = stdio.color('green')

local function processSources(options: Options)
local testMode = options.testMode
local tempPath = options.tempPath
local tempDarkluaConfigName = options.tempDarkluaConfigName
local nodeModulesPath = Path.from(options.nodeModulesPath or 'node_modules')
local workingDir = Path.from(options.workingDir or '')

local darklua = createDarkluaRunner(options.showCommands)

Expand Down Expand Up @@ -62,24 +67,23 @@ local function processSources(options: Options)
end

awaitAll(function()
if root:equal(Path.from('')) then
if root:equal(workingDir) then
darklua.process(
tempDarkluaConfigName,
DEFAULT_SOURCE_PATH,
tempPath:join(DEFAULT_SOURCE_PATH)
workingDir:join(DEFAULT_SOURCE_PATH),
workingDir:join(tempPath:join(DEFAULT_SOURCE_PATH))
)
else
darklua.process(tempDarkluaConfigName, root, tempPath:join(root))
end
end, function()
local aliasFile = `node_modules/.luau-aliases/{packageJson.name}.luau`
local aliasFile = nodeModulesPath:join(`.luau-aliases/{packageJson.name}.luau`)
if utils.isFile(aliasFile) then
darklua.process(tempDarkluaConfigName, aliasFile, tempPath:join(aliasFile))
end
end)

local green = stdio.color('green')
print(`{DIM}processed{green} {packageJson.name}{RESET}`)
print(`{DIM}processed{GREEN} {packageJson.name}{RESET}`)

local dependencies = {}

Expand All @@ -98,7 +102,7 @@ local function processSources(options: Options)
local jobs = {}

for _, dependencyName in dependencies do
local dependencyJsonPath = Path.from(`node_modules/{dependencyName}/package.json`)
local dependencyJsonPath = nodeModulesPath:join(`{dependencyName}/package.json`)
if options.parallelMode then
table.insert(jobs, function()
processPackage(dependencyJsonPath)
Expand Down
5 changes: 5 additions & 0 deletions src/utils.luau
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ local function move(from: AsPath, to: AsPath)
fs.move(tostring(from), tostring(to), true)
end

local function copy(from: AsPath, to: AsPath)
fs.copy(tostring(from), tostring(to), true)
end

local function removeFile(path: AsPath)
fs.removeFile(tostring(path))
end
Expand Down Expand Up @@ -62,6 +66,7 @@ return {
readJsonFile = readJsonFile,
isFile = isFile,
move = move,
copy = copy,
removeFile = removeFile,
removeDirAll = removeDirAll,
}