unfold-js v1.1.0
unfold-js
A command-line tool to quickly build and serve javascript without having to manually rebuild it every time, convenient during development.
Install
npm install -g unfold-js
Basic usage
unfold-js main.js
Opens an http server on port 8080. Upon every connection to localhost:8080
, the specified file is bundled with all its dependencies, wrapped into a generic index.html
, and served to the client. Other resources are served relative to the directory where the command was called.
Full reference
unfold-js [input-file] [port] [prebuild-command1] [prebuild-command2] [...]
port
: specifies the port on which to open the http server (default is 8080)input-file
: entry point from which to build the bundle; it must end with a.js
extensionprebuild-command
: command that will be run before the bundle is built
If unfold-js
is called without argument, it simply opens an http server on the specified port.
Prebuild commands
All command-line options that do not end with .js
and are not numbers are interpreted as pre-build commands, i.e commands that will be run before the bundle is built. This is useful in various cases, for example if other resources need to be processed before being served.
A pre-build command can be prefixed with any number of the following:
?
: silent flag; indicates that the output of the command should be discarded!
: async flag; indicates that the command should not be waited for before building the bundle
Example: assume we develop a web application, with the server being developped in Rust and the client in Javascript. The Javascript is served in the browser and communicates with the server via websockets.
The command unfold-js client.js '?killall server' 'cargo build' '!cargo run'
:
- silently kills a running instance of the server
- re-compiles the server
- start the server
at each connection, before building the bundle. These steps would be run every time a client connects to the http server. The !
in !cargo run
is necessary so the script doesn't wait for the server to exit before bundling the client.
Pre-build commands can also be specified at the top of the input file. Every line at the top of the file that begins with // pre-build:
will be interpreted as a pre-build command (take a look at examples/basic.js
for an example).
Notes
- WebAssembly files are supported (i.e it is possible to
import wasm from './path/to/module.wasm'
). - The tool produces ES6 code with sourcemap enabled (using
esbuild
). It cannot transpile to ES5.