/
githubmirror
/
30-seconds-of-code
Обзор
Документация
Войти
/
githubmirror
/
30-seconds-of-code
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/lib/contentUtils/componentCreator.js
54 строки
1 KB
Angelos Chalaris
Add content component creation utility
19 апр 2025, 11:26
Не верифицирован
19 апр 2025, 11:26
04ae837
Код
Авторство
О чём код?
import path from 'node:path'; import fs from 'node:fs'; import { componentScriptsPath, componentStylesPath, } from '#src/lib/contentUtils/config.js'; export default class ComponentCreator { static create = name => { const componentScriptPath = path.join( process.cwd(), componentScriptsPath, `${name}.mjs` ); const componentStylePath = path.join( process.cwd(), componentStylesPath, `${name}.scss` ); const webComponentName = name.replace(/-(\w)/g, (_, c) => c.toUpperCase()); const componentScriptContent = [ `import styles from '../styles/${name}.css' with { type: 'css' };`, '', `class ${webComponentName} extends HTMLElement {`, ` constructor() {`, ` super();`, ` }`, '', ` connectedCallback() {`, ` this.setAttribute('interactive', 'true');`, ` }`, '', `}`, '', `document.addEventListener('DOMContentLoaded', () => {`, ` document.adoptedStyleSheets.push(styles);`, ` customElements.define('${name}', ${webComponentName});`, `});`, ].join('\n'); const componentStyleContent = [`${name}[interactive='true'] {`, '}'].join( '\n' ); try { fs.writeFileSync(componentScriptPath, componentScriptContent); fs.writeFileSync(componentStylePath, componentStyleContent); } catch (err) { console.error(err); } }; }