posthtml / gulp-posthtml

PostHTML for Gulp
Other
18 stars 4 forks source link

improve: pass meta data of chunk to plugin #2

Closed island205 closed 8 years ago

island205 commented 8 years ago

this is need by my posthtml plugin posthtml-web-component

say as I have an index.html:

<!doctype html>
<html>
<head>

    <meta charset="utf-8">
    <title>hello-world</title>

    <!-- Imports custom element -->
    <link rel="import" href="./hello-world.html">

</head>
<body>

    <!-- Runs custom element -->
    <hello-world></hello-world>

</body>
</html>

if i want get the real path of hello-world.html in <link rel="import" href="./hello-world.html">, i need to kown the real path of index.html.

How?

Working with jade, well, one posthtml instance handle one file, i can pass the path by options passed to the plugin.

var posthtml = require('posthtml')
module.exports = function (path, options, callback) {

    var html = require('ejs').renderFile(path, options, function(err, html) {
      if (err) {
        return callback(err)
      }
      posthtml([
        require('../src/index')({
          hostURI:path
        })
      ])
          .process(html)
          .then(function (result) {
              if (typeof callback === 'function') {
                  var res;
                  try {
                      res = result.html;
                  } catch (ex) {
                      return callback(ex);
                  }
                  return callback(null, res);
              }
          });
    });
}

but when come to gulp-posthtml, i can't. options pass to plugin only pass once, and i don't kown the real path of index.html, until in the gulp stream.

so my workaround is pass the real path by options in the .process(tree, options) call, pass it to the tree.options.

voischev commented 8 years ago

Write gulp configuration or create tree in http://github.com/posthtml/project-stub for examples

michael-ciniawsky commented 8 years ago

@island205 I have taken this into account, maybe (hopefully) posthtml gets a file option soon and plugins can access the path directly from the AST(PostHTMLTree)

poshtml().process(html, { file: 'path/to/file.html' }).then(({ tree }) => console.log(tree))
[ 
  '<!DOCTYPE html>',
  '\n',
  { tag: 'html', attrs: { lang: 'en' } },
  '\n',
  { tag: 'head', content: [ '\n  ', [Object], '\n  ', [Object], '\n' ] },
  '\n',
  { tag: 'body', content: [ '\n  ', [Object], '\n' ] },
  file: { path: 'path/to', name: 'file.html' } // <------
  options: { parser: [Function] } 
]

I will close this PR for now

island205 commented 8 years ago

@michael-ciniawsky great work, look forward to this api!