|
| 1 | +import fs from 'fs'; |
1 | 2 | import path from 'path';
|
2 | 3 |
|
| 4 | +import { getDirectGlyphImports } from './getDirectGlyphImports.mjs'; |
| 5 | +import { constructUMDGlobalName } from './constructUMDGlobalName.mjs'; |
| 6 | + |
3 | 7 | export function getUMDGlobals() {
|
4 | 8 | // Read from the current package's package.json
|
5 | 9 | // to get the package name and dependencies
|
6 |
| - const { dependencies, devDependencies, peerDependencies } = import( |
7 |
| - path.resolve(process.cwd(), 'package.json'), |
8 |
| - { with: { type: 'json' } } |
9 |
| - ); |
| 10 | + const packageJsonPath = path.resolve(process.cwd(), 'package.json'); |
| 11 | + const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8'); |
| 12 | + const { |
| 13 | + dependencies = {}, |
| 14 | + devDependencies = {}, |
| 15 | + peerDependencies = {}, |
| 16 | + } = JSON.parse(packageJsonContent); |
10 | 17 |
|
11 | 18 | const allDependencies = {
|
12 | 19 | ...dependencies,
|
13 | 20 | ...devDependencies,
|
14 | 21 | ...peerDependencies,
|
15 | 22 | };
|
16 | 23 |
|
17 |
| - const lgGlobals = Object.keys(allDependencies).reduce((acc, pkg) => { |
18 |
| - acc[pkg] = pkg; |
19 |
| - return acc; |
20 |
| - }, {}); |
| 24 | + const lgGlobals = Object.entries(allDependencies).reduce( |
| 25 | + (acc, [pkg, version]) => { |
| 26 | + // Only include packages in this monorepo |
| 27 | + if (version.includes('workspace:')) { |
| 28 | + acc[pkg] = constructUMDGlobalName(pkg); |
| 29 | + } |
| 30 | + return acc; |
| 31 | + }, |
| 32 | + {}, |
| 33 | + ); |
21 | 34 |
|
22 |
| - const iconGlobals = getDirectGlyphImports().reduce((acc, glyph) => { |
23 |
| - acc[glyph] = /[^/]+$/.exec(glyph)[0]; |
24 |
| - return acc; |
25 |
| - }, {}); |
| 35 | + const iconGlobals = getDirectGlyphImports(allDependencies).reduce( |
| 36 | + (acc, glyph) => { |
| 37 | + const iconName = /[^/]+$/.exec(glyph)[0]; // Get the part after the last slash |
| 38 | + acc[glyph] = constructUMDGlobalName('icon', iconName); |
| 39 | + return acc; |
| 40 | + }, |
| 41 | + {}, |
| 42 | + ); |
26 | 43 |
|
27 | 44 | // Mapping of packages to the `window` property they'd be
|
28 | 45 | // bound to if used in the browser without a module loader.
|
@@ -59,25 +76,5 @@ export function getUMDGlobals() {
|
59 | 76 | }
|
60 | 77 | }
|
61 | 78 |
|
62 |
| - /** |
63 |
| - * @returns An array of all glyph import paths |
64 |
| - */ |
65 |
| - function getDirectGlyphImports() { |
66 |
| - const pkgHasIconDependency = allDependencies['@leafygreen-ui/icon']; |
67 |
| - const glyphsDir = path.resolve(process.cwd(), '../icon/src/glyphs'); |
68 |
| - |
69 |
| - if (pkgHasIconDependency && fs.existsSync(glyphsDir)) { |
70 |
| - return fs |
71 |
| - .readdirSync(glyphsDir) |
72 |
| - .filter(path => /.svg/.test(path)) |
73 |
| - .map( |
74 |
| - fileName => |
75 |
| - `@leafygreen-ui/icon/dist/${path.basename(fileName, '.svg')}`, |
76 |
| - ); |
77 |
| - } |
78 |
| - |
79 |
| - return []; |
80 |
| - } |
81 |
| - |
82 | 79 | return globals;
|
83 | 80 | }
|
0 commit comments