{"id":13686708,"url":"https://github.com/richardo2016/fib-typify","last_synced_at":"2025-04-11T05:15:26.895Z","repository":{"id":57235128,"uuid":"136600971","full_name":"richardo2016/fib-typify","owner":"richardo2016","description":"just write fibjs with typescript : )","archived":false,"fork":false,"pushed_at":"2024-04-17T12:43:04.000Z","size":210,"stargazers_count":6,"open_issues_count":2,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-11T05:15:18.228Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/richardo2016.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2018-06-08T09:54:29.000Z","updated_at":"2024-06-18T18:03:45.000Z","dependencies_parsed_at":"2024-01-14T18:12:29.421Z","dependency_job_id":"d7c9b5fc-9d47-477e-8c2d-60190b0222a5","html_url":"https://github.com/richardo2016/fib-typify","commit_stats":{"total_commits":139,"total_committers":2,"mean_commits":69.5,"dds":"0.014388489208633115","last_synced_commit":"274282d9294ba66f0b11092fd857a5c9a370ace2"},"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/richardo2016%2Ffib-typify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/richardo2016%2Ffib-typify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/richardo2016%2Ffib-typify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/richardo2016%2Ffib-typify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/richardo2016","download_url":"https://codeload.github.com/richardo2016/fib-typify/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248345268,"owners_count":21088245,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T15:00:38.307Z","updated_at":"2025-04-11T05:15:26.871Z","avatar_url":"https://github.com/richardo2016.png","language":"JavaScript","funding_links":[],"categories":["Packages"],"sub_categories":["Packager/Transformer"],"readme":"# fib-typify\n\n[![NPM version](https://img.shields.io/npm/v/fib-typify.svg)](https://www.npmjs.org/package/fib-typify)\n[![Build Status](https://travis-ci.com/richardo2016/fib-typify.svg?branch=master)](https://travis-ci.org/richardo2016/fib-typify)\n[![Build status](https://ci.appveyor.com/api/projects/status/lmp5jopw8m149v9h?svg=true)](https://ci.appveyor.com/project/richardo2016/fib-typify)\n\n🚀 Just coding fibjs program with typescript\n \n## Introduction**\n`fib-typify` allows you to write fibjs with [typescript] at compilation or at runtime. It depends on official [typescript]'s [internal compiler API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API).\n\nAs typescript is written with **nodejs**, it's not restricted in nodejs runtime only --- you can also compile typescript in browser or _ANY_ other pure Javascript runtime. That is, you can use it in fibjs also.\n\n## Pre-requisites\n\n- fibjs `\u003e= 0.27.0`\n\n## Usage\n\n```bash\n# locally\nnpm i -S fib-typify\n# or globally\nnpm i -g fib-typify\n```\n\n**Via Javascript**\n\n```javascript\n// entry.js\nconst vm = require('vm')\nconst typify = require('fib-typify')\n\ntypify.loader({\n    compilerOptions: {\n        inlineSourceMap: true\n    }\n}).sandbox().require('./index.ts', __dirname)\n\n// index.ts\nexport function foo (str: string): string {\n    return `hello, ${str}`\n}\n```\n\n**Via CLI**\n\n#### `fstc`\n\nStarted from `0.8.0`, you can run `ftsc`, it's command line like `tsc` from typescript, but it's for fibjs.\n\n```bash\n# compile, source directory(such as `src/*` below) is required\n./node_modules/.bin/ftsc src/* --outDir lib\n```\n\nMost general options of `tsc` are supported:\n\n- `--target`\n- `--module`\n- `--moduleResolution`\n- **`--jsx`**\n- **`--declaration`**\n- `--declarationDir`\n- `--emitDeclarationOnly`\n- **`--outDir`**\n- `--allowJs`\n- `--noEmit`\n- ...\n\nYou can just pass those options to `ftsc` as arguments(flags), just like what you did with `tsc`.\n\n```bash\n# compile your project's source to `lib` directory, with dts files emitted 🚀\nftsc ./src/* \\ \n    --outDir ./lib \\\n    --declaration \\\n    --declarationDir ./typings \\\n    --allowJs\n```\n\n#### `fib-typify`(deprecated)\n\nRun .ts script directly.\n\n```bash\n./node_modules/.bin/fib-typify ./src/index.ts\n```\n\nCommand above means compiling directory `src` to directory `lib` with configuration file `.typify.json`, which would be passed to `typescript.transpileModule(input, moduleOptions)` as 2nd param `moduleOptions`.\n\n\u003c!-- Or compile it to same directory with corresponding filename\n```bash\n# get compiled script `./script/index.js`\nfib-typify ./src/index.ts -o\n``` --\u003e\n\n\u003c!-- Or compile one file to specified position\n```bash\n# get compiled script `/tmp/a.js`\nfib-typify ./src/index.ts -o /tmp/a.js\n``` --\u003e\n\nrun valid resolvable script directly.\n\n```bash\n## which would try to run `./index.ts`, main script in `package.json`, './index.js', './index.json'...\nfib-typify ./\n\n## run `./src/index.js`, `./src/index.ts`, ...\nfib-typify ./src\n```\n\nI only provided simple and crude error exception mechanism, so in some cases the error emitted may be not friendly as you like, it's welcome to take PR to help optimizting this part of `fib-typify` :)\n\n**options**\n\n`-c, --config-file`: equals to `tsconfig.compilerOptions`, would overwrite the one from `tsconfig.json`\n\n`-o, --out`: (fallback to file when necessary,) equals to `tsconfig.outDir`, would overwrite the one from `tsconfig.json`\n\n## default tsCompilerOptions\n\n### internal default compilerOptions\n```javascript\n{\n    target: 'es6',\n    module: 'commonjs',\n    noImplicitUseStrict: true\n}\n```\n\n### Baisc Usage\n\n* `loader(moduleOptions: any, sandBoxCfg?: SandBoxInitialConfig): ChainLoader`\n\ngenerate one [ChainLoader]\n\n* `loader().sandbox(mods: object, require: Function, global: object, func?: SetLoaderCallback): Class_SandBox`\n\ngenerate one [ChainLoader]'s sandbox\n\n\n### Advanced APIs\n\n**NOTE** All `TSCompilerOptions` is just typescript's [compilerOptions]\n\n---\n\n* `generateLoaderbox(tsCompilerOptions: TSCompilerOptions, basedir: string)`\n\ngenerate one loaderBox with compilation options `tsCompilerOptions`.\n\n* `registerTsCompiler(sandbox: vm.SandBox, tsCompilerOptions: TSCompilerOptions = defaultCompilerOptions)`\n\nregister '.ts' compiler for one Sanbox which haven't been registered before.\n\n* `loaderBox`\n\ndefault loaderBox of `fib-typify`, it use **default tsCompilerOptions** as compiler options for `.ts`.\n\n* `defaultCompilerOptions`\n\ndefault typescript compiler options. More detail in [typescript's compiler options]\n\n---\n\n## loaderBox\n\nloaderBox is one new feature started from fib-typify`\u003e= 0.3`, it depends on new API `Sandbox::setModuleCompiler` in fibjs`\u003e= 0.26.0`, but it also work in fibjs `\u003e= 0.22.0 \u003c 0.26.0 ` **partly**\n\n### require typescript directly by default loaderBox\n\n```javascript\n// default-loader.js\nconst Typify = require('fib-typify')\n\nconst module = Typify.loaderBox.require('./index.ts', __dirname)\n```\n\n### require typescript directly by customized loaderBox\n\n```javascript\n// customized-loader.js\nconst Typify = require('fib-typify')\n\nconst loaderBox = Typify.loader({\n    compilerOptions: {\n        ...Typify.defaultCompilerOptions,\n        // enable some options as you like\n        strict: true,\n        diagnostics: true,\n        allowJs: true,\n    }\n}).sandbox()\n\nconst module = loaderBox.require('./index.ts', __dirname)\n```\n\n## Warning\n\n### `loaderBox` Limitations when `fibjs \u003c 0.25.0`\n\n**NOTE** it's **NOT** recommended to use fib-typify in fibjs \u003c= 0.26.x.\n\nFrom fibjs `0.26.0`, fibjs supports `setModuleCompiler` API which allow to customize compiler for module with explicit extension, so we can require typescript file directly by providing compiler for `.ts` file, which provided by fib-typify's `loaderBox`.\n\nfib-typify also support `loaderBox` feature in lower version fibjs(`\u003c 0.25.0`), but not full-feature support, so there are some advices for your application depending on fib-typify in fibjs(`\u003c 0.25.0`):\n\n- always add `.ts` suffix in `require` and `import` statement(**ESPECIALLY** when you run typescript by `fib-typify` CLI directly!)\n- don't `export interface` in pure `.ts` file\n- dont't write loop-requirement with import statement in `.ts`, if you really do it, write `exports.xxx = ...` instead of `export const xxx = ...` in the loop requirement.\n\nso it's better to upgrade fibjs to version`\u003e=0.25.0`, best to `\u003e=0.26.0`, which resolves typescript source faster than previous version fibjs in fib-typify.\n\n### compile `.ts` to `.js` before your deployment\n\nBy the way, although I have tested in some cases, but it's not enough to improve \"fib-typify's loaderBox can run in production directly\". In my own work, I use fib-typify's loaderBox to load all source code when app's in developing stage, but I would\ncompile source to **pure javascript** code before publishing.\n\nJust use command  `ftsc ./src/* --outDir ./lib --declaration` to compile your source codes, shipped with all dts files :)\n\n## Contributors ✨\n\nIf you wanna contribute to this package, just learn about [fibjs] first, then fork this repo :)\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --\u003e\n\u003c!-- prettier-ignore-start --\u003e\n\u003c!-- markdownlint-disable --\u003e\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"http://js.chenlei.me\"\u003e\u003cimg src=\"https://avatars0.githubusercontent.com/u/6339390?v=4\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eRay\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\u003ca href=\"https://github.com/yaoqiaofeng\"\u003e\u003cimg src=\"https://avatars0.githubusercontent.com/u/26225038?v=4\" width=\"100px;\" alt=\"\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eyaoqiaofeng\u003c/b\u003e\u003c/sub\u003e\u003c/a\u003e\u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n\u003c!-- markdownlint-enable --\u003e\n\u003c!-- prettier-ignore-end --\u003e\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n\n[typescript]:https://github.com/Microsoft/TypeScript\n[fibjs]:http://fibjs.org/\n[jstransformer-typescript]:https://github.com/jstransformers/jstransformer-typescript\n[jstransformer]:https://github.com/jstransformers/jstransformer\n\n[compilerOptions]:https://www.typescriptlang.org/docs/handbook/compiler-options.html\n[directoryCompilationOptions]:#directoryCompilationOptions\n[micromatch]:https://github.com/micromatch/micromatch#options\n[typescript's compiler options]:https://www.typescriptlang.org/docs/handbook/tsconfig-json.html\n[ChainLoader]:./@types/index.d.ts\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frichardo2016%2Ffib-typify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frichardo2016%2Ffib-typify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frichardo2016%2Ffib-typify/lists"}