├── .gitignore ├── LICENSE ├── README.md ├── package.json ├── src └── index.js └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | pkg -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Igor Adrov 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## i18next-scanner-typescript 2 | 3 | Typescript support for [i18next-scanner](https://github.com/i18next/i18next-scanner/) 4 | 5 | ## Install 6 | 7 | ```bash 8 | yarn add -D i18next-scanner-typescript 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | const typescriptTransform = require('i18next-scanner-typescript'); 15 | 16 | module.exports = { 17 | options: { 18 | func: { 19 | // don't pass ts or tsx here! 20 | extensions: ['.js', '.jsx'], 21 | }, 22 | trans: { 23 | // don't pass ts or tsx here! 24 | extensions: ['.js', '.jsx'], 25 | }, 26 | }, 27 | // your i18next-scanner config 28 | // ... 29 | transform: typescriptTransform( 30 | // options 31 | { 32 | // default value for extensions 33 | extensions: [".ts", ".tsx"], 34 | // optional ts configuration 35 | tsOptions: { 36 | target: "es2017", 37 | }, 38 | }, 39 | 40 | // optional custom transform function 41 | function customTransform(outputText, file, enc, done) { 42 | // do something custom with the transpiled `outputText` 43 | parser.parseTransFromString(outputText); 44 | parser.parseFuncFromString(outputText); 45 | 46 | done(); 47 | }, 48 | ), 49 | }; 50 | ``` 51 | 52 | Double check that you don't have TS extensions in the non-transform configuration 53 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "i18next-scanner-typescript", 3 | "version": "1.2.1", 4 | "description": "i18next-scanner Typescript transform", 5 | "repository": { 6 | "type": "git", 7 | "url": "git+ssh://git@github.com/nucleartux/i18next-scanner-typescript.git" 8 | }, 9 | "main": "src/index.js", 10 | "author": "Igor Adrov", 11 | "license": "MIT", 12 | "private": false, 13 | "devDependencies": { 14 | "typescript": "^5.3.2" 15 | }, 16 | "peerDependencies": { 17 | "typescript": "5.x" 18 | }, 19 | "publishConfig": { 20 | "access": "public" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | const fs = require("fs"); 2 | const path = require("path"); 3 | const typescript = require("typescript"); 4 | 5 | module.exports = function typescriptTransform( 6 | options = { 7 | tsOptions: { 8 | target: "es2018", 9 | }, 10 | extensions: [".ts", ".tsx"], 11 | }, 12 | transformFn, 13 | ) { 14 | return function transform(file, enc, done) { 15 | const { base, ext } = path.parse(file.path); 16 | 17 | if (options.extensions.includes(ext) && !base.includes(".d.ts")) { 18 | const content = fs.readFileSync(file.path, enc); 19 | 20 | const { outputText } = typescript.transpileModule(content, { 21 | compilerOptions: options.tsOptions, 22 | fileName: path.basename(file.path), 23 | }); 24 | 25 | if (typeof transformFn === 'function') { 26 | transformFn.call(this, outputText, file, enc, done); 27 | return; 28 | } 29 | 30 | this.parser.parseTransFromString(outputText); 31 | this.parser.parseFuncFromString(outputText); 32 | } 33 | 34 | done(); 35 | }; 36 | }; 37 | -------------------------------------------------------------------------------- /yarn.lock: -------------------------------------------------------------------------------- 1 | # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. 2 | # yarn lockfile v1 3 | 4 | 5 | typescript@^5.3.2: 6 | version "5.3.2" 7 | resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.3.2.tgz#00d1c7c1c46928c5845c1ee8d0cc2791031d4c43" 8 | integrity sha512-6l+RyNy7oAHDfxC4FzSJcz9vnjTKxrLpDG5M2Vu4SHRVNg6xzqZp6LYSR9zjqQTu8DU/f5xwxUdADOkbrIX2gQ== 9 | --------------------------------------------------------------------------------