From 55c5973d379eda689fd1bfd4146f330d3661fbff Mon Sep 17 00:00:00 2001 From: Alf Eaton Date: Mon, 21 Mar 2022 10:26:25 +0000 Subject: [PATCH] Add custom transform for extracting i18next keys from TypeScript files (#7138) GitOrigin-RevId: 3b435b999c72e666eff25f86eb65e0368e953910 --- services/web/i18next-scanner.config.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/services/web/i18next-scanner.config.js b/services/web/i18next-scanner.config.js index 10e6252230..30394cbf90 100644 --- a/services/web/i18next-scanner.config.js +++ b/services/web/i18next-scanner.config.js @@ -1,7 +1,11 @@ +const fs = require('node:fs') +const path = require('node:path') +const typescript = require('typescript') + module.exports = { input: [ - 'frontend/js/**/*.{js,jsx}', - 'modules/**/*.{js,jsx}', + 'frontend/js/**/*.{js,jsx,ts,tsx}', + 'modules/**/*.{js,jsx,ts,tsx}', '!frontend/js/vendor/**', ], output: './', @@ -24,4 +28,22 @@ module.exports = { lineEnding: '\n', }, }, + // adapted from https://github.com/nucleartux/i18next-scanner-typescript/blob/master/src/index.js + transform: function (file, enc, done) { + const { base, ext } = path.parse(file.path) + + if (['.ts', '.tsx'].includes(ext) && !base.endsWith('.d.ts')) { + const content = fs.readFileSync(file.path, enc) + + const { outputText } = typescript.transpileModule(content, { + compilerOptions: { target: 'es2018', jsx: 'preserve' }, + fileName: base, + }) + + this.parser.parseTransFromString(outputText) + this.parser.parseFuncFromString(outputText) + } + + done() + }, }