h5_for_webserver.build.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // 执行脚本 taro build --type h5 ,并且把环境变量 TARO_API_URL 传递过去
  2. const { execSync } = require('child_process');
  3. const path = require('path');
  4. const fs = require('fs-extra');
  5. const TARO_API_URL = ''; //
  6. const TARO_MQTT_URL = 'ws://localhost:8083/mqtt';
  7. const rootDir = path.join(__dirname, '..'); // 项目根目录
  8. execSync(`taro build --type h5`, { cwd: rootDir, stdio: 'inherit', env: { ...process.env, TARO_API_URL, TARO_MQTT_URL } }, (error, stdout, stderr) => {
  9. if (error) {
  10. console.error(`Error executing command: ${error.message}`);
  11. return;
  12. }
  13. if (stderr) {
  14. console.error(`Command stderr: ${stderr}`);
  15. return;
  16. }
  17. console.log(`Command stdout: ${stdout}`);
  18. });
  19. try {
  20. // Step 1: Copy static H5 resources from dist/h5 to .build/pkg/static
  21. const sourceDir = path.join(__dirname, '..', 'dist', 'h5');
  22. const destDir = path.join(__dirname, 'pkg', 'static');
  23. if (!fs.existsSync(destDir)) {
  24. fs.mkdirSync(destDir, { recursive: true });
  25. }
  26. fs.copySync(sourceDir, destDir);
  27. console.log(`Copied static H5 resources from ${sourceDir} to ${destDir}`);
  28. // Step 2: Execute the pkg packaging command
  29. const pkgJsonPath = path.join(__dirname, 'pkg', 'pkg.json');
  30. const entryPoint = path.join(__dirname, 'pkg', 'server.js'); // Specify the entry point
  31. const outputPath = path.join(__dirname, 'pkg'); // Specify the output path
  32. const pkgCommand = `npx pkg ${entryPoint} --config ${pkgJsonPath} --output ${outputPath}/dros_server`;
  33. try {
  34. execSync(pkgCommand, { stdio: 'inherit' });
  35. console.log('Packaging completed successfully.');
  36. } catch (error) {
  37. console.error('Error during packaging:', error);
  38. }
  39. } catch (err) {
  40. console.error('Failed to execute npm run pkg:', err.message);
  41. process.exit(1);
  42. }