# @servicenow/isomorphic-rollup

Rollup plugins and build tools for ServiceNow. The core plugin set is isomorphic — it runs in both Node.js and the browser, enabling in-platform bundling without a local toolchain. The package also includes Vite plugins for local development and production builds targeting ServiceNow instances.

## Install

```sh
npm install @servicenow/isomorphic-rollup
```

## Usage

### Vite

```js
import { build } from 'vite'
import { servicenowVitePlugins } from '@servicenow/isomorphic-rollup/vite'

const plugins = await servicenowVitePlugins({
  scope: 'x_myapp',
  rootDir: './src/client',
  registerExplicitId,
})

await build({ plugins })
```

### Rollup

```js
import { servicenowFrontEndPlugins } from '@servicenow/isomorphic-rollup'

const plugins = await servicenowFrontEndPlugins({
  scope: 'x_myapp',
  rootDir: './src/client',
  registerExplicitId,
})

export default { input: 'src/index.ts', plugins }
```

### `registerExplicitId`

Both the Rollup and Vite plugin sets accept a `registerExplicitId` function. This is provided by the ServiceNow build toolchain (`now-sdk`) and is used to register output artifacts (CSS theme assets, HTML pages, etc.) with deterministic `sys_id` values derived from the file path. The function signature is:

```ts
type RegisterExplicitIdFunction = (
  table: string,
  key: string | number,
  guidOverride?: string,
) => string
```

It is required during production builds and omitted during development.

---

## Exports

### `@servicenow/isomorphic-rollup`

Core Rollup plugins and utilities. All plugins in this entry point are isomorphic and work in both Node.js and browser environments.

#### Plugins

| Plugin | Description |
|---|---|
| `nodeResolve` | Bare specifier resolution |
| `commonjs` | CJS to ESM conversion |
| `json` | JSON file imports |
| `swc` | TypeScript and JSX compilation via SWC |
| `minify` | Code minification via SWC |
| `replace` / `define` | Build-time value substitution |
| `sass` | CSS and Sass/SCSS compilation |
| `url` / `cssUrl` | Asset URL handling |
| `seismic` | ServiceNow component instrumentation |
| `uiPage` | HTML entry point processing for UI pages |
| `uiPageSourceManifest` | Source manifest generation for in-instance editing |
| `header` | Prepends a string header to output chunks |
| `sourcemapUrl` | Rewrites sourcemap URLs in output |
| `jsxSource` | Embeds original JSX source for in-instance editing |
| `litSource` | Embeds original Lit source for in-instance editing |
| `collectSnTranslate` | Extracts `sn-translate` translation strings |
| `polyfill` | Injects platform polyfills |
| `rollupPluginSbom` | Software bill of materials generation |

#### Composed plugin sets

| Export | Description |
|---|---|
| `servicenowFrontEndPlugins` | Pre-composed plugin set for ServiceNow front-end builds (see options below) |

#### Utilities

| Export | Description |
|---|---|
| `rollup` / `watch` | Isomorphic Rollup wrappers (`watch` is Node.js only) |
| `resolver` | Module resolver for ServiceNow external specifiers |
| `glob` | Isomorphic file globbing |
| `inputOptions` / `outputOptions` | Rollup option transform helpers |
| `createUpdateSrcPath` | Generates a `src` path rewriter for HTML script tags |
| `webFsLoader` | Browser-side filesystem loader |
| `RollupFsAdapter` | Adapter for custom filesystem implementations |

#### `servicenowFrontEndPlugins` options

| Option | Type | Default | Description |
|---|---|---|---|
| `scope` | `string` | — | App scope (e.g. `x_myapp`) |
| `rootDir` | `string` | — | Path to the client source directory |
| `projectRootDir` | `string` | — | Absolute project root, required when `editableSourceCodeOnInstance` is true |
| `registerExplicitId` | `RegisterExplicitIdFunction` | — | Registers output files with deterministic sys_ids (required for production builds) |
| `dev` | `boolean` | `false` | Enables dev mode (watch, proxy, live reload) |
| `enableCacheBusting` | `boolean` | `true` | Appends cache-busting query params to script URLs |
| `editableSourceCodeOnInstance` | `boolean \| object` | `false` | Embeds source code for in-instance editing |
| `watchPaths` | `string[]` | `[]` | Additional paths to watch in dev mode |
| `proxyPaths` | `string[]` | See source | URL paths to proxy to the ServiceNow instance |
| `credential` | `Credential` | — | Instance authentication for dev proxy |
| `httpProxy` | `string` | — | HTTP proxy for outbound requests |
| `omitDevUxGlobals` | `boolean` | `false` | Skips UX globals injection in dev mode |

---

### `@servicenow/isomorphic-rollup/vite`

Vite plugins for building ServiceNow UI pages with CSS asset registration, image scoping, HTML generation, and dev server proxy support.

#### Plugins

| Export | Description |
|---|---|
| `servicenowVitePlugins` | Composed plugin set (see options below) |
| `servicenowCssAssetPlugin` | Rewrites CSS URLs to ServiceNow theme asset paths (`/uxta/<sys_id>.assetx`) via `renderBuiltUrl`, patches Vite's preload helper to recognize `.assetx` as CSS |
| `servicenowHtmlPlugin` | Processes HTML entry points, injects CSS `<link>` tags for entry chunk stylesheets, handles dev server UX globals transforms |
| `servicenowImageAssetPlugin` | Scopes image asset URLs with the app prefix (e.g. `/x_myapp/assets/logo.svg`) via `renderBuiltUrl` |

#### Utilities

| Export | Description |
|---|---|
| `createViteProxy` | Generates a Vite proxy config for forwarding API requests to a ServiceNow instance |
| `DEFAULT_PROXY_PATHS` | Default proxy path list (`/api`, `/amb`, `/uxasset/externals`, etc.) |

#### Types

| Export | Description |
|---|---|
| `ServicenowVitePluginOptions` | Options for `servicenowVitePlugins` |
| `RegisterExplicitIdFunction` | Re-exported from core |
| `Credential` | Re-exported from core |

#### `servicenowVitePlugins` options

| Option | Type | Default | Description |
|---|---|---|---|
| `scope` | `string` | — | App scope (e.g. `x_myapp`) |
| `rootDir` | `string` | — | Path to the client source directory |
| `registerExplicitId` | `RegisterExplicitIdFunction` | — | Registers output files with deterministic sys_ids (required for production builds) |
| `enableCacheBusting` | `boolean` | `false` | Appends cache-busting query params to script URLs |
| `credential` | `Credential` | — | Instance authentication (used by `createViteProxy`) |

---

### `@servicenow/isomorphic-rollup/resolver`

Standalone, isomorphic module resolver for mapping bare specifiers to ServiceNow external paths. Runs in both Node.js and browser environments.

## License

MIT
