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 |
--------------------------------------------------------------------------------