All files / src/seismic index.ts

95% Statements 19/20
75% Branches 6/8
100% Functions 3/3
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 642x 2x 2x             2x 11x                         14x 3x           11x 11x 10x 10x               1x                   1x 1x 1x 1x 1x 1x 1x            
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<string, string>;
        custom?: CustomPluginOptions;
        isEntry: boolean;
      },
    ): Promise<ResolveIdResult> {
      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)) {
        Iif (chunk.type !== "chunk") continue;
        const result = await isolateExternalImports(chunk.code, PREFIX);
        Eif (result) {
          chunk.code = result.code;
          composeSourcemap(chunk, result.map, bundle);
        }
      }
    },
  };
}