Skip to Content
Docs@utoo/webAPI Reference

API Reference

new UtooProject(options)

Creates a new project instance.

Options

OptionTypeDescription
cwdstringRequired. The absolute path that will serve as the root of the project in the real file system (e.g., /my-app).
workerUrlstringRequired. URL of the Worker thread where the Project instance’s core logic runs. The main thread Project object is a proxy that delegates tasks to this Worker.
threadWorkerUrlstringRequired. URL of a separate Worker thread for CPU-intensive tasks like bundling and compiling.
loaderWorkerUrlstringURL of a separate Worker thread for handling webpack loaders.
wasmUrlstringURL to the WASM binary. Defaults to the bundled binary.
serviceWorkerobjectService worker configuration for preview functionality. See sub-options below.
logFilterstringFilter string for tracing logs.
loadersImportMapobjectA map for configuring Webpack loader imports. Keys are loader names, values are UMD/CommonJS module URLs or content strings.

serviceWorker sub-options:

OptionTypeDescription
urlstringRequired. The URL to the service worker script.
scopestringRequired. The URL scope that the service worker will intercept requests for.
targetDirToCwdstringPath from the target directory to the project root.

File System Methods

These methods are asynchronous and mimic the Node.js fs API.

project.writeFile(path, content, encoding?)

Writes content to a file in the real file system. If the file doesn’t exist, it will be created.

ParameterTypeDescription
pathstringThe absolute path to the file (e.g., /src/index.js).
contentstring | Uint8ArrayThe content to write.
encodingstringThe encoding (e.g., 'utf8'). Optional.

project.readFile(path, encoding?)

Reads the content of a file.

ParameterTypeDescription
pathstringThe path to the file.
encodingstringThe encoding of the file (e.g., 'utf8'). If not provided, returns Uint8Array.

Returns: Promise<string> (with encoding) or Promise<Uint8Array> (without encoding).

project.readdir(path, options?)

Reads the contents of a directory.

ParameterTypeDescription
pathstringThe path to the directory.
options.recursivebooleanIf true, reads recursively.

Returns: Promise<Dirent[]> — An array of Dirent objects with name, isDirectory(), and isFile() methods.

project.mkdir(path, options?)

Creates a new directory.

ParameterTypeDescription
pathstringThe path to the directory to be created.
options.recursivebooleanIf true, creates parent directories as needed.

project.rm(path, options?)

Removes a file or directory.

ParameterTypeDescription
pathstringThe path to the file or directory to be removed.
options.recursivebooleanIf true, performs a recursive directory removal.

project.rmdir(path, options?)

Removes a directory.

ParameterTypeDescription
pathstringThe path to the directory to be removed.
options.recursivebooleanIf true, removes recursively.

project.copyFile(src, dst)

Copies a file from source to destination.

ParameterTypeDescription
srcstringSource file path.
dststringDestination file path.

project.stat(path)

Gets file/directory stats.

ParameterTypeDescription
pathstringThe path to the file or directory.

Returns: Promise<Stats> — Stats object with size, isDirectory(), isFile(), timestamps (atime, mtime, ctime, birthtime), etc.


Dependency Management

project.deps(options?)

Resolves dependencies from the project’s package.json and generates a lock file string. This enables dependency resolution directly in the browser without requiring a pre-existing package-lock.json.

OptionTypeDescription
registrystringThe npm registry URL. Default: https://registry.npmmirror.com
concurrencynumberMaximum concurrent network requests. Default: 20

Returns: Promise<string> — A JSON string representing the resolved dependency lock file, compatible with package-lock.json format.

Examples:

app.ts
// Using default registry (npmmirror) const lockFile = await project.deps(); // Using official npm registry const lockFile = await project.deps({ registry: "https://registry.npmjs.org" }); // Using a private registry with custom concurrency const lockFile = await project.deps({ registry: "https://npm.mycompany.com", concurrency: 10 });

project.install(packageLockJsonString, options?)

Populates the node_modules directory based on a lock file string.

ParameterTypeDescription
packageLockJsonStringstringA JSON string of the dependency lock file (from deps() or a package-lock.json file).
options.maxConcurrentDownloadsnumberMaximum concurrent package downloads.
options.omitstring[]Dependency types to omit. Possible values: "dev", "optional".

Examples:

app.ts
// Basic install await project.install(lockFile); // Install with concurrency limit await project.install(lockFile, { maxConcurrentDownloads: 10 }); // Install without dev dependencies (production mode) await project.install(lockFile, { omit: ["dev"] });

Build & Development

project.build()

Triggers the build process in the thread worker. It reads the build configuration from utoopack.json in the project’s root and runs the bundler based on that configuration.

Returns: Promise<BuildOutput> — Object with an issues array containing any build warnings or errors.

app.ts
const result = await project.build(); if (result.issues.length > 0) { console.warn("Build completed with issues:", result.issues); }

project.dev(onUpdate?)

Start dev mode with file watching. Triggers incremental rebuilds on file changes.

ParameterTypeDescription
onUpdatefunctionCallback called on each rebuild with BuildOutput. Optional.
app.ts
project.dev((result) => { console.log("Rebuild complete, issues:", result.issues); });

project.hmrSubscribe(identifier, callback)

Subscribe to HMR (Hot Module Replacement) events for a specific identifier.

ParameterTypeDescription
identifierstringThe module identifier to subscribe to.
callbackfunctionCalled with update instructions when the module changes.

project.updateInfoSubscribe(aggregationMs, callback)

Subscribe to compilation lifecycle events (start/end).

ParameterTypeDescription
aggregationMsnumberAggregation interval in milliseconds.
callbackfunctionCalled with UpdateMessage on compilation events.

Preview Functionality

project.installServiceWorker()

Registers and activates the service worker defined in the constructor. This is essential for the preview functionality.


Types

BuildOutput

interface BuildOutput { issues: Issue[]; }

InstallOptions

interface InstallOptions { maxConcurrentDownloads?: number; omit?: ("dev" | "optional")[]; }

DepsOptions

interface DepsOptions { registry?: string; concurrency?: number; }

Dirent

class Dirent { name: string; isDirectory(): boolean; isFile(): boolean; }

Stats

class Stats { size: number; isDirectory(): boolean; isFile(): boolean; atime: Date; mtime: Date; ctime: Date; birthtime: Date; }
Last updated on