From 7c7e07b6405ed87a5636ec243fbdf6ec6b22f051 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Mon, 6 Sep 2021 20:45:01 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E5=A2=9E=E5=8A=A0scanner=20=E5=AF=B9t?= =?UTF-8?q?sx=E7=9A=84Trans=E7=BB=84=E4=BB=B6=E7=9A=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build/config/i18next-scanner.config.js | 32 ++++++++++++++++++++++---- package.json | 1 + yarn.lock | 5 ++++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/build/config/i18next-scanner.config.js b/build/config/i18next-scanner.config.js index a5386677..96078b8d 100644 --- a/build/config/i18next-scanner.config.js +++ b/build/config/i18next-scanner.config.js @@ -1,5 +1,7 @@ -const fs = require('fs'); +const fs = require('fs-extra'); +const path = require('path'); const { crc32 } = require('crc'); +const esbuild = require('esbuild'); console.log('Scanning Translation in src folder...'); @@ -37,20 +39,42 @@ module.exports = { suffix: '}}', }, }, - transform: function customTransform(file, enc, done) { + transform: async function customTransform(file, enc, done) { //自己通过该函数来加工key或value 'use strict'; const parser = this.parser; - const content = fs.readFileSync(file.path, enc); + const content = await fs.readFile(file.path, enc); + parser.parseFuncFromString( content, { list: ['lang', 't'] }, (key, options) => { options.defaultValue = key; - let hashKey = `k${crc32(key).toString(16)}`; + const hashKey = `k${crc32(key).toString(16)}`; parser.set(hashKey, options); } ); + + // 如果是 tsx 文件,则使用esbuild转换成jsx后再输入 + if (path.extname(file.path) === '.tsx') { + const { code } = await esbuild.transform(content, { + jsx: 'preserve', + loader: 'tsx', + }); + parser.parseTransFromString( + code, + { component: 'Trans', i18nKey: 'i18nKey' }, + (key, options) => { + // 如果不是手动给, 则使用defaultValue 作为key + if (key === '') { + key = options.defaultValue; + } + let hashKey = `k${crc32(key).toString(16)}`; + parser.set(hashKey, options); + } + ); + } + done(); }, }; diff --git a/package.json b/package.json index 535abe36..ace78218 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,7 @@ "@typescript-eslint/eslint-plugin": "^4.28.1", "@typescript-eslint/parser": "^4.28.1", "commitlint": "^12.1.4", + "esbuild": "^0.12.25", "eslint": "^7.30.0", "eslint-config-prettier": "^8.3.0", "eslint-plugin-prettier": "^3.4.0", diff --git a/yarn.lock b/yarn.lock index 8edecfdd..791a2e88 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3853,6 +3853,11 @@ esbuild@^0.11.19: resolved "https://registry.npmjs.org/esbuild/-/esbuild-0.11.23.tgz#c42534f632e165120671d64db67883634333b4b8" integrity sha512-iaiZZ9vUF5wJV8ob1tl+5aJTrwDczlvGP0JoMmnpC2B0ppiMCu8n8gmy5ZTGl5bcG081XBVn+U+jP+mPFm5T5Q== +esbuild@^0.12.25: + version "0.12.25" + resolved "https://registry.npmjs.org/esbuild/-/esbuild-0.12.25.tgz#c2131cef022cf9fe94aaa5e00110b27fc976221a" + integrity sha512-woie0PosbRSoN8gQytrdCzUbS2ByKgO8nD1xCZkEup3D9q92miCze4PqEI9TZDYAuwn6CruEnQpJxgTRWdooAg== + esbuild@^0.12.9: version "0.12.21" resolved "https://registry.nlark.com/esbuild/download/esbuild-0.12.21.tgz#7ff32a9ac73ce4310f9cb61ea4c3da9756570d46"