#Tutorial on bundling scripts

1 messages · Page 1 of 1 (latest)

winter moth
#

The preferred way of using external npm packages in your pack’s code. Mojang and myself prefer esbuild for starters, but webpack is not a bad choice too.

Esbuild - https://jaylydev.github.io/posts/bundle-minecraft-scripts-esbuild/

Webpack - https://jaylydev.github.io/posts/scripts-bundle-minecraft/

unreal tangle
#

Regolith also has this function which is easily configurable through the gametests filter

knotty sequoia
#

I might also include a tutorial for bundling with typescript for all of these bundlers. I cant remember how I used to use typescript with the ones mentioned above, but I personally use rollup now with the following configuration:

// ./rollup.config.mjs

import { nodeResolve } from "@rollup/plugin-node-resolve";
import typescript from "@rollup/plugin-typescript";

export default {
  input: "src/main.ts",
  output: {
    file: "scripts/main.js",
    format: "es",
  },
  external: [
    "@minecraft/debug-utilities",
    "@minecraft/server",
    "@minecraft/server-admin",
    "@minecraft/server-common",
    "@minecraft/server-editor",
    "@minecraft/server-editor-bindings",
    "@minecraft/server-gametest",
    "@minecraft/server-net",
    "@minecraft/server-ui",
  ],
  plugins: [nodeResolve(), typescript()],
};

required dependencies:

npm i -D rimraf tslib typescript rollup @rollup/plugin-node-resolve @rollup/plugin-typescript

scripts:

  "scripts": {
    "build": "rimraf scripts && rollup --config rollup.config.mjs",
    "build-watch": "rimraf scripts && rollup --config rollup.config.mjs --watch"
  }
knotty sequoia
#

actually i take this back, it used to work im pretty sure but it seems to have broken with the @minecraft/math module and potentially others 😭

knotty sequoia
#

now using esbuild with the following build file:

// ./esbuild.mjs
import { build, context } from "esbuild";
import { rimrafSync } from "rimraf";

const buildOptions = {
  entryPoints: ["src/main.ts"],
  outfile: "scripts/main.js",
  bundle: true,
  minify: true,
  sourcemap: true,
  format: "esm",
  external: [
    "@minecraft/server",
    "@minecraft/server-ui",
    "@minecraft/server-admin",
    "@minecraft/server-gametest",
    "@minecraft/server-net",
    "@minecraft/server-common",
    "@minecraft/server-editor",
    "@minecraft/debug-utilities",
  ],
};

const args = process.argv.slice(2);

rimrafSync("scripts");

if (args[0] === "--watch") {
  context({
    ...buildOptions,
    logLevel: "info",
  }).then((ctx) => {
    ctx.watch();
  });
} else {
  build(buildOptions).catch((error) => {
    console.error(error);
  });
}

dependencies:

npm i -D rimraf typescript esbuild

scripts:

  "scripts": {
    "build": "node esbuild.mjs",
    "build-watch": "node esbuild.mjs --watch"
  },
#

a little less elegant, but it works with mc libs now at least

#

esbuild is a lot faster than rollup actually anyway

grim niche
knotty sequoia
#

i originally had rimraf cos I used to do that step in that cli before i started using build files e.g. rimraf scripts && esbuild .... no real reason why i kept it. im using the build method using "just" described in the mojang ts example now anyway

#

probably gonna switch to regolith some time later too since it looks quite appealing

knotty sequoia
hasty plinth
#

Anyone who is to lazy setting all this up buy yourself:
I have simple command line tool which will create a scripting enviroment based on your choices. Just run npx create-mca to get started

winter moth
silent lance
#

uhm what is bundling?

half slate
#

multiple scripts to one script

knotty sequoia
#

useful cos u cant import libraries, so u can bundle them with ur code instead

forest viper
#

With this I still can import other script files? the last time I use ts I have some troubles with import other ts files and compile then to js

knotty sequoia
#

yes. ur tsconfig or bundler config was probably incorrect

past timber
#

So u can bundle idk , fs reader ?

#

Or any npm module ?

half slate
#

ofc not

hasty plinth
#

stuff like fs has bindings to binaries which only node has the ability to create a translation layer for