All files / packages/sds-subsystems/scripts buildSldsLwcRegistry.js

0% Statements 0/45
0% Branches 0/2
0% Functions 0/7
0% Lines 0/45

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 132                                                                                                                                                                                                                                                                       
import * as glob from 'glob';
import path from 'path';
import fs from 'fs-extra';
import { symlink, mkdir, readdir } from 'fs/promises';
import { fileURLToPath } from 'url';
 
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const root = path.resolve(__dirname, '../');
const nodeModules = path.resolve(root, '../../node_modules');
const lbcPath = path.resolve(nodeModules, 'lightning-base-components/src/lightning');
 
const lbcPackageJson = fs.readJsonSync(path.resolve(nodeModules, 'lightning-base-components/package.json'));
 
// find component modules, return array
const findModules = () => {
  return glob.sync(path.resolve(root, 'src/**/*.js'), {
    ignore: [
      path.resolve(root, '**/__stories__/*.stories.js'),
      path.resolve(root, '**/__tests__/**/*.js'),
      path.resolve(root, '**/__mocks__/**/*.js'),
      path.resolve(root, '**/index.js'),
      path.resolve(root, '**/utils/**/*.js'),
      path.resolve(root, '**/privateThemeProvider/*.js'),
      path.resolve(root, '**/__stories__/*.stories.js'),
      path.resolve(root, '**/dataTable/data.js'),
      path.resolve(root, '**/dataTable/columns.js'),
      path.resolve(root, '**/dataTable/sort.js'),
    ],
  });
};
 
const findExampleModules = () => {
  return glob.sync(path.resolve(lbcPath, '**/__examples__/**/*.js'), {
    ignore: [
      path.resolve(lbcPath, '**/__stories__/*.stories.js'),
      path.resolve(lbcPath, '**/__tests__/**/*.js'),
      path.resolve(lbcPath, '**/__mocks__/**/*.js'),
      path.resolve(lbcPath, '**/index.js'),
      path.resolve(lbcPath, '**/utils/**/*.js'),
      path.resolve(lbcPath, '**/privateThemeProvider/*.js'),
      path.resolve(lbcPath, '**/sampleData.js'),
      path.resolve(lbcPath, '**/generateData.js'),
      path.resolve(lbcPath, '**/generateDataWithDelay.js'),
    ],
  });
};
 
const entries = findModules().reduce((obj, entry) => {
  const name = path.parse(entry).name;
  obj = Object.assign(obj, { [name]: entry });
  return obj;
}, {});
 
// Small helper function to build our data structure in findExampleModules
function addProperty(obj, cmp, name, value) {
  if (!obj[cmp]) {
    obj[cmp] = {};
  }
  obj[cmp][name] = value;
}
 
const exampleEntries = findExampleModules().reduce((obj, entry) => {
  const name = path.parse(entry).name;
  const regex = /lightning\/(.*?)\/__examples__/;
  const cmp = path.parse(entry).dir.match(regex)[1];
  addProperty(obj, cmp, name, entry);
  return obj;
}, {});
 
const imports = () => {
  const imports = Object.keys(entries)
    .sort()
    .reduce((arr, entry) => {
      arr.push(`import ${entry.replaceAll('-', '')} from "./${entry.toLowerCase()}";`);
      return arr;
    }, []);
  return imports.join('\n');
};
 
const exampleImports = () => {
  let arr = [];
  for (let key of Object.keys(exampleEntries).sort()) {
    const childObject = exampleEntries[key];
    for (let childKey of Object.keys(childObject).sort()) {
      arr.push(`import ${key}${childKey} from "./${key.toLowerCase()}${childKey.toLowerCase()}.js";`);
    }
  }
  return arr.join('\n');
};
 
const registries = () => {
  const registries = Object.keys(entries)
    .sort()
    .reduce((arr, entry) => {
      const name = entry.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
      arr.push(
        `if (customElements.get("slds-${name}") === undefined) customElements.define("slds-${name}", ${entry.replaceAll(
          '-',
          '',
        )}.CustomElementConstructor);`,
      );
      return arr;
    }, []);
  return registries.join('\n');
};
 
const exampleRegistries = () => {
  let arr = [];
  for (let key of Object.keys(exampleEntries).sort()) {
    const childObject = exampleEntries[key];
    for (let childKey of Object.keys(childObject).sort()) {
      arr.push(
        `if (customElements.get("lightning-${key.toLowerCase()}-${childKey.toLowerCase()}") === undefined) customElements.define("lightning-${key.toLowerCase()}-${childKey.toLowerCase()}", ${key}${childKey}.CustomElementConstructor);`,
      );
    }
  }
  return arr.join('\n');
};
 
const content = ['import "@lwc/synthetic-shadow";', '\n', imports(), '\n', registries()].join('');
const lightningContent = [
  'import "@lwc/synthetic-shadow";',
  '\n',
  exampleImports(),
  '\n',
  exampleRegistries(),
].join('');
 
fs.outputFile(path.resolve(root, 'build/define.js'), content);
fs.outputFile(path.resolve(root, 'build/lightningdefine.js'), lightningContent);