- Include specified files or folders (+ rename them if you want to).
- Run your TypeScript compiler.
- Run your ESBuild bundler.
- Restore NPM packages.
- Replace strings in files after build.
- Download latest release.
- Place it in your project folder.
- Prepend content in it with
CONFIG
. - Run script using
node build.js
. - You are done!
CONFIG
constant is used to configure your build and keep everything in the single build.js
file.
It contains:
- Resources - strings you want to replace in your distribution.
"destination": "your folder where distribution is placed"
- Includes - files you want to include in your distribution.
- ESBuild - configuration options for ESBuild bundler.
- Fancy stuff - Flags if you want to force change some auto settings.
Example CONFIG
const CONFIG = {
resources: {
version: "1.0.0"
},
destination: "dist",
includes: ["text.txt", "assets/icon.png"]
// Or
// includes: [
// ["src/text.txt", "text.txt"],
// ["assets/icon-128.png", "icon.png"]
// ]
// Or
// includes: {
// configuration1: ["text.txt"],
// configuration2: ["icon.png"]
// }
// Or
// includes: {
// configuration1: [
// ["src/text.txt", "text.txt"]
// ],
// configuration2: [
// ["assets/icon-128.png", "icon.png"]
// ]
// }
// Maybe ESBuild?
// esbuild: {
// entry: "src/index.ts",
// outFile: "index.js"
// },
// Also some special flags
// npm: false
// typescript: false
};
// The rest of build.js
You can use resources to replace specified strings in your files:
{
resources: {
version: "1.0.0"
}
}
Will replace all $(VERSION)
with 1.0.0
in your distribution files.
As you can see, replacement format is $(YOUR_RESOURCE_NAME_UPPERCASE)
.
You can also replace $(THING)
with specified file content. Use file://
format to specify file as a content source:
{
resources: {
text: "file://text.txt"
}
}
Will replace all $(TEXT)
with text.txt
content.
If file is binary then
$(THING)
will be replaced withbase64
encoded content.
You can include files using
{
destination: "dist",
includes: ["text.txt", "assets/icon.png"]
}
Using this syntax file will be included in destnation folder with path provided to file (like assets/icon.png
will be dist/assets/icon.png
).
or
{
destination: "dist",
includes: [
["src/text.txt", "text.txt"],
["assets/icon-128.png", "icon.png"]
]
}
This will include specified files renaming them and placing in folder you specified.
or
{
destination: "dist",
includes: {
configuration1: ["text.txt"],
configuration2: ["icon.png"]
}
}
This will include files for selected build configuration.
or
{
destination: "dist",
includes: {
configuration1: [
["src/text.txt", "text.txt"]
],
configuration2: [
["assets/icon-128.png", "icon.png"]
]
}
}
This will include files for selected build configuration renaming and copying them into directory you specified.
As mentioned before, configurations are used for includes (and maybe will be used for more in the future 😜).
You can run build using configuration specifying it in args, like:
node build.js configuration1
Also to run build using configuration in release use:
node build.js configuration1 --release
You can read more about release in TypeScript section.
There are two compiling modes: Release and Debug.
- Debug mode is used by default. To build in Release mode add --release or -r flag when running script.
- Release mode compiles using tsconfig.release.json (you can use it to overwrite some settings in default tsconfig.json, like disable mappings).
e.g. to run build in Release mode, use node build.js --release
(or -r
).
Add esbuild
to your configuration:
{
esbuild: {
entry: "src/index.ts", // Path to your entry point file
outFile: "index.js" // Path to out file relative to CONFIG.destination
}
}
You can also build with esbuild without bundling by omitting outFile
:
{
esbuild: {
entry: "src/index.ts", // Path to your entry point file
}
}
NPM is being run with npm install
if there is package.json
in the root folder.
See fancy stuff to disable this.
typescript: false
- force-disable typescript.npm: false
- force-disable npm.
- Run
tsc
You need TSC (TypeScript Compiler) to be installed in your environment.