-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle.js
More file actions
134 lines (103 loc) · 3.79 KB
/
bundle.js
File metadata and controls
134 lines (103 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const fs = require('fs')
const path = require('path')
const args = process.argv.slice(2)
if (args.length < 2) {
console.error('❌ Usage: node bundle.js <entry.lua> <output.lua>')
process.exit(1)
}
const ENTRY_FILE = args[0]
let outputPath = args[1]
if (fs.existsSync(outputPath) && fs.statSync(outputPath).isDirectory()) {
outputPath = path.join(outputPath, path.basename(ENTRY_FILE))
}
if (path.extname(outputPath) !== '.lua') outputPath += '.lua'
const OUTPUT_FILE = outputPath
// 상태 관리
const includedModules = new Set()
const preloads = []
const hoistedComments = []
let totalInputBytes = 0
const REQUIRE_REGEX = /require\s*\(?["']([^"']+)["']\)?/g
function resolvePath(moduleName) {
return moduleName.replace(/\./g, '/') + '.lua'
}
function formatBytes(bytes) {
if (bytes === 0) return '0 B'
const k = 1024
const sizes = ['B', 'KB', 'MB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
}
function smartMinify(content) {
const tokenRegex = /(--\[(=*)\[[\s\S]*?\]\2\])|(--.*)|(\[(=*)\[[\s\S]*?\]\5\])|("([^"\\]|\\.)*")|('([^'\\]|\\.)*')/g
return content
.replace(tokenRegex, (match, longComm, lcEq, shortComm) => {
if (longComm || shortComm) {
if (shortComm && match.startsWith('--!')) {
hoistedComments.push(match)
return ' '
}
if (longComm && /^--\[(=*)\[!/.test(match)) {
hoistedComments.push(match)
return ' '
}
return ' '
}
return match
})
.split('\n')
.map((line) => line.trim())
.filter((line) => line.length > 0)
.join('\n')
}
function processModule(moduleName) {
if (includedModules.has(moduleName)) return
const rootDir = path.dirname(path.resolve(ENTRY_FILE))
const modulePath = path.join(rootDir, resolvePath(moduleName))
if (!fs.existsSync(modulePath)) {
console.warn(`⚠️ Warning: Module '${moduleName}' not found.`)
return
}
const stats = fs.statSync(modulePath)
totalInputBytes += stats.size
includedModules.add(moduleName)
const rawContent = fs.readFileSync(modulePath, 'utf8')
let match
const regex = new RegExp(REQUIRE_REGEX)
while ((match = regex.exec(rawContent)) !== null) {
processModule(match[1])
}
const minifiedContent = smartMinify(rawContent)
preloads.push(`package.preload["${moduleName}"]=function(...)${minifiedContent} end`)
}
function bundle() {
console.log(`🚀 Bundling: ${ENTRY_FILE} -> ${OUTPUT_FILE}`)
if (!fs.existsSync(ENTRY_FILE)) {
console.error(`❌ Error: Entry file not found: ${ENTRY_FILE}`)
process.exit(1)
}
const mainStats = fs.statSync(ENTRY_FILE)
totalInputBytes += mainStats.size
const mainRaw = fs.readFileSync(ENTRY_FILE, 'utf8')
let match
const mainRegex = new RegExp(REQUIRE_REGEX)
while ((match = mainRegex.exec(mainRaw)) !== null) {
processModule(match[1])
}
const mainMinified = smartMinify(mainRaw)
const hoisted = hoistedComments.length > 0 ? hoistedComments.join('\n') + '\n' : ''
const finalOutput = hoisted + preloads.join('\n') + '\n' + mainMinified
const outputDir = path.dirname(OUTPUT_FILE)
if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true })
fs.writeFileSync(OUTPUT_FILE, finalOutput, 'utf8')
const outputBytes = fs.statSync(OUTPUT_FILE).size
const reduction = ((totalInputBytes - outputBytes) / totalInputBytes) * 100
console.log(`\n📊 Build Summary`)
console.log(`-----------------------------------`)
console.log(` Target: ${includedModules.size} modules + Entry`)
console.log(` Original: ${formatBytes(totalInputBytes)}`)
console.log(` Bundled: ${formatBytes(outputBytes)}`)
console.log(` Reduction: ${reduction.toFixed(2)}% 📉`)
console.log(`-----------------------------------`)
}
bundle()