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 133 | import { globSync } from 'glob';
import path from 'node:path';
import fs from 'fs-extra';
import { fileURLToPath } from 'node: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'));
void lbcPackageJson;
// find component modules, return array
const findModules = () => {
return globSync(path.resolve(root, 'src/**/*.js'), {
ignore: [
path.resolve(root, 'src/coreMonolith/appShell/__stories__/appShellNavigationOverflow.js'),
path.resolve(root, 'src/coreMonolith/appShell/__stories__/appShellViewport.js'),
path.resolve(root, '**/__stories__/*.stories.js'),
path.resolve(root, '**/__templates__/**/*.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, '**/dataTable/data.js'),
path.resolve(root, '**/dataTable/columns.js'),
path.resolve(root, '**/dataTable/sort.js'),
],
});
};
const findExampleModules = () => {
return globSync(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'),
path.resolve(lbcPath, '**/primitiveOverlay/__examples__/**/*.js'),
],
});
};
const entries = findModules().reduce<Record<string, string>>((obj, entry) => {
const name = path.parse(entry).name;
obj[name] = entry;
return obj;
}, {});
// Small helper function to build our data structure in findExampleModules
function addProperty(obj: Record<string, Record<string, string>>, cmp: string, name: string, value: string) {
if (!obj[cmp]) {
obj[cmp] = {};
}
obj[cmp][name] = value;
}
const exampleEntries = findExampleModules().reduce<Record<string, Record<string, string>>>((obj, entry) => {
const name = path.parse(entry).name;
const regex = /lightning\/(.*?)\/__examples__/;
const cmp = path.parse(entry).dir.match(regex)?.[1];
if (!cmp) return obj;
addProperty(obj, cmp, name, entry);
return obj;
}, {});
const imports = () => {
const importLines = Object.keys(entries)
.sort((a, b) => a.localeCompare(b))
.reduce<string[]>((arr, entry) => {
arr.push(`import ${entry.replaceAll('-', '')} from "./${entry.toLowerCase()}";`);
return arr;
}, []);
return importLines.join('\n');
};
const exampleImports = () => {
const arr: string[] = [];
for (const key of Object.keys(exampleEntries).sort((a, b) => a.localeCompare(b))) {
const childObject = exampleEntries[key];
for (const childKey of Object.keys(childObject).sort((a, b) => a.localeCompare(b))) {
arr.push(`import ${key}${childKey} from "./${key.toLowerCase()}${childKey.toLowerCase()}.js";`);
}
}
return arr.join('\n');
};
const registries = () => {
const registryLines = Object.keys(entries)
.sort((a, b) => a.localeCompare(b))
.reduce<string[]>((arr, entry) => {
const name = entry.replaceAll(/([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 registryLines.join('\n');
};
const exampleRegistries = () => {
const arr: string[] = [];
for (const key of Object.keys(exampleEntries).sort((a, b) => a.localeCompare(b))) {
const childObject = exampleEntries[key];
for (const childKey of Object.keys(childObject).sort((a, b) => a.localeCompare(b))) {
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/storybook/define.js'), content);
fs.outputFile(path.resolve(root, 'build/storybook/lightningdefine.js'), lightningContent);
|