@babel/core#transformFileSync TypeScript Examples

The following examples show how to use @babel/core#transformFileSync. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: wrap.ts    From i18n-helper with MIT License 4 votes vote down vote up
wrap = async (
  files: string[],
  i18nConf: iI18nConf,
  cmdConf: Partial<iCmd>,
): Promise<iWrapResult> => {
  const wrapResult: iWrapResult = { wrapInfo: { WRAP_FILE: 0 } };
  let failCount = 0;

  files.forEach((filename) => {
    const transInfo: iTransInfo = {
      needT: false,
      needTrans: false,
      needImport: true,
      wordInfoArray: [],
      wrapCount: 0,
      wrapSuccess: true,
    };
    const plugin = i18nPlugin(transInfo, i18nConf);
    const transResult = transformFileSync(filename, {
      plugins: [
        ['@babel/plugin-syntax-typescript', { isTSX: true }],
        // ['@babel/plugin-proposal-decorators', { legacy: true }],
        // ['@babel/plugin-proposal-class-properties', { loose: true }],
        plugin,
      ],
      retainLines: true,
      configFile: false,
      babelrc: false,
    });

    if (transResult) {
      const code = transResult.code as string;
      const needWrap = transInfo.needT || transInfo.needTrans;
      // wordInfoArray 包含2个部分
      // 1. 未被包裹的中文词条
      // 2. 被 t 包裹后的 中文词条 (不包含这部分的话在包裹后无法提取词条)
      if (transInfo.wordInfoArray.length > 0) {
        originalScanWordInfoList.push(transInfo.wordInfoArray);
        wrapResult.originalScanWordInfoLit = originalScanWordInfoList;
      }

      if (needWrap) {
        if (cmdConf.wrap || cmdConf.check) {
          wrapResult.wrapInfo[filename] = transInfo.wrapCount;
          generateFileCount += 1;
          if (cmdConf.wrap) {
            generateFile(transInfo, code, filename, i18nConf);
          }
        }
      }

      if (!transInfo.wrapSuccess) {
        failCount += 1;
      }
    }
  });

  if (cmdConf.wrap) {
    if (generateFileCount > 0) {
      Logger.success(t('【包裹】词条包裹已完成!'));
    } else {
      Logger.warning(t('【包裹】所有词条已被包裹,无需再次包裹!'));
    }

    if (failCount > 0) {
      Logger.warning(
        '【包裹】本次有词条包裹不成功,详情参见./i18n.error.log!',
      );
    }
  }

  if (cmdConf.check) {
    if (generateFileCount === 0) {
      Logger.success(t('【检查未翻译词条】恭喜,所有词条都已经包裹!'));
    } else {
      const words = Object.values(wrapResult.wrapInfo);
      const wordCount = words.reduce((total, num) => {
        return total + num;
      });
      Logger.error(
        t(
          `【检查未包裹词条】涉及【${generateFileCount}】文件,共有【${wordCount}】词条未包裹!详情如下表:`,
        ),
      );
    }
  }

  return wrapResult;
}