Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
ci-cd/node_modules
pipeline/node_modules
**/node_modules
dist
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ Special thanks to **kizoku246** for the house in these images!
- [Nextr](https://mcpedl.com/user/nextr): Control item textures (tweaked)
- [Indyfficient](https://www.youtube.com/@Indyfficient): The idea of changing structure by hitting the armour stand
- [Prowl8413](https://www.youtube.com/@Prowl8413): General feedback during development
- [Hellothere1234627284yehnsndnsnwnd](https://github.com/Hellothere1234627284yehnsndnsnwnd): Feedback and suggestions
- [Rainbow-Editor](https://github.com/PurpleRain-PR/Rainbow-Editor) by [PurpleRain-PR](https://github.com/PurpleRain-PR): The idea of exporting a GLB from the preview
- Documentation:
- [Bedrock Wiki](https://wiki.bedrock.dev): Best resource for learning about resource/behaviour packs!
- [Minecraft Wiki](https://minecraft.wiki): Block entity list, block states and data values, and so much more!
Expand Down
2 changes: 2 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
- [Nextr](https://mcpedl.com/user/nextr): Control item textures (tweaked)
- [Indyfficient](https://www.youtube.com/@Indyfficient):通过攻击盔甲架切换结构的创意。
- [Prowl8413](https://www.youtube.com/@Prowl8413):开发过程中的反馈支持。
- [Hellothere1234627284yehnsndnsnwnd](https://github.com/Hellothere1234627284yehnsndnsnwnd): Feedback and suggestions
- [Rainbow-Editor](https://github.com/PurpleRain-PR/Rainbow-Editor) by [PurpleRain-PR](https://github.com/PurpleRain-PR): The idea of exporting a GLB from the preview
- **参考文档:**
- [基岩版 Wiki](https://wiki.bedrock.dev):学习资源包/行为包的最佳指南!
- [Minecraft Wiki](https://minecraft.wiki):方块实体列表、状态数据等全面资料。
Expand Down
2 changes: 1 addition & 1 deletion globalPatches.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ declare global {
}
interface String {
startsWith<S extends string, This extends string>(this: This, searchString: S, position?: 0): This extends `${S}${string}`? true : false;
startsWith<S extends string, This extends string>(this: This, searchString: S): This extends `${string}${S}`? true : false;
endsWith<S extends string, This extends string>(this: This, searchString: S): This extends `${string}${S}`? true : false;
Comment thread
SuperLlama88888 marked this conversation as resolved.
}
interface JSON {
rawJSON(text: string): RawJSON;
Expand Down
90 changes: 70 additions & 20 deletions pipeline/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,76 @@ import browserslist from "browserslist";
import { transform, browserslistToTargets } from "lightningcss";
import minifyJSON from "jsonminify";

const srcDir = "src";
const distDir = "dist";

const versionParamName = "--version=";
const exportHoloPrintLibFlagName = "--export-holoprint-lib";

const buildVersion = process.argv.find(arg => arg.startsWith(versionParamName))?.slice(versionParamName.length) ?? "testing";
const exportHoloPrintLib = process.argv.includes(exportHoloPrintLibFlagName);

if(fs.existsSync("dist")) {
fs.rmSync("dist", {
recursive: true
});
}
fs.cpSync("src", "dist", {
recursive: true
});
process.chdir("dist");

const baseDir = ".";
const cssTargets = browserslistToTargets(browserslist(">= 0.1%"));
const importMapPattern = /<script type="importmap">([^]+?)<\/script>/;

await processDir(baseDir);
process.chdir(path.resolve(import.meta.dirname, "../"));
rmDir("temp");
fs.cpSync(srcDir, "temp", {
recursive: true
});
await processDir("temp");
rmDir(distDir);
fs.cpSync("temp", distDir, {
recursive: true,
filter: filename => !(path.extname(filename) == ".js" || (fs.statSync(filename).isDirectory() && fs.readdirSync(filename).every(file => path.extname(file) == ".js")))
});

let importMapJSON = fs.readFileSync("index.html", "utf-8").match(/<script type="importmap">([^]+?)<\/script>/)[1];
let importMapJSON = fs.readFileSync(`${distDir}/index.html`, "utf-8").match(importMapPattern)[1];
let externalModules = Object.keys(JSON.parse(importMapJSON)["imports"]);
Comment thread
SuperLlama88888 marked this conversation as resolved.
let { metafile } = esbuild.buildSync({
absWorkingDir: process.cwd(),
entryPoints: ["index.js"],
entryPoints: ["temp/index.js"],
bundle: true,
external: ["./entityScripts.molang.js", ...externalModules],
external: externalModules,
dropLabels: ["TS"],
minify: true,
format: "esm",
outdir: ".",
allowOverwrite: true,
outdir: distDir,
entryNames: "[name]-[hash]",
assetNames: "[name]-[hash]",
loader: {
".molang.js": "copy" // don't process these files at all, treat them as assets
},
sourcemap: true,
metafile: true
});
rmDir("temp");
console.log(esbuild.analyzeMetafileSync(metafile));
Object.keys(metafile["inputs"]).forEach(filename => {
if(!filename.endsWith("index.js")) {
fs.unlinkSync(filename); // remove bundled JS files to make build smaller
let scriptImportReplacements = [];
Object.entries(metafile["outputs"]).forEach(([output, { entryPoint }]) => {
if(entryPoint) {
scriptImportReplacements.push([entryPoint.replace("temp/", ""), output.replace(distDir + "/", "")]);
}
});
Comment thread
SuperLlama88888 marked this conversation as resolved.
fs.readdirSync(distDir).forEach(filename => {
if(path.extname(filename) == ".html") {
let filepath = path.join(distDir, filename);
let html = fs.readFileSync(filepath, "utf-8");
scriptImportReplacements.forEach(([oldName, newName]) => {
let regExp = new RegExp(`\\b${oldName.replaceAll(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\b`, "g");
html = html.replaceAll(regExp, newName);
});
if(importMapPattern.test(html)) {
html = html.replace(importMapPattern, (_, importMapJSON) => addImportReplacementsToImportMap(JSON.parse(importMapJSON)));
} else {
let importMapScript = addImportReplacementsToImportMap({});
if(html.includes("</head>")) {
html = html.replace("</head>", `${importMapScript}</head>`);
} else {
html += importMapScript;
}
}
Comment thread
SuperLlama88888 marked this conversation as resolved.
fs.writeFileSync(filepath, html);
}
});

Expand Down Expand Up @@ -156,6 +186,26 @@ function processJSON(code) {
return { code };
}

/** @param {string} dir */
function rmDir(dir) {
if(fs.existsSync(dir)) {
fs.rmSync(dir, {
recursive: true
});
}
}

/**
* @param {object} importMap
* @returns {string}
*/
function addImportReplacementsToImportMap(importMap) {
importMap["imports"] ??= {};
scriptImportReplacements.forEach(([oldName, newName]) => {
importMap["imports"][`./${oldName}`] = `./${newName}`;
});
return `<script type="importmap">${JSON.stringify(importMap)}</script>`;
}
/**
* @param {string} str
* @param {RegExp} regexp
Expand Down
Loading