{"id":27017472,"url":"https://github.com/frptools/tiny-preprocessor","last_synced_at":"2025-04-04T16:23:41.193Z","repository":{"id":65374835,"uuid":"83232148","full_name":"frptools/tiny-preprocessor","owner":"frptools","description":"A tiny DEV vs PROD preprocessor for JS and TS code","archived":false,"fork":false,"pushed_at":"2017-02-26T18:49:10.000Z","size":3,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-15T17:38:01.840Z","etag":null,"topics":["build","build-automation","build-pipelines","build-tools","javascript","typescript"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/frptools.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2017-02-26T18:48:40.000Z","updated_at":"2020-12-13T10:13:14.000Z","dependencies_parsed_at":"2023-01-20T01:35:14.405Z","dependency_job_id":null,"html_url":"https://github.com/frptools/tiny-preprocessor","commit_stats":null,"previous_names":["axefrog/tiny-preprocessor"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frptools%2Ftiny-preprocessor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frptools%2Ftiny-preprocessor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frptools%2Ftiny-preprocessor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frptools%2Ftiny-preprocessor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/frptools","download_url":"https://codeload.github.com/frptools/tiny-preprocessor/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247208459,"owners_count":20901570,"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":["build","build-automation","build-pipelines","build-tools","javascript","typescript"],"created_at":"2025-04-04T16:23:40.611Z","updated_at":"2025-04-04T16:23:41.177Z","avatar_url":"https://github.com/frptools.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Tiny Preprocessor\n\n\u003e A tiny DEV vs PROD preprocessor for JS and TS code\n\nIt's super annoying to have to strip out logging and debug code, or to have to clean up values and function arguments that were changed to make debugging and testing easier during development, particularly if your development-only code is necessary for ongoing development between releases.\n\nThe library exports a single function -- `preprocess` -- which takes a string argument (your source code), transforms it according to preprocessor comments you've included, then returns the result.\n\nInsert it into your preferred build pipeline so that your source is transformed before being passed to any transpilers, compilers, or just directly copied to a distributable folder. Be sure to run your test suite after preprocessing, to make sure you haven't accidentally broken your library with erroneous preprocessor comments.\n\nDuring development, compile and test using whatever regular process you use outside of production builds, and keep the\nproduction build process separate for when you're ready to release.\n\n## Preprocessor Syntax:\n\nPreprocessor blocks are simply IF or IF-ELSE constructs. They start with `## DEV|PROD`, and are terminated with another\n`##` token. `// ## DEV ##` comments affect the entire line. Square brackets `[[` and `]]` define the start and end of an\naffected block of code. Both forms of comment syntax are supported, depending on your needs. Typically your\nproduction-only code blocks will be contained within block comments, seeing as you don't want them to be active during\ndevelopment.\n\n```js\n// --- OMIT A SINGLE LINE ----------------------------------------------------\n\nlog(\"This line will be excluded from the build\"); // ## DEV ##\n\n// --- OMIT A WHOLE BLOCK ----------------------------------------------------\n\n// ## DEV [[\nconsole.warn('This line will be excluded.');\nwrite('This line too.');\n// ]] ##\n\n// --- USE A DIFFERENT VALUE IN PRODUCTION -----------------------------------\n\nvar value = /* ## DEV [[ */ 3 /* ]] ELSE [[ 27 ]] ## */;\n// The production build for the above line will render as:\nvar value = 27;\n\n// ## DEV [[\ntrace.silent(status1);\ntrace.silent(status2);\n/* ]] ELSE [[\ntrace.verbose(status1);\ntrace.verbose(status2);\n]] ## */\n\n// The production build for the above lines will render as:\ntrace.verbose(status1);\ntrace.verbose(status2);\n\n// --- PRODUCTION-ONLY CODE --------------------------------------------------\n\n/* ## PROD [[\ntrace.verbose(status1);\ntrace.verbose(status2);\n]] ## */\n\n// The production build for the above lines will render as:\ntrace.verbose(status1);\ntrace.verbose(status2);\n```\n\n## Installation\n\n```bash\n$ npm install --save-dev tiny-preprocessor\n```\n\nor\n\n```bash\n$ yarn add --dev tiny-preprocessor\n```\n\n## Usage\n\n```js\nimport {preprocess} from 'tiny-preprocessor';\n\nconst input = '... some source code ...';\nconst output = preprocess(input);\n```\n\n## Gulp\n\nI like to use Gulp, so this is how I use tiny-preprocessor:\n\n```js\n// gulpfile.js\n\nconst {preprocess} = require(`tiny-preprocessor`);\nconst gulp = require(`gulp`);\nconst merge = require(`merge2`);\nconst plumber = require(`gulp-plumber`);\nconst sourcemaps = require(`gulp-sourcemaps`);\nconst transform = require(`gulp-transform`);\n\n// ...\n\nfunction preprocessSourceText(buffer) {\n  return preprocess(buffer.toString());\n}\n\nfunction prebuild() {\n  return merge([\n    gulp.src(`src/**/*.ts`)\n      .pipe(plumber())\n      .pipe(transform(preprocessSourceText))\n      .pipe(gulp.dest(`.build/ts`)), // copy to a temporary build folder (omitted from the repo and npm)\n\n    gulp.src(`tests/**/*.ts`)\n      .pipe(plumber())\n      .pipe(transform(preprocessSourceText))\n      .pipe(transform(replace(/\\.\\.\\/src/g, `../ts`))) // fix paths so that tests will still compile\n      .pipe(gulp.dest(`.build/ts.tests`)),\n  ]);\n}\n\n// ...\n\nfunction compile() {\n  // your compile function should read from the build folder, e.g. .build/* in this case\n  const src = gulp.src(`.build/ts/**/*.ts`)\n    .pipe(plumber())\n    .pipe(sourcemaps.init())\n    // ...\n    ;\n  \n  const tests = gulp.src(`.build/ts.tests/**/*.ts`)\n    .pipe(plumber())\n    .pipe(sourcemaps.init())\n    // ...\n    ;\n  \n  return merge([\n    src,\n    tests\n  ]);\n}\n\ngulp.task(`prebuild`, prebuild);\ngulp.task(`compile`, [`prebuild`], compile);\n\n// ...\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrptools%2Ftiny-preprocessor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrptools%2Ftiny-preprocessor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrptools%2Ftiny-preprocessor/lists"}