All files / libs/tools/plugin/src/generators/heal/healthpacks babel-frontend.ts

91.35% Statements 74/81
73.33% Branches 11/15
100% Functions 5/5
91.35% Lines 74/81

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 821x 1x 1x 1x 1x 1x 1x 1x 1x 13x 13x 1x 1x 12x 12x 1x 1x 12x 12x 1x 1x 12x 12x 12x 12x 12x 12x 12x     12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 1x 1x 13x     13x 13x 13x 1x 1x 1x 12x 12x 12x 13x 13x 6x 13x       12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x  
import { join } from 'node:path';
 
import { generateFiles, offsetFromRoot, updateJson, type ProjectConfiguration, type Tree } from '@nx/devkit';
import { type TsConfigJson } from 'type-fest';
 
const ROOT_BABEL = 'tools/frontend/.babelrc.react.cts';
const ROOT_JEST_SETUP_AFTER_ENV = 'tools/frontend/jest-setupAfterEnv.ts';
 
const removeBabelRc = (tree: Tree, projectConfiguration: ProjectConfiguration) => {
  tree.delete(`${projectConfiguration.root}/.babelrc`);
};
 
const removeObsoleteJestSetupAfterEnv = (tree: Tree, projectConfiguration: ProjectConfiguration) => {
  tree.delete(`${projectConfiguration.root}/test/setupAfterEnv.ts`);
};
 
const removeSwcRc = (tree: Tree, projectConfiguration: ProjectConfiguration) => {
  tree.delete(`${projectConfiguration.root}/.swcrc`);
};
 
const addBabelRcCtsAndRootJestSetupAfterEnvToTsConfigSpec = (
  tree: Tree,
  projectConfiguration: ProjectConfiguration,
) => {
  const offset = offsetFromRoot(projectConfiguration.root);
  const pathToRootSetupAfterEnv = join(offset, ROOT_JEST_SETUP_AFTER_ENV);
 
  if (!tree.exists(`${projectConfiguration.root}/tsconfig.spec.json`)) {
    return;
  }
 
  updateJson(tree, `${projectConfiguration.root}/tsconfig.spec.json`, (json: TsConfigJson) => {
    const include = new Set(json.include ?? []);
    include.add(pathToRootSetupAfterEnv);
    include.add('.babelrc.cts');
 
    return {
      ...json,
      include: Array.from(include),
    };
  });
};
 
export const useRootBabelConfig = (tree: Tree, projectConfiguration: ProjectConfiguration) => {
  if (projectConfiguration.projectType !== 'library') {
    return;
  }
 
  // Don't keep .babelrc for backend libraries
  if (projectConfiguration.tags?.includes('layer:backend')) {
    removeBabelRc(tree, projectConfiguration);
    return;
  }
 
  // Use root babel config for frontend & common libraries
  if (
    !projectConfiguration.tags?.includes('layer:frontend') &&
    !projectConfiguration.tags?.includes('layer:frontend-mobile') &&
    !projectConfiguration.tags?.includes('layer:shared')
  ) {
    // Ignore other types of libs
    return;
  }
 
  // Remove obsolete files
  removeBabelRc(tree, projectConfiguration);
  removeObsoleteJestSetupAfterEnv(tree, projectConfiguration);
  removeSwcRc(tree, projectConfiguration);
 
  // Update tsconfig.spec.json to add the root setupAfterEnv.ts
  addBabelRcCtsAndRootJestSetupAfterEnvToTsConfigSpec(tree, projectConfiguration);
 
  // Generate .babelrc.ts & jest.config.cts
  generateFiles(tree, join(__dirname, '..', 'files', 'babel-frontend'), projectConfiguration.root, {
    projectName: projectConfiguration.name,
    projectRoot: projectConfiguration.root,
    offsetFromRoot: offsetFromRoot(projectConfiguration.root),
    ROOT_BABEL,
    ROOT_JEST_SETUP_AFTER_ENV,
  });
};