|
1 | 1 | # gulp-map-transform |
2 | 2 |
|
3 | | -Easily transform virtual paths in any file to real paths |
| 3 | +[](https://www.npmjs.com/package/gulp-map-transform) |
| 4 | +[](https://circleci.com/gh/davideperozzi/gulp-map-transform/tree/master) |
| 5 | +[](./LICENSE) |
| 6 | + |
| 7 | +Easily transform virtual paths inside any file to real paths |
| 8 | + |
| 9 | +## Install |
| 10 | + |
| 11 | +Install via npm: |
| 12 | +```sh |
| 13 | +npm install --save-dev gulp-map-transform |
| 14 | +``` |
| 15 | + |
| 16 | +## Usage |
| 17 | + |
| 18 | +### Transforming scss paths to compiled css paths |
| 19 | + |
| 20 | +Given this section of a html file: |
| 21 | + |
| 22 | +```html |
| 23 | +... |
| 24 | +<head> |
| 25 | + <link rel="stylesheet" href="@styles/test.css" /> |
| 26 | +</head> |
| 27 | +... |
| 28 | +``` |
| 29 | + |
| 30 | +And this configuration inside your gulpfile |
| 31 | + |
| 32 | +```js |
| 33 | +const { src, dest } = require('gulp'); |
| 34 | +const sass = require('gulp-sass'); |
| 35 | +const mapTransform = require('gulp-map-transform'); |
| 36 | + |
| 37 | +const OUT_PATH = '/dist/folder'; |
| 38 | + |
| 39 | +src('/html/files/**/*.html') |
| 40 | + .pipe( |
| 41 | + mapTransform({ |
| 42 | + search: /href="@styles\/(.*?).scss"/gi, |
| 43 | + replace: /"(.*?)"/i, |
| 44 | + rootPath: OUT_PATH, |
| 45 | + rewrite: (path) => path.replace('@styles', 'css/files'), |
| 46 | + transform: (stream) => stream |
| 47 | + .pipe(sass()) |
| 48 | + .pipe(dest(path.join(OUT_PATH, 'css'))) |
| 49 | + }) |
| 50 | + ) |
| 51 | + .pipe(dest(OUT_PATH)) |
| 52 | +``` |
| 53 | + |
| 54 | +This will generate the following html file after compiling the css file: |
| 55 | +```html |
| 56 | +... |
| 57 | +<head> |
| 58 | + <link rel="stylesheet" href="/css/files/test.css" /> |
| 59 | +</head> |
| 60 | +... |
| 61 | +``` |
| 62 | + |
| 63 | +## Options |
| 64 | +| Name | Type | Required | Description |
| 65 | +| -- | -- | -- | -- |
| 66 | +| search | `RegExp` | yes | This expression is used to identify in which location you want to execute a transformation of the content |
| 67 | +| replace | `RegExp` | yes | This is used to replace the path of the file inside a result of the search matches |
| 68 | +| rewrite | Function | yes | Use this to tell what and how to replace the path inside the string found by the replace expression |
| 69 | +| transform | Function | yes | In this callback you get a separate stream to handle all your files already present with the real path |
| 70 | +| rootPath | string | no | The root path to which the transfomed file paths will be appended (with `path.relative()`) |
| 71 | + |
| 72 | +## License |
| 73 | +See the [LICENSE](./LICENSE) file for license rights and limitations (MIT). |
0 commit comments