import { composeSourcemap } from "../utils/compose-sourcemap"; import { UXASSET_EXTERNAL_PREFIX } from "../utils/constants"; import { isolateExternalImports } from "../utils/isolate-external-imports"; import type { CustomPluginOptions, ResolveIdResult, Plugin as RollupPlugin, } from "rollup"; export default function plugin(): RollupPlugin { return { name: "seismic", version: "1.0.0", async resolveId( source: string, _importer?: string, options?: { attributes: Record; custom?: CustomPluginOptions; isEntry: boolean; }, ): Promise { if (source.startsWith(UXASSET_EXTERNAL_PREFIX)) { return { id: source, external: true, resolvedBy: "seismic", }; } const withAssetExternal = options?.attributes?.external === "uxasset"; if (withAssetExternal) { const resolvedPath = `${UXASSET_EXTERNAL_PREFIX}${source}/index.jsdbx`; return { id: resolvedPath, external: true, // makes sure the code output doesn't produce an `assert` like `import _ from '/uxasset/externals/lodash/index.jsdbx' assert { external: 'uxasset' };` attributes: {}, resolvedBy: "seismic", }; } return null; }, // The ServiceNow instance rewrites uxasset external import paths to // different bundles. If an import shares a line with other statements, // the rewrite shifts columns and corrupts their sourcemap entries. // Static imports are moved to the top of the chunk; dynamic imports // stay in place but are isolated on their own line. // generateBundle runs after all renderChunk hooks (including minifiers). async generateBundle(_options, bundle) { const PREFIX = UXASSET_EXTERNAL_PREFIX; for (const chunk of Object.values(bundle)) { if (chunk.type !== "chunk") continue; const result = await isolateExternalImports(chunk.code, PREFIX); if (result) { chunk.code = result.code; composeSourcemap(chunk, result.map, bundle); } } }, }; }