├── .editorconfig ├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── .npmignore ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE ├── PLUGIN.md ├── README.md ├── index.js ├── package-lock.json ├── package.json └── test ├── expect ├── html-result.html ├── index.html └── sugar-result.html ├── fixtures ├── components │ └── component.html ├── img.html ├── index.html ├── index.sml └── posthtml.config.js └── index.test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_size = 2 6 | end_of_line = lf 7 | indent_style = space 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.html] 12 | insert_final_newline = false 13 | 14 | [*.md] 15 | trim_trailing_whitespace = false 16 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @voischev @GitScrum @michael-ciniawsky -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | > ✏️ Briefly describe the issue you are experiencing (or the feature you want to see added to the plugin). Tell us what you were trying to do and what happened instead. Remember, this is _not_ a place to ask questions. For that, go to http://gitter.im/posthtml/posthtml 2 | 3 | ### `Details` 4 | 5 | > ✏️ Describe in more detail the problem you have been experiencing, if necessary. 6 | 7 | ## `Error (Logs|Stacks)` 8 | 9 | > 👉 Create a [gist](https://gist.github.com) which is a paste of your **full** logs, and link them here. 10 | 11 | > ⚠️ Do **not** paste your full logs here (or at least hide them by using a `
` block), as it will make this issue long and hard to read! If you are reporting a bug, **always** include logs! 12 | 13 | ### `Reproduction (Code)` 14 | 15 | > ⚠️ Please remember that, with sample code; it's easier to reproduce a bug and much faster to fix it. 16 | 17 | > 🔗 Please refer to a simple code example. 18 | 19 | ```bash 20 | $ git clone https://github.com// 21 | ``` 22 | 23 | ### `Environment` 24 | 25 | > ℹ️ Please provide information about your current environment. 26 | 27 | |OS|node|npm/yarn|package| 28 | |:-:|:--:|:-:|:------:| 29 | |[name][version]|[version]|[version]|[version]| 30 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ### `Notable Changes` 2 | 3 | > ✏️ Describe the big picture of your changes here to communicate to the maintainers why we should accept this pull request. If it fixes a bug or resolves a feature request, be sure to link to that issue down below. 4 | 5 | #### `Commit Message Summary (CHANGELOG)` 6 | 7 | ``` 8 | commit message body... 9 | ``` 10 | 11 | ### `Type` 12 | 13 | > ℹ️ What types of changes does your code introduce? 14 | 15 | > 👉 _Put an `x` in the boxes that apply and delete all others_ 16 | 17 | - [ ] CI 18 | - [ ] Fix 19 | - [ ] Perf 20 | - [ ] Docs 21 | - [ ] Test 22 | - [ ] Chore 23 | - [ ] Style 24 | - [ ] Build 25 | - [ ] Feature 26 | - [ ] Refactor 27 | 28 | ### `SemVer` 29 | 30 | > ℹ️ What changes to the current `semver` range does your PR introduce? 31 | 32 | > 👉 _Put an `x` in the boxes that apply and delete all others_ 33 | 34 | - [ ] Fix (:label: Patch) 35 | - [ ] Feature (:label: Minor) 36 | - [ ] Breaking Change (:label: Major) 37 | 38 | ### `Issues` 39 | 40 | > ℹ️ What issue(s) (if any) are closed by your PR? 41 | 42 | > 👉 _Replace `#1` with the issue number that applies and remove the ``` ` ```_ 43 | 44 | - Fixes `#1` 45 | 46 | ### `Checklist` 47 | 48 | > ℹ️ You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. This is a reminder of what we are going to look for before merging your code. 49 | 50 | > 👉 _Put an `x` in the boxes that apply and delete all others._ 51 | 52 | - [ ] Lint and unit tests pass with my changes 53 | - [ ] I have added tests that prove my fix is effective/works 54 | - [ ] I have added necessary documentation (if appropriate) 55 | - [ ] Any dependent changes are merged and published in downstream modules -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # OS 2 | 3 | .DS_Store 4 | ._* 5 | 6 | # NODEJS 7 | 8 | npm-debug.log 9 | 10 | dmd 11 | jest 12 | coverage 13 | jsdoc-api 14 | node_modules 15 | .nyc_output 16 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | # FILES 2 | 3 | .travis.yml 4 | .gitignore 5 | .editorconfig 6 | 7 | CONTRIBUTING.md 8 | 9 | npm-debug.log 10 | 11 | # DIRECTORIES 12 | 13 | .github 14 | 15 | dmd 16 | test 17 | jest 18 | coverage 19 | jsdoc-api 20 | node_modules 21 | .nyc_output 22 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - node 4 | - lts/* 5 | - 10 6 | 7 | after_success: 8 | - npm i coveralls 9 | - './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls' 10 | 11 | notifications: 12 | email: false 13 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | 6 | ## [3.0.4](https://github.com/posthtml/gulp-posthtml/compare/v3.0.3...v3.0.4) (2018-01-22) 7 | 8 | 9 | 10 | 11 | ## [3.0.3](https://github.com/posthtml/gulp-posthtml/compare/v3.0.2...v3.0.3) (2017-12-08) 12 | 13 | 14 | 15 | 16 | # [3.0.0](https://github.com/posthtml/gulp-posthtml/compare/v1.5.2...v3.0.0) (2017-03-11) 17 | 18 | 19 | ### Features 20 | 21 | * **index:** add config callback ([bdda211](https://github.com/posthtml/gulp-posthtml/commit/bdda211)) 22 | * **index:** add posthtml-load-config for autoload support ([800368f](https://github.com/posthtml/gulp-posthtml/commit/800368f)) 23 | * **index:** small refactor, add posthtml-load-config ([c7c631c](https://github.com/posthtml/gulp-posthtml/commit/c7c631c)) 24 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | You want to help? You rock! Now, take a moment to be sure your contributions make sense to everyone else. 2 | 3 | ## Reporting Issues 4 | 5 | Found a problem? Want a new feature? 6 | 7 | - See if your issue or idea has [already been reported]. 8 | - Provide a [reduced test case] or a [live example]. 9 | 10 | Remember, a bug is a _demonstrable problem_ caused by _our_ code. 11 | 12 | ## Submitting Pull Requests 13 | 14 | Pull requests are the greatest contributions, so be sure they are focused in scope, and do avoid unrelated commits. 15 | 16 | 1. To begin, [fork this project], clone your fork, and add our upstream. 17 | ```bash 18 | # Clone your fork of the repo into the current directory 19 | git clone https://github.com//PLUGIN_NAME 20 | # Navigate to the newly cloned directory 21 | cd PLUGIN_NAME 22 | # Assign the original repo to a remote called "upstream" 23 | git remote add upstream https://github.com/GITHUB_NAME/PLUGIN_NAME 24 | # Install the tools necessary for development 25 | npm install 26 | ``` 27 | 28 | 2. Create a branch for your feature or fix: 29 | ```bash 30 | # Move into a new branch for a feature 31 | git checkout -b feature/thing 32 | ``` 33 | ```bash 34 | # Move into a new branch for a fix 35 | git checkout -b fix/something 36 | ``` 37 | 38 | 3. Be sure your code follows our practices. 39 | ```bash 40 | # Test current code 41 | npm run test 42 | ``` 43 | 44 | 4. Push your branch up to your fork: 45 | ```bash 46 | # Push a feature branch 47 | git push origin feature/thing 48 | ``` 49 | ```bash 50 | # Push a fix branch 51 | git push origin fix/something 52 | ``` 53 | 54 | 5. Now [open a pull request] with a clear title and description. 55 | 56 | [already been reported]: issues 57 | [fork this project]: fork 58 | [live example]: http://codepen.io/pen 59 | [open a pull request]: https://help.github.com/articles/using-pull-requests/ 60 | [reduced test case]: https://css-tricks.com/reduced-test-cases/ 61 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | License (MIT) 2 | 3 | Copyright (c) PostHTML 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /PLUGIN.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## gulp-posthtml(plugins, options) ⇒ function 4 | PostHTML Plugin for Gulp 5 | 6 | **Kind**: global function 7 | **Returns**: function - Stream (Transform) 8 | **Requires**: module:plugin-error, module:through2, module:posthtml, module:posthtml-load-config 9 | **Version**: 3.0.0 10 | **Author**: Ivan Voishev (@voishev) 11 | **License**: MIT 12 | 13 | | Param | Type | Description | 14 | | --- | --- | --- | 15 | | plugins | Array | PostHTML Plugins | 16 | | options | Object | PostHTML Options | 17 | 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![npm][npm]][npm-url] 2 | [![node][node]][node-url] 3 | [![deps][deps]][deps-url] 4 | [![tests][tests]][tests-url] 5 | [![coverage][cover]][cover-url] 6 | [![code style][style]][style-url] 7 | [![chat][chat]][chat-url] 8 | 9 |
10 | 11 | 12 | 13 | 14 |

Gulp PostHTML

15 |
16 | 17 |

Install

18 | 19 | ```bash 20 | npm i -D gulp-posthtml 21 | ``` 22 | 23 |

Usage

24 | 25 | ```js 26 | import { task, src, dest } from 'gulp' 27 | 28 | import posthtml from 'gulp-posthtml' 29 | 30 | task('html', () => { 31 | return src('src/*.html') 32 | .pipe(posthtml()) 33 | .pipe(dest('dest')) 34 | }) 35 | ``` 36 | 37 |

Options

38 | 39 | ### `Plugins` 40 | 41 | |Name|Type|Default|Description| 42 | |:--:|:--:|:-----:|:----------| 43 | |`plugins`|`{Array}`|`[]`|PostHTML Plugins| 44 | 45 | ### `Options` 46 | 47 | |Name|Type|Default|Description| 48 | |:--:|:--:|:-----:|:----------| 49 | |`options`|`{Object}`|`{}`|PostHTML Options| 50 | 51 | **gulpfile.js** 52 | ```js 53 | import { task, src, dest } from 'gulp' 54 | 55 | import tap from 'gulp-tap' 56 | import rename from 'gulp-rename' 57 | import posthtml from 'gulp-posthtml' 58 | 59 | task('html', () => { 60 | let path 61 | 62 | const plugins = [ require('posthtml-include')({ root: path }) ] 63 | const options = { parser: require('posthtml-sugarml')() } 64 | 65 | return src('src/*.html') 66 | .pipe(tap((file) => path = file.path)) 67 | .pipe(posthtml(plugins, options)) 68 | .pipe(rename({ ext: '.html' })) 69 | .pipe(dest('dest')) 70 | }) 71 | ``` 72 | 73 | ### `Config` 74 | 75 | |Name|Type|Default|Description| 76 | |:--:|:--:|:-----:|:----------| 77 | |`plugins`|`{Array}`|`[]`|PostHTML Plugins| 78 | |`options`|`{Object}`|`{}`|PostHTML Options| 79 | 80 | **gulpfile.js** 81 | ```js 82 | import { task, src, dest } from 'gulp' 83 | 84 | import rename from 'gulp-rename' 85 | import posthtml from 'gulp-posthtml' 86 | 87 | 88 | task('ssml', () => { 89 | const config = (file) => ({ 90 | plugins: [ require('posthtml-include')({ root: file.dirname }) ], 91 | options: { parser: require('posthtml-sugarml')() } 92 | }) 93 | 94 | return src('src/*.sml') 95 | .pipe(posthtml(config)) 96 | .pipe(rename({ ext: '.html' })) 97 | .pipe(dest('dest')) 98 | }) 99 | ``` 100 | 101 | ### [`posthtml.config.js`](https://github.com/posthtml/posthtml-load-config) 102 | 103 | #### `Context` 104 | 105 | |Name|Type|Default|Description| 106 | |:--:|:--:|:-----:|:----------| 107 | |`env`|`{String}`|`'development'`|process.env.NODE_ENV| 108 | |`file`|`{Object}`|`dirname, basename, extname`|File| 109 | |`options`|`{Object}`|`{}`|Options (Parser, Render, Plugin Options)| 110 | 111 | **posthtml.config.js** 112 | ```js 113 | module.exports = ({ file, options, env }) => ({ 114 | parser: 'posthtml-sugarml' 115 | plugins: { 116 | 'posthtml-include': { root: file.dirname }, 117 | 'posthtm-expressions': { locals: options.locals } 118 | 'htmlnano': env === 'production' ? {} : false 119 | } 120 | }) 121 | ``` 122 | 123 | **gulpfile.js** 124 | ```js 125 | import { task, src, dest } from 'gulp' 126 | 127 | import rename from 'gulp-rename' 128 | import posthtml from 'gulp-posthtml' 129 | 130 | 131 | task('ssml', () => { 132 | const ctx = { locals: { a: 'Hello World!'} } 133 | 134 | return src('src/*.sml') 135 | .pipe(posthtml(ctx)) 136 | .pipe(rename({ ext: '.html' })) 137 | .pipe(dest('dest')) 138 | }) 139 | ``` 140 | 141 |

Maintainer

142 | 143 | 144 | 145 | 146 | 152 | 153 | 154 |
147 | 149 |
150 | Ivan Voischev 151 |
155 | 156 |

Contributors

157 | 158 | 159 | 160 | 161 | 167 | 168 | 169 |
162 | 164 |
165 | Michael Ciniawsky 166 |
170 | 171 | [npm]: https://img.shields.io/npm/v/gulp-posthtml.svg 172 | [npm-url]: https://npmjs.com/package/gulp-posthtml 173 | 174 | [node]: https://img.shields.io/node/v/gulp-posthtml.svg 175 | [node-url]: https://nodejs.org/ 176 | 177 | [deps]: https://david-dm.org/posthtml/gulp-posthtml.svg 178 | [deps-url]: https://david-dm.org/posthtml/gulp-posthtml 179 | 180 | [tests]: http://img.shields.io/travis/posthtml/gulp-posthtml.svg 181 | [tests-url]: https://travis-ci.org/posthtml/gulp-posthtml 182 | 183 | [cover]: https://coveralls.io/repos/github/posthtml/gulp-posthtml/badge.svg 184 | [cover-url]: https://coveralls.io/github/posthtml/gulp-posthtml 185 | 186 | [style]: https://img.shields.io/badge/code%20style-standard-yellow.svg 187 | [style-url]: http://standardjs.com/ 188 | 189 | [chat]: https://badges.gitter.im/posthtml/posthtml.svg 190 | [chat-url]: https://gitter.im/posthtml/posthtml 191 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | // ------------------------------------ 2 | // #GULP - POSTHTML - INDEX 3 | // ------------------------------------ 4 | 5 | 'use strict' 6 | 7 | const path = require('path') 8 | 9 | const transform = require('through2').obj 10 | 11 | const PLUGIN_NAME = 'gulp-posthtml' 12 | 13 | const posthtml = require('posthtml') 14 | const posthtmlrc = require('posthtml-load-config') 15 | 16 | const PluginError = require('plugin-error') 17 | 18 | function rc (cb) { 19 | return function (plugins, options) { 20 | if (Array.isArray(plugins)) { 21 | return cb(() => Promise.resolve({ plugins: plugins, options: options })) // eslint-disable-line standard/no-callback-literal 22 | } else if (typeof plugins === 'function') { 23 | return cb((file) => Promise.resolve(plugins(file))) // eslint-disable-line standard/no-callback-literal 24 | } else { 25 | const ctx = plugins || {} 26 | 27 | return cb((file) => { // eslint-disable-line standard/no-callback-literal 28 | const config = {} 29 | 30 | if (ctx.config) { 31 | config.path = path.resolve(ctx.config) 32 | } else { 33 | config.path = file.dirname 34 | } 35 | 36 | config.ctx = { file: file, options: ctx } 37 | 38 | return posthtmlrc(config.ctx, config.path) 39 | }) 40 | } 41 | } 42 | } 43 | /** 44 | * @author Ivan Voishev (@voishev) 45 | * @description PostHTML Plugin for Gulp 46 | * @license MIT 47 | * 48 | * @module gulp-posthtml 49 | * @version 3.0.0 50 | * 51 | * @requires plugin-error 52 | * @requires through2 53 | * @requires posthtml 54 | * @requires posthtml-load-config 55 | * 56 | * @method gulp-posthtml 57 | * 58 | * @param {Array} plugins PostHTML Plugins 59 | * @param {Object} options PostHTML Options 60 | * 61 | * @return {Function} Stream (Transform) 62 | */ 63 | module.exports = rc((loadConfig) => { 64 | return transform((file, enc, cb) => { 65 | if (file.isNull()) { 66 | return cb(null, file) 67 | } 68 | 69 | if (file.isStream()) { 70 | return cb( 71 | new PluginError({ 72 | plugin: PLUGIN_NAME, 73 | message: 'Streams are not supported' 74 | }) 75 | ) 76 | } 77 | 78 | loadConfig(file).then((config) => { 79 | config.options = Object.assign( 80 | { from: file.path, to: file.path }, config.options 81 | ) 82 | 83 | return posthtml(config.plugins) 84 | .process(file.contents.toString(enc), config.options) 85 | .then((result) => { 86 | file.contents = Buffer.from(result.html) 87 | cb(null, file) 88 | }) 89 | .catch((err) => { 90 | // passing the error object directly would usually be fine, 91 | // but plugins like posthtml-expressions are an exception, so we're being safe 92 | // https://github.com/posthtml/posthtml-expressions/issues/89 93 | cb( 94 | new PluginError({ 95 | plugin: PLUGIN_NAME, 96 | message: err.message, 97 | stack: err.stack, 98 | showStack: true 99 | }) 100 | ) 101 | }) 102 | }) 103 | }) 104 | }) 105 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-posthtml", 3 | "version": "3.0.5", 4 | "description": "Gulp PostHTML Plugin", 5 | "main": "index.js", 6 | "engines": { 7 | "node": ">=4" 8 | }, 9 | "scripts": { 10 | "lint": "standard --verbose | snazzy", 11 | "test": "nyc ava", 12 | "docs": "jsdoc2md index.js > PLUGIN.md", 13 | "start": "npm run lint && npm test", 14 | "release": "standard-version" 15 | }, 16 | "dependencies": { 17 | "plugin-error": "^1.0.1", 18 | "posthtml": "^0.11.6", 19 | "posthtml-load-config": "^1.0.0", 20 | "through2": "^3.0.2" 21 | }, 22 | "devDependencies": { 23 | "ava": "^3.9.0", 24 | "jsdoc-to-markdown": "^6.0.1", 25 | "nyc": "^15.1.0", 26 | "posthtml-include": "^1.4.3", 27 | "posthtml-sugarml": "^1.0.0-alpha3", 28 | "snazzy": "^8.0.0", 29 | "standard": "^14.3.4", 30 | "standard-version": "^8.0.0", 31 | "vinyl": "^2.2.0" 32 | }, 33 | "keywords": [ 34 | "html", 35 | "posthtml", 36 | "posthtml-plugin", 37 | "gulp", 38 | "gulpplugin", 39 | "gulp-posthtml" 40 | ], 41 | "author": { 42 | "name": "Ivan Voishev", 43 | "email": "voischev.ivan@ya.ru" 44 | }, 45 | "contributors": [ 46 | { 47 | "name": "Michael Ciniawsky", 48 | "email": "michael.ciniawsky@gmail.com" 49 | } 50 | ], 51 | "repository": "https://github.com/posthtml/gulp-posthtml.git", 52 | "bugs": "https://github.com/posthtml/gulp-posthtml/issues", 53 | "homepage": "https://github.com/posthtml/gulp-posthtml#readme", 54 | "license": "MIT" 55 | } 56 | -------------------------------------------------------------------------------- /test/expect/html-result.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Gulp PostHTML 6 | 7 | 8 |
9 |

Hello World

10 |
11 | Lorem ipsum dolor sit amet, consectetur adipisicing elit. Modi vitae quis commodi ducimus quas sequi, laborum pariatur architecto, officiis amet sit consequatur placeat omnis explicabo sed. Illum, blanditiis doloribus quasi. 12 |
13 | 14 |
15 | 16 | 17 | -------------------------------------------------------------------------------- /test/expect/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Gulp PostHTML 6 | 7 | 8 |
9 |

Hello World

10 | 11 |
12 | 13 | 14 | -------------------------------------------------------------------------------- /test/expect/sugar-result.html: -------------------------------------------------------------------------------- 1 | Gulp PostHTML
2 | Lorem ipsum dolor sit amet, consectetur adipisicing elit. Modi vitae quis commodi ducimus quas sequi, laborum pariatur architecto, officiis amet sit consequatur placeat omnis explicabo sed. Illum, blanditiis doloribus quasi. 3 |
4 | -------------------------------------------------------------------------------- /test/fixtures/components/component.html: -------------------------------------------------------------------------------- 1 |
2 | Lorem ipsum dolor sit amet, consectetur adipisicing elit. Modi vitae quis commodi ducimus quas sequi, laborum pariatur architecto, officiis amet sit consequatur placeat omnis explicabo sed. Illum, blanditiis doloribus quasi. 3 |
4 | -------------------------------------------------------------------------------- /test/fixtures/img.html: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /test/fixtures/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Gulp PostHTML 6 | 7 | 8 |
9 |

Hello World

10 | 11 |
12 | 13 | 14 | -------------------------------------------------------------------------------- /test/fixtures/index.sml: -------------------------------------------------------------------------------- 1 | doctype html 2 | html(lang='en') 3 | head 4 | title Gulp PostHTML 5 | body 6 | include(src='component.html') 7 | -------------------------------------------------------------------------------- /test/fixtures/posthtml.config.js: -------------------------------------------------------------------------------- 1 | module.exports = (ctx) => ({ 2 | parser: 'posthtml-sugarml', 3 | plugins: { 4 | 'posthtml-include': ctx.options.include, 5 | 'posthtml-content': false, 6 | htmlnano: ctx.env === 'production' ? {} : false 7 | } 8 | }) 9 | -------------------------------------------------------------------------------- /test/index.test.js: -------------------------------------------------------------------------------- 1 | // ------------------------------------ 2 | // #GULP - POSTHTML - TEST - INDEX 3 | // ------------------------------------ 4 | 5 | const test = require('ava') 6 | const fs = require('fs') 7 | const path = require('path') 8 | const File = require('vinyl') 9 | 10 | const fixture = (file) => { 11 | return new File({ 12 | path: path.resolve('test/fixtures', file), 13 | contents: Buffer.from( 14 | fs.readFileSync(path.resolve('test/fixtures', file), 'utf8') 15 | ) 16 | }) 17 | } 18 | 19 | const expected = (file) => { 20 | return fs.readFileSync(path.resolve('test/expect', file), 'utf8') 21 | } 22 | 23 | const posthtml = require('..') 24 | 25 | test.cb('File', t => { 26 | t.plan(2) 27 | const html = fixture('index.html') 28 | 29 | const plugin = posthtml([]) 30 | 31 | plugin.write(html) 32 | 33 | plugin.on('data', (html) => { 34 | t.true(html.isBuffer()) 35 | t.is(html.contents.toString('utf8'), expected('index.html')) 36 | t.end() 37 | }) 38 | }) 39 | 40 | test.cb('Plugins', t => { 41 | t.plan(2) 42 | const html = fixture('index.html') 43 | 44 | const plugins = [ 45 | require('posthtml-include')({ root: './test/fixtures/components' }) 46 | ] 47 | 48 | const plugin = posthtml(plugins) 49 | 50 | plugin.write(html) 51 | 52 | plugin.on('data', (html) => { 53 | t.true(html.isBuffer()) 54 | t.is(html.contents.toString('utf8'), expected('html-result.html')) 55 | t.end() 56 | }) 57 | }) 58 | 59 | test.cb('Options', t => { 60 | t.plan(2) 61 | const html = fixture('index.sml') 62 | 63 | const cb = (file) => ({ 64 | plugins: [ 65 | require('posthtml-include')({ root: `${file.dirname}/components` }) 66 | ], 67 | options: { parser: require('posthtml-sugarml')() } 68 | }) 69 | 70 | const plugin = posthtml(cb) 71 | 72 | plugin.write(html) 73 | 74 | plugin.on('data', (html) => { 75 | t.true(html.isBuffer()) 76 | t.is(html.contents.toString('utf8'), expected('sugar-result.html')) 77 | t.end() 78 | }) 79 | }) 80 | 81 | test.cb('Function', t => { 82 | t.plan(2) 83 | const html = fixture('index.sml') 84 | 85 | const cb = (file) => ({ 86 | plugins: [ 87 | require('posthtml-include')({ root: `${file.dirname}/components` }) 88 | ], 89 | options: { parser: require('posthtml-sugarml')() } 90 | }) 91 | 92 | const plugin = posthtml(cb) 93 | 94 | plugin.write(html) 95 | 96 | plugin.on('data', (html) => { 97 | t.true(html.isBuffer()) 98 | t.is(html.contents.toString('utf8'), expected('sugar-result.html')) 99 | t.end() 100 | }) 101 | }) 102 | 103 | test.cb('Config', t => { 104 | t.plan(2) 105 | const html = fixture('index.sml') 106 | 107 | const ctx = { include: { root: './test/fixtures/components' } } 108 | 109 | const plugin = posthtml(ctx) 110 | 111 | plugin.write(html) 112 | 113 | plugin.on('data', (html) => { 114 | t.true(html.isBuffer()) 115 | t.is(html.contents.toString('utf8'), expected('sugar-result.html')) 116 | t.end() 117 | }) 118 | }) 119 | --------------------------------------------------------------------------------