-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add root entry point with global component registration (#2641)
This feature allows using `import '@sbb-esta/lyne-components'`, which will import all components and add them to `globalThis`, but will not provide an export. This is useful for SSR or for providing examples/reproductions and wanting to load all components.
- Loading branch information
1 parent
159e2d4
commit 3572fa9
Showing
3 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type { LibraryOptions, PluginOption, ResolvedConfig } from 'vite'; | ||
|
||
export function generateRootEntryPoint(): PluginOption { | ||
let viteConfig: ResolvedConfig; | ||
return { | ||
name: 'package-json-templating', | ||
configResolved(config) { | ||
viteConfig = config; | ||
}, | ||
async generateBundle() { | ||
if (viteConfig.command !== 'build') { | ||
return; | ||
} | ||
const entry = (viteConfig.build.lib as LibraryOptions).entry as Record<string, string>; | ||
const classNameMap: Record<string, string> = { | ||
SbbOptgroupElement: 'SbbOptGroupElement', | ||
}; | ||
const toElementName = (key: string): string => { | ||
const className = `sbb-${key.split('/').reverse()[0]}-element`.replace(/(^\w|-\w)/g, (m) => | ||
m.replace(/-/, '').toUpperCase(), | ||
); | ||
return classNameMap[className] ?? className; | ||
}; | ||
const keys = Object.keys(entry) | ||
.filter( | ||
(e, _, a) => | ||
a.every((iv) => !iv.startsWith(e + '/')) && | ||
!e.startsWith('core/') && | ||
!e.endsWith('/common'), | ||
) | ||
.sort() | ||
.map((e) => ({ path: `./${e}.js`, symbol: toElementName(e) })) | ||
.filter((v, i, a) => a.findIndex((iv) => iv.symbol === v.symbol) === i); | ||
const imports = keys.map((e) => `import { ${e.symbol} } from "${e.path}";\n`).join(''); | ||
this.emitFile({ | ||
type: 'asset', | ||
fileName: 'index.js', | ||
source: `${imports}\n${keys.map((e) => `globalThis.${e.symbol} = ${e.symbol};\n`).join('')}\nexport {}\n`, | ||
}); | ||
this.emitFile({ | ||
type: 'asset', | ||
fileName: 'index.d.ts', | ||
source: `${imports}\ndeclare global {\n${keys.map((e) => ` var ${e.symbol}: ${e.symbol};\n`).join('')}}\n\nexport {}\n`, | ||
}); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters