{"_id":"5652003550c76e0d00615a49","category":{"_id":"56c219d8d3199a0d00694be6","project":"5617a1f5a410c90d00c612f5","__v":0,"version":"5619a39913874c0d007038e9","pages":[],"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-02-15T18:32:56.618Z","from_sync":false,"order":9999,"slug":"other-modules","title":"Other Modules"},"user":"5617a0a4a410c90d00c612f4","version":{"_id":"5619a39913874c0d007038e9","project":"5617a1f5a410c90d00c612f5","__v":5,"createdAt":"2015-10-10T23:47:37.777Z","releaseDate":"2015-10-10T23:47:37.777Z","categories":["5619a39a13874c0d007038ea","5619a39a13874c0d007038eb","5619a39a13874c0d007038ec","5619a39a13874c0d007038ed","5651062a3b4d113500ed22d1","5651c89a852cfb3500a69830","565505ad055aa40d006f0ca7","56c219d8d3199a0d00694be6"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"Orange Arrow","version_clean":"0.2.0","version":"0.2"},"project":"5617a1f5a410c90d00c612f5","__v":22,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-11-22T17:49:41.739Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":0,"body":"[![Version][npm-img]][npm-url] [![Downloads][dlm-img]][npm-url] [![Build Status][travis-img]][travis-url]\n\n[npm-img]: https://img.shields.io/npm/v/vinyl-transformer.svg\n[npm-url]: https://npmjs.org/package/vinyl-transformer\n[dlm-img]: https://img.shields.io/npm/dm/vinyl-transformer.svg\n[travis-img]: https://travis-ci.org/natronjs/vinyl-transformer.svg\n[travis-url]: https://travis-ci.org/natronjs/vinyl-transformer\n\n## Create a Transformer\n\n### Create by Extending the Base Class\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import {src, dest} from \\\"vinyl-fs\\\";\\nimport {Transformer} from \\\"vinyl-transformer\\\";\\nimport {compile} from \\\"...\\\";\\n\\nclass MyTransformer extends Transformer {\\n  \\n  construct(compileFn) {\\n    this._compileFn = compileFn;\\n  }\\n  \\n  transform(file) {\\n    file.contents = this._compileFn(file.contents);\\n  }\\n}\\n\\nlet stream = (src(\\\"src/**/*.js\\\")\\n  .pipe(new MyTransformer(compile))\\n  .pipe(dest(\\\"dist\\\"))\\n);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n### Create via a Function\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import {src, dest} from \\\"vinyl-fs\\\";\\nimport {createTransformer} from \\\"vinyl-transformer\\\";\\nimport {compile} from \\\"...\\\";\\n\\nlet MyTransformer = createTransformer((file) => {\\n  file.contents = compile(file.contents);\\n});\\n\\nlet stream = (src(\\\"src/**/*.js\\\")\\n  .pipe(new MyTransformer())\\n  .pipe(dest(\\\"dist\\\"))\\n);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n### Create via an Object\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import {src, dest} from \\\"vinyl-fs\\\";\\nimport {createTransformer} from \\\"vinyl-transformer\\\";\\nimport {compile} from \\\"...\\\";\\n\\nlet MyTransformer = createTransformer({\\n  initialize(compileFn) {\\n    this._compileFn = compileFn;\\n  },\\n\\ttransform(file) {\\n  \\tfile.contents = this._compileFn(file.contents);\\n  }\\n});\\n\\nlet stream = (src(\\\"src/**/*.js\\\")\\n  .pipe(new MyTransformer(compile))\\n  .pipe(dest(\\\"dist\\\"))\\n);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n## Create a Transformer Function\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import {src, dest} from \\\"vinyl-fs\\\";\\nimport {createTransformerFn} from \\\"vinyl-transformer\\\";\\nimport {compile} from \\\"...\\\";\\n\\nlet transform = createTransformerFn({\\n  initialize(compileFn) {\\n    this._compileFn = compileFn;\\n  },\\n\\ttransform(file) {\\n  \\tfile.contents = this._compileFn(file.contents);\\n  }\\n});\\n\\nlet stream = (src(\\\"src/**/*.js\\\")\\n  .pipe(transform(compile))\\n  .pipe(dest(\\\"dist\\\"))\\n);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n## Create an Immediate Transformer Instance\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import {src, dest} from \\\"vinyl-fs\\\";\\nimport {transformer} from \\\"vinyl-transformer\\\";\\nimport {compile} from \\\"...\\\";\\n\\nlet stream = (src(\\\"src/**/*.js\\\")\\n  .pipe(transformer((file) => {\\n\\t\\tfile.contents = compile(file.contents);\\n\\t}))\\n  .pipe(dest(\\\"dist\\\"))\\n);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n## Examples\n\n### List Files in the Pipeline\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import {src, dest} from \\\"vinyl-fs\\\";\\nimport {createTransformerFn} from \\\"vinyl-transformer\\\";\\n\\nlet filelist = createTransformerFn({\\n  initialize() {\\n    this._fileCount = 0;\\n  },\\n  transform(file) {\\n    console.log(\\\"File:\\\", file.path);\\n    this._fileCount++;\\n  },\\n  flush() {\\n    console.log(\\\"---- Files:\\\", this._fileCount);\\n  },\\n});\\n\\nlet stream = (src(\\\"src/**/*.js\\\")\\n  .pipe(filelist())\\n  .pipe(dest(\\\"dist\\\"))\\n);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n### Add a Header to all Files in the Pipeline\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import {src, dest} from \\\"vinyl-fs\\\";\\nimport {createTransformerFn} from \\\"vinyl-transformer\\\";\\n\\nlet header = createTransformerFn({\\n  initialize(headerStr) {\\n    this._header = new Buffer(headerStr);\\n  },\\n  transform(file) {\\n    file.contents = Buffer.concat([\\n      this._header, file.contents,\\n    ]);\\n  },\\n});\\n\\nlet stream = (src(\\\"src/**/*.js\\\")\\n  .pipe(header(\\\"/* HEADER */\\\\n\\\"))\\n  .pipe(dest(\\\"dist\\\"))\\n);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]","excerpt":"Create Transformers for Vinyl File Streams","slug":"module-vinyl-transformer","type":"basic","title":"vinyl-transformer"}

vinyl-transformer

Create Transformers for Vinyl File Streams

[![Version][npm-img]][npm-url] [![Downloads][dlm-img]][npm-url] [![Build Status][travis-img]][travis-url] [npm-img]: https://img.shields.io/npm/v/vinyl-transformer.svg [npm-url]: https://npmjs.org/package/vinyl-transformer [dlm-img]: https://img.shields.io/npm/dm/vinyl-transformer.svg [travis-img]: https://travis-ci.org/natronjs/vinyl-transformer.svg [travis-url]: https://travis-ci.org/natronjs/vinyl-transformer ## Create a Transformer ### Create by Extending the Base Class [block:code] { "codes": [ { "code": "import {src, dest} from \"vinyl-fs\";\nimport {Transformer} from \"vinyl-transformer\";\nimport {compile} from \"...\";\n\nclass MyTransformer extends Transformer {\n \n construct(compileFn) {\n this._compileFn = compileFn;\n }\n \n transform(file) {\n file.contents = this._compileFn(file.contents);\n }\n}\n\nlet stream = (src(\"src/**/*.js\")\n .pipe(new MyTransformer(compile))\n .pipe(dest(\"dist\"))\n);", "language": "javascript" } ] } [/block] ### Create via a Function [block:code] { "codes": [ { "code": "import {src, dest} from \"vinyl-fs\";\nimport {createTransformer} from \"vinyl-transformer\";\nimport {compile} from \"...\";\n\nlet MyTransformer = createTransformer((file) => {\n file.contents = compile(file.contents);\n});\n\nlet stream = (src(\"src/**/*.js\")\n .pipe(new MyTransformer())\n .pipe(dest(\"dist\"))\n);", "language": "javascript" } ] } [/block] ### Create via an Object [block:code] { "codes": [ { "code": "import {src, dest} from \"vinyl-fs\";\nimport {createTransformer} from \"vinyl-transformer\";\nimport {compile} from \"...\";\n\nlet MyTransformer = createTransformer({\n initialize(compileFn) {\n this._compileFn = compileFn;\n },\n\ttransform(file) {\n \tfile.contents = this._compileFn(file.contents);\n }\n});\n\nlet stream = (src(\"src/**/*.js\")\n .pipe(new MyTransformer(compile))\n .pipe(dest(\"dist\"))\n);", "language": "javascript" } ] } [/block] ## Create a Transformer Function [block:code] { "codes": [ { "code": "import {src, dest} from \"vinyl-fs\";\nimport {createTransformerFn} from \"vinyl-transformer\";\nimport {compile} from \"...\";\n\nlet transform = createTransformerFn({\n initialize(compileFn) {\n this._compileFn = compileFn;\n },\n\ttransform(file) {\n \tfile.contents = this._compileFn(file.contents);\n }\n});\n\nlet stream = (src(\"src/**/*.js\")\n .pipe(transform(compile))\n .pipe(dest(\"dist\"))\n);", "language": "javascript" } ] } [/block] ## Create an Immediate Transformer Instance [block:code] { "codes": [ { "code": "import {src, dest} from \"vinyl-fs\";\nimport {transformer} from \"vinyl-transformer\";\nimport {compile} from \"...\";\n\nlet stream = (src(\"src/**/*.js\")\n .pipe(transformer((file) => {\n\t\tfile.contents = compile(file.contents);\n\t}))\n .pipe(dest(\"dist\"))\n);", "language": "javascript" } ] } [/block] ## Examples ### List Files in the Pipeline [block:code] { "codes": [ { "code": "import {src, dest} from \"vinyl-fs\";\nimport {createTransformerFn} from \"vinyl-transformer\";\n\nlet filelist = createTransformerFn({\n initialize() {\n this._fileCount = 0;\n },\n transform(file) {\n console.log(\"File:\", file.path);\n this._fileCount++;\n },\n flush() {\n console.log(\"---- Files:\", this._fileCount);\n },\n});\n\nlet stream = (src(\"src/**/*.js\")\n .pipe(filelist())\n .pipe(dest(\"dist\"))\n);", "language": "javascript" } ] } [/block] ### Add a Header to all Files in the Pipeline [block:code] { "codes": [ { "code": "import {src, dest} from \"vinyl-fs\";\nimport {createTransformerFn} from \"vinyl-transformer\";\n\nlet header = createTransformerFn({\n initialize(headerStr) {\n this._header = new Buffer(headerStr);\n },\n transform(file) {\n file.contents = Buffer.concat([\n this._header, file.contents,\n ]);\n },\n});\n\nlet stream = (src(\"src/**/*.js\")\n .pipe(header(\"/* HEADER */\\n\"))\n .pipe(dest(\"dist\"))\n);", "language": "javascript" } ] } [/block]