@locale-labs/miniapp-builder 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -0
- package/dist/index.js +195 -0
- package/package.json +26 -0
package/README.md
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Locale Mini-App Builder
|
|
2
|
+
|
|
3
|
+
Tooling for building Localé Mini-Apps.
|
|
4
|
+
|
|
5
|
+
This package provides the CLI `locale-miniapp-builder` which handles:
|
|
6
|
+
- Bundling TypeScript with esbuild
|
|
7
|
+
- Processing HTML templates
|
|
8
|
+
- Injecting the Localé SDK
|
|
9
|
+
- Cleaning up development scripts (Tailwind, Alpine CDN) for production
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
bun add -d @locale-labs/miniapp-builder
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
In your `package.json`:
|
|
18
|
+
|
|
19
|
+
```json
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "bun run build:html && bun run build:ts && bun run build:css && locale-miniapp-builder"
|
|
22
|
+
}
|
|
23
|
+
```
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from))
|
|
16
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
17
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return to;
|
|
20
|
+
};
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
29
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
30
|
+
|
|
31
|
+
// src/index.ts
|
|
32
|
+
var src_exports = {};
|
|
33
|
+
__export(src_exports, {
|
|
34
|
+
buildSingleFile: () => buildSingleFile
|
|
35
|
+
});
|
|
36
|
+
module.exports = __toCommonJS(src_exports);
|
|
37
|
+
var fs = __toESM(require("fs"));
|
|
38
|
+
var path = __toESM(require("path"));
|
|
39
|
+
var CWD = process.cwd();
|
|
40
|
+
var SRC_DIR = path.join(CWD, "src");
|
|
41
|
+
var TEMP_DIR = path.join(CWD, ".temp");
|
|
42
|
+
var BUILD_DIR = path.join(CWD, "build");
|
|
43
|
+
var DEBUG_ENABLED = true;
|
|
44
|
+
function log(...args) {
|
|
45
|
+
if (DEBUG_ENABLED) {
|
|
46
|
+
console.log("[Builder]", ...args);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function logInfo(...args) {
|
|
50
|
+
console.log(...args);
|
|
51
|
+
}
|
|
52
|
+
function logWarn(...args) {
|
|
53
|
+
console.warn("\u26A0\uFE0F ", ...args);
|
|
54
|
+
}
|
|
55
|
+
function logError(...args) {
|
|
56
|
+
console.error("\u274C ", ...args);
|
|
57
|
+
}
|
|
58
|
+
var FILES = {
|
|
59
|
+
// HTML is now generated by posthtml-include into .temp/
|
|
60
|
+
html: path.join(TEMP_DIR, "index.html"),
|
|
61
|
+
css: path.join(TEMP_DIR, "output.css"),
|
|
62
|
+
js: path.join(TEMP_DIR, "alpine.js")
|
|
63
|
+
};
|
|
64
|
+
var OUT_FILE = path.join(BUILD_DIR, "index.html");
|
|
65
|
+
function ensureBuildDir() {
|
|
66
|
+
if (!fs.existsSync(BUILD_DIR)) {
|
|
67
|
+
fs.mkdirSync(BUILD_DIR, { recursive: true });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
function findSdkPath() {
|
|
71
|
+
if (process.env.LOCALE_SDK_PATH) {
|
|
72
|
+
const envPath = path.resolve(CWD, process.env.LOCALE_SDK_PATH);
|
|
73
|
+
log(`Searching SDK in LOCALE_SDK_PATH: ${envPath}`);
|
|
74
|
+
if (fs.existsSync(envPath)) return envPath;
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
const nodeModulePath = require.resolve("@locale-labs/miniapp-sdk/dist/sdk.js", {
|
|
78
|
+
paths: [CWD, __dirname]
|
|
79
|
+
});
|
|
80
|
+
log(`Searching SDK in node_modules: ${nodeModulePath}`);
|
|
81
|
+
if (fs.existsSync(nodeModulePath)) return nodeModulePath;
|
|
82
|
+
} catch (e) {
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
const nodeModulePath = require.resolve("@locale/miniapp-sdk/dist/sdk.js", {
|
|
86
|
+
paths: [CWD, __dirname]
|
|
87
|
+
});
|
|
88
|
+
log(`Searching SDK in legacy node_modules: ${nodeModulePath}`);
|
|
89
|
+
if (fs.existsSync(nodeModulePath)) return nodeModulePath;
|
|
90
|
+
} catch (e) {
|
|
91
|
+
}
|
|
92
|
+
const localPath = path.join(SRC_DIR, "sdk.js");
|
|
93
|
+
log(`Searching SDK in local src: ${localPath}`);
|
|
94
|
+
if (fs.existsSync(localPath)) return localPath;
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
function buildSingleFile() {
|
|
98
|
+
logInfo("\u{1F3D7}\uFE0F Construyendo archivo \xFAnico HTML (Local\xE9 Builder)...");
|
|
99
|
+
log(`\u{1F4C2} CWD: ${CWD}`);
|
|
100
|
+
try {
|
|
101
|
+
ensureBuildDir();
|
|
102
|
+
if (!fs.existsSync(FILES.html)) {
|
|
103
|
+
throw new Error(
|
|
104
|
+
`No se encontr\xF3 el archivo HTML de entrada: ${FILES.html}`
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
let htmlContent = fs.readFileSync(FILES.html, "utf8");
|
|
108
|
+
let cssContent = "";
|
|
109
|
+
if (fs.existsSync(FILES.css)) {
|
|
110
|
+
cssContent = fs.readFileSync(FILES.css, "utf8");
|
|
111
|
+
} else {
|
|
112
|
+
logWarn(
|
|
113
|
+
"No se encontr\xF3 output.css, aseg\xFArate de que Tailwind se haya ejecutado."
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
let jsContent = "";
|
|
117
|
+
if (fs.existsSync(FILES.js)) {
|
|
118
|
+
jsContent = fs.readFileSync(FILES.js, "utf8");
|
|
119
|
+
} else {
|
|
120
|
+
logWarn(
|
|
121
|
+
"No se encontr\xF3 alpine.js, aseg\xFArate de que esbuild se haya ejecutado."
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
let sdkContent = "";
|
|
125
|
+
const sdkPath = findSdkPath();
|
|
126
|
+
if (sdkPath) {
|
|
127
|
+
log(`\u{1F4CD} SDK Path encontrado: ${sdkPath}`);
|
|
128
|
+
sdkContent = fs.readFileSync(sdkPath, "utf8");
|
|
129
|
+
logInfo("\u2705 SDK cargado correctamente");
|
|
130
|
+
} else {
|
|
131
|
+
logWarn(
|
|
132
|
+
"CRITICO: No se encontr\xF3 el SDK de Local\xE9. La app no funcionar\xE1 correctamente."
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
htmlContent = htmlContent.replace(
|
|
136
|
+
/<script[^>]*tailwindcss[^>]*>[\s\S]*?<\/script>/g,
|
|
137
|
+
""
|
|
138
|
+
);
|
|
139
|
+
htmlContent = htmlContent.replace(
|
|
140
|
+
/<script[^>]*alpinejs[^>]*>[\s\S]*?<\/script>/gi,
|
|
141
|
+
""
|
|
142
|
+
);
|
|
143
|
+
htmlContent = htmlContent.replace(
|
|
144
|
+
/<script\s+src\s*=\s*["']alpine\.js["']\s*>\s*<\/script>/gi,
|
|
145
|
+
""
|
|
146
|
+
);
|
|
147
|
+
htmlContent = htmlContent.replace(
|
|
148
|
+
/<script[^>]*sdk\.js[^>]*>[\s\S]*?<\/script>/g,
|
|
149
|
+
""
|
|
150
|
+
);
|
|
151
|
+
const styleTag = `<style>
|
|
152
|
+
${cssContent}
|
|
153
|
+
</style>`;
|
|
154
|
+
const headClose = "</head>";
|
|
155
|
+
const headIdx = htmlContent.indexOf(headClose);
|
|
156
|
+
if (headIdx !== -1) {
|
|
157
|
+
htmlContent = htmlContent.substring(0, headIdx) + `${styleTag}
|
|
158
|
+
${headClose}` + htmlContent.substring(headIdx + headClose.length);
|
|
159
|
+
} else {
|
|
160
|
+
htmlContent = styleTag + htmlContent;
|
|
161
|
+
}
|
|
162
|
+
let scripts = "";
|
|
163
|
+
if (sdkContent) {
|
|
164
|
+
scripts += `<script>
|
|
165
|
+
/* Local\xE9 SDK */
|
|
166
|
+
${sdkContent}
|
|
167
|
+
</script>
|
|
168
|
+
`;
|
|
169
|
+
}
|
|
170
|
+
scripts += `<script>
|
|
171
|
+
/* App Bundle */
|
|
172
|
+
${jsContent}
|
|
173
|
+
</script>`;
|
|
174
|
+
const bodyClose = "</body>";
|
|
175
|
+
const bodyIdx = htmlContent.indexOf(bodyClose);
|
|
176
|
+
if (bodyIdx !== -1) {
|
|
177
|
+
htmlContent = htmlContent.substring(0, bodyIdx) + `${scripts}
|
|
178
|
+
${bodyClose}` + htmlContent.substring(bodyIdx + bodyClose.length);
|
|
179
|
+
} else {
|
|
180
|
+
htmlContent += `${scripts}`;
|
|
181
|
+
}
|
|
182
|
+
fs.writeFileSync(OUT_FILE, htmlContent);
|
|
183
|
+
logInfo(`\u2705 Build completado exitosamente en: ${OUT_FILE}`);
|
|
184
|
+
} catch (error) {
|
|
185
|
+
logError("Error en build:", error);
|
|
186
|
+
process.exit(1);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
if (require.main === module) {
|
|
190
|
+
buildSingleFile();
|
|
191
|
+
}
|
|
192
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
193
|
+
0 && (module.exports = {
|
|
194
|
+
buildSingleFile
|
|
195
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@locale-labs/miniapp-builder",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Build tool for Localé Mini-Apps",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"locale-miniapp-builder": "./dist/index.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "esbuild src/index.ts --bundle --platform=node --target=node18 --outfile=dist/index.js --format=cjs --banner:js=\"#!/usr/bin/env node\""
|
|
14
|
+
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@locale-labs/miniapp-sdk": "^0.1.2"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/node": "^20.0.0",
|
|
20
|
+
"esbuild": "^0.23.0",
|
|
21
|
+
"typescript": "^5.0.0"
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
}
|
|
26
|
+
}
|