{"id":13834674,"url":"https://github.com/itsdouges/ts-transform-define","last_synced_at":"2025-04-15T01:50:21.949Z","repository":{"id":41955896,"uuid":"257530889","full_name":"itsdouges/ts-transform-define","owner":"itsdouges","description":"Create global constants which can be configured at compile time using the TypeScript compiler.","archived":false,"fork":false,"pushed_at":"2023-01-06T03:55:15.000Z","size":1031,"stargazers_count":7,"open_issues_count":11,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-29T12:57:28.568Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/itsdouges.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-04-21T08:31:54.000Z","updated_at":"2023-06-17T09:01:49.000Z","dependencies_parsed_at":"2023-02-05T06:32:08.736Z","dependency_job_id":null,"html_url":"https://github.com/itsdouges/ts-transform-define","commit_stats":null,"previous_names":["compiled/ts-transform-define"],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itsdouges%2Fts-transform-define","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itsdouges%2Fts-transform-define/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itsdouges%2Fts-transform-define/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/itsdouges%2Fts-transform-define/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/itsdouges","download_url":"https://codeload.github.com/itsdouges/ts-transform-define/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248688575,"owners_count":21145765,"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-04T14:00:50.799Z","updated_at":"2025-04-15T01:50:21.932Z","avatar_url":"https://github.com/itsdouges.png","language":"TypeScript","funding_links":[],"categories":["Transformers"],"sub_categories":["General transformers"],"readme":"# ts-transform-define [![npm](https://img.shields.io/npm/v/ts-transform-define?style=flat-square)](https://www.npmjs.com/package/ts-transform-define)\n\nAllows you to create global constants which can be configured at compile time.\nThis is the TypeScript equivalent of https://webpack.js.org/plugins/define-plugin/.\n\n## Usage\n\n```bash\nnpm i ts-transform-define ttypescript\n```\n\nUpdate your tsconfig.json:\n\n```json\n{\n  \"compilerOptions\": {\n    \"plugins\": [\n      {\n        \"transform\": \"ts-transform-define\",\n        \"replace\": {\n          \"PRODUCTION\": \"true\",\n          \"VERSION\": \"'5fa3b9'\",\n          \"BROWSER_SUPPORTS_HTML5\": true,\n          \"TWO\": \"1+1\",\n          \"typeof window\": \"'object'\",\n          \"process.env.NODE_ENV\": \"process.env.NODE_ENV\",\n          \"isNodeEnvironment()\": \"true\"\n        }\n      }\n    ]\n  }\n}\n```\n\nRun TTypeScript instead of TypeScript:\n\n```diff\n-tsc\n+ttsc\n```\n\n\u003e Note that because the plugin does a direct text replacement,\n\u003e the value given to it must include actual quotes inside of the string itself.\n\u003e Typically,\n\u003e this is done either with alternate quotes,\n\u003e such as '\"production\"'.\n\n**index.ts**\n\n```js\nif (!PRODUCTION) {\n  console.log('Debug info');\n}\n\nif (PRODUCTION) {\n  console.log('Production log');\n}\n```\n\nAfter passing through the transformer:\n\n```js\nif (!true) {\n  console.log('Debug info');\n}\nif (true) {\n  console.log('Production log');\n}\n```\n\nand then after a minification pass results in:\n\n```js\nconsole.log('Production log');\n```\n\n### Environment variables\n\nWhen replacing with environment variables it will pass them through,\nso if we have this code:\n\n```ts\nif (process.env.NODE_ENV === 'development') {\n  doLotsOfWorkInDev();\n}\n```\n\nAnd then we have this config\n\n```json\n{\n  \"compilerOptions\": {\n    \"plugins\": [\n      {\n        \"transform\": \"ts-transform-define\",\n        \"replace\": {\n          \"process.env.NODE_ENV\": \"process.env.NODE_ENV\"\n        }\n      }\n    ]\n  }\n}\n```\n\nAnd then we run\n\n```bash\nNODE_ENV=\"production\" ttsc\n```\n\nThe code gets transformed to\n\n```tsx\nif ('production' === 'development') {\n  doLotsOfWorkInDev();\n}\n```\n\nWhich minifiers will see `false` in the if statement and delete the block.\n\n## Gotchas\n\nWhen replacing with environment variables, e.g. `process.env.NODE_ENV` - if it is not set at build time it will ignore replacing the expression.\n\nE.g:\n\n```json\n{\n  \"compilerOptions\": {\n    \"plugins\": [\n      {\n        \"transform\": \"ts-transform-define\",\n        \"replace\": {\n          \"process.env.NODE_ENV\": \"process.env.DONT_EXIST\"\n        }\n      }\n    ]\n  }\n}\n```\n\nWill result in:\n\n```ts\nif (process.env.NODE_ENV === 'development') {\n  doLotsOfWorkInDev();\n}\n```\n\nInstead of:\n\n```ts\nif (process.env.DONT_EXIST === 'development') {\n  doLotsOfWorkInDev();\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fitsdouges%2Fts-transform-define","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fitsdouges%2Fts-transform-define","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fitsdouges%2Fts-transform-define/lists"}