---
title: "Using the Slice Compiler"
url: https://docs.zeroc.com/ice/3.8/slice/using-the-slice-compiler
language: js
docs_index: https://docs.zeroc.com/llms.txt
---

> For an index of every page, see [llms.txt](https://docs.zeroc.com/llms.txt).

# Using the Slice Compiler

Programming language: JavaScript. Other languages: [C++](https://docs.zeroc.com/ice/3.8/slice/using-the-slice-compiler/cpp.md), [C#](https://docs.zeroc.com/ice/3.8/slice/using-the-slice-compiler/csharp.md), [Java](https://docs.zeroc.com/ice/3.8/slice/using-the-slice-compiler/java.md), [MATLAB](https://docs.zeroc.com/ice/3.8/slice/using-the-slice-compiler/matlab.md), [PHP](https://docs.zeroc.com/ice/3.8/slice/using-the-slice-compiler/php.md), [Python](https://docs.zeroc.com/ice/3.8/slice/using-the-slice-compiler/python.md), [Ruby](https://docs.zeroc.com/ice/3.8/slice/using-the-slice-compiler/ruby.md), [Swift](https://docs.zeroc.com/ice/3.8/slice/using-the-slice-compiler/swift.md).

## Common Options

Ice provides a Slice compiler for each language mapping. The compilers share a similar command-line syntax:

```shell
slice2<name> [options] file...
```

Regardless of which compiler you use, a number of command-line options are common to the compilers for any language mapping:

- `-h, --help` Displays a help message.
- `-v, --version` Displays the compiler version.
- `-DNAME` Defines the preprocessor symbol `NAME` with the value `1`.
- `-DNAME=DEF` Defines the preprocessor symbol `NAME` with the value `DEF`.
- `-UNAME` Undefines the preprocessor symbol `NAME`.
- `-IDIR` Add the directory `DIR` to the search path for `#include` directives.
- `--output-dir` `DIR` Place the generated files into directory `DIR`, which must already exist.
- `-d, --debug` Print debug information showing the operation of the Slice parser.

The following options print dependency information:

- `--depend` Print dependency information in Makefile format to standard output by default, or to the file specified by the `--depend-file` option.
- `--depend-xml` Print dependency information in XML format to standard output by default, or to the file specified by the `--depend-file` option.
- `--depend-json` Print dependency information in JSON format to standard output by default, or to the file specified by the `--depend-file` option.
- `--depend-file FILE` Directs dependency information to the specified file. The output format depends on whether `--depend`, `--depend-xml`, or `--depend-json` is specified.

With these options, the compiler prints the complete list of Slice files that the input Slice files depend on, through direct or indirect inclusion, and generates no code.

The Slice compilers permit you to compile more than a single source file, so you can compile several Slice definitions at once, for example:

```shell
slice2cpp -I. file1.ice file2.ice file3.ice
```

Each compiler predefines macros that identify the compiler and the Ice version; see [Conditional Compilation](https://docs.zeroc.com/ice/3.8/slice/slice-source-files.md#conditional-compilation).

## The Slice Compiler for JavaScript

The Slice-to-JavaScript compiler (`slice2js`) offers the following command-line options in addition to the standard options:

- `--stdout` Print generated code to standard output.
- `--typescript` Generate a TypeScript declaration file (`.d.ts`) next to each generated JavaScript file.

## Running slice2js

The `@zeroc/ice` package includes the `slice2js` compiler for Linux (x64 and arm64), macOS (arm64), and Windows (x64). Run it with `npx`:

```shell
npx slice2js --output-dir src/generated slice/Greeter.ice
```

The `slice2js` command adds the Slice files of Ice, which the package includes, to the include search path, so your Slice files can include them, as in `#include <Ice/Identity.ice>`.

## Compiling Slice Files During the Build

The optional `@zeroc/slice2js` package provides a plug-in that compiles your Slice files at the start of each build of a Vite, Rollup, webpack, or esbuild project. Install it as a development dependency:

```shell
npm install --save-dev @zeroc/slice2js
```

Import the plug-in for your bundler from its entry point:

| Bundler | Entry point |
| --- | --- |
| Vite | `@zeroc/slice2js/unplugin/vite` |
| Rollup | `@zeroc/slice2js/unplugin/rollup` |
| webpack | `@zeroc/slice2js/unplugin/webpack` |
| esbuild | `@zeroc/slice2js/unplugin/esbuild` |

Then add the plug-in to the configuration of your bundler. For example, with Vite:

```js title="vite.config.js"
import slice2js from "@zeroc/slice2js/unplugin/vite";

export default {
    plugins: [
        slice2js({
            inputs: ["slice/*.ice"],
            outputDir: "src/generated",
            include: ["slice"],
            args: ["--typescript"],
        }),
    ],
};
```

The plug-in accepts the following options:

| Option | Description |
| --- | --- |
| `inputs` | The Slice files to compile, as file names or glob patterns. Required. |
| `outputDir` | The directory for the generated files. The plug-in creates it if it doesn't exist. Required. |
| `include` | Additional directories for the include search path of `slice2js`. |
| `args` | Additional `slice2js` command-line options, such as `--typescript`. |
| `cwd` | The directory against which the relative paths in `inputs`, `outputDir`, and `include` resolve. Defaults to the current directory. |
| `toolsPath` | The directory of a `slice2js` executable that replaces the one in the package. |
| `slicePath` | The directory of Slice files that replaces the Slice files of Ice in the include search path. |

To compile Slice files from your own build script, call `runSlice2js` from `@zeroc/slice2js/unplugin`, which takes the same options as the plug-in, or `compile` from `@zeroc/slice2js`, which takes the `slice2js` command-line arguments and returns the exit code of the compiler:

```js
import { runSlice2js } from "@zeroc/slice2js/unplugin";

await runSlice2js({ inputs: ["slice/*.ice"], outputDir: "src/generated", args: ["--typescript"] });
```

## See Also

- [Slice Compilation](https://docs.zeroc.com/ice/3.8/slice/slice-compilation.md)
