createHtmlFile 方法

实现思路:

  • 新增一个 index.html 页面,设置一下 document.titlemain.jsx 入口文件

实现代码

点击 展开/收起 jsx 代码
import fs from 'fs';
import path from 'path';

function createHtmlFile() {
  const htmlFilePath = path.join(
    process.cwd(),
    'components-page',
    'src',
    'index.html',
  );

  const htmlContent = `
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>公共组件平台</title>
</head>
<body data-theme='light'>
  <div id="root"></div>
  <script type="module" src="/main.jsx"></script>
</body>
</html>
  `;

  if (!fs.existsSync(path.dirname(htmlFilePath))) {
    fs.mkdirSync(path.dirname(htmlFilePath), { recursive: true });
  }

  fs.writeFileSync(htmlFilePath, htmlContent.trim());
  console.log('------------------------------------------------------------');

  console.log(`index.html 文件已更新或创建,请查看:${htmlFilePath}`);
}

export default createHtmlFile;