All files / src/vite plugins.ts

42.3% Statements 11/26
66.66% Branches 4/6
25% Functions 1/4
42.3% Lines 11/26

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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 1321x     1x                 1x 1x 1x 1x 1x 1x                                     1x                 5x   5x                                                                                                                                                                        
import path from "path";
import type { Plugin } from "vite";
import type { Plugin as RollupPlugin } from "rollup";
import {
  replace,
  swc as swcPlugin,
  minify,
  header,
  sourcemapUrl,
  outputOptions,
  collectSnTranslate,
} from "../index";
import { createUpdateSrcPath } from "../servicenow/react-plugins";
import { buildOnly } from "./build-only";
import { servicenowCssAssetPlugin } from "./css-asset";
import { servicenowHtmlPlugin } from "./html";
import { servicenowImageAssetPlugin } from "./image-asset";
import { servicenowSeismicPlugin } from "./seismic";
import type {
  RegisterExplicitIdFunction,
  Credential,
} from "../servicenow/types";
 
export interface ServicenowVitePluginOptions {
  scope: string;
  rootDir: string;
  registerExplicitId?: RegisterExplicitIdFunction;
  enableCacheBusting?: boolean;
  credential?: Credential;
  /**
   * Enable/disable SWC transpilation in favor of another transpiler (esbuild/oxc).
   * @default true
   */
  swc?: boolean;
}
 
export async function servicenowVitePlugins(
  options: ServicenowVitePluginOptions,
): Promise<Plugin[]> {
  const {
    scope,
    rootDir,
    registerExplicitId,
    enableCacheBusting = false,
    swc: swc = true,
  } = options;
 
  return [
    servicenowSeismicPlugin(),
    servicenowCssAssetPlugin({ registerExplicitId }),
    servicenowHtmlPlugin({
      rootDir,
      entryPath(entry: string, { fullPath }: { fullPath: string }) {
        const entryWithoutQueryParams = entry.replace(/\?.+/, "");
        const fullPathWithoutQueryParams = fullPath.replace(/\?.+/, "");
        const normalizedPath = path.normalize(
          entryWithoutQueryParams.replace(/^(?:\.\.\/)+/, ""),
        );
        const ext = path.extname(normalizedPath);
        const outName = normalizedPath.substring(
          0,
          normalizedPath.length - ext.length,
        );
        return {
          [`${outName}`]: fullPathWithoutQueryParams,
        };
      },
      updateSrcPath: createUpdateSrcPath({
        scope,
        // This plugin is never used for the dev server, so we don't need to preserve source paths
        preserveSrc: false,
        enableCacheBusting,
      }),
    }),
    buildOnly(
      outputOptions((rawOptions) => {
        const fileNames = "[name].jsdbx";
        if (Array.isArray(rawOptions)) {
          for (const output of rawOptions) {
            output.entryFileNames = fileNames;
            output.chunkFileNames = fileNames;
          }
        } else {
          rawOptions.entryFileNames = fileNames;
          rawOptions.chunkFileNames = fileNames;
        }
        return rawOptions;
      }),
    ),
    buildOnly(
      replace({
        "process.env.NODE_ENV": JSON.stringify("production"),
        preventAssignment: true,
      }),
    ),
    // No json() here: Vite has handled `.json` imports natively since v6
    buildOnly(collectSnTranslate()),
    ...(swc
      ? [
          buildOnly(
            swcPlugin({
              swc: {
                jsc: {
                  parser: {
                    tsx: true,
                    syntax: "typescript",
                  },
                  transform: {
                    react: {
                      runtime: "automatic",
                    },
                  },
                  experimental: {
                    keepImportAssertions: true,
                  },
                },
              },
            }),
          ),
        ]
      : []),
    buildOnly(minify({ compress: true, mangle: false })),
    buildOnly(header("/*__ESM_BODY_START__*/window.process={versions:null};")),
    buildOnly(
      sourcemapUrl({
        replaceUrl: (url: string) => `${url.replace("dbx", "")}.jsdbx`,
      }),
    ),
    buildOnly(servicenowImageAssetPlugin({ scope })),
  ];
}