aurelia / webpack-plugin

A plugin for webpack that enables bundling Aurelia applications.
MIT License
90 stars 36 forks source link

bug: dependencies are not tracked with the as-element="compose" attribute #130

Open rmja opened 6 years ago

rmja commented 6 years ago

I'm submitting a bug report

Please tell us about your environment:

Current behavior: The plugin correctly tracks dependencies for the <compose view-model="./vs" /> but not for <some-other-tag as-element="compose" view-model="./vm" />

Expected/desired behavior: It should be possible to use the as-element in a similar way as with compose directly.

jods4 commented 6 years ago

Seems legitimate but given how the html parser (re-used from Webpack) works that might be a bit tricky.

I know it does not exactly solve this issue but it might help to know that you can register your own tag/attribute combinations as modules, see https://github.com/aurelia/webpack-plugin/wiki/Managing-dependencies#html-dependencies

3cp commented 6 years ago

It doesn't sound too difficult to me, but I am using a different html parser.

As reference, with htmlparser2, I do

let parser = new htmlparser.Parser({
    onopentag: function(name, attrs) {
      // <require from="dep"></require>
      if (name === 'require') {
        add(attrs.from);
      // <compose view-model="vm" view="view"></compose>
      // <any as-element="compose" view-model="vm" view="view"></any>
      } else if (name === 'compose' || attrs['as-element'] === 'compose') {
        add([attrs['view-model'], attrs.view]);
      // <router-view layout-view-model="lvm" layout-view="ly"></router-view>
      // <any as-element === 'router-view' layout-view-model="lvm" layout-view="ly"></any>
      } else if (name === 'router-view' || attrs['as-element'] === 'router-view') {
        add([attrs['layout-view-model'], attrs['layout-view']]);
      }
    }
  });
jods4 commented 6 years ago

I used the same parser as Webpack to minimize the number of dependencies installed.

It calls you back with with one (tag, attr) pair for each attribute in document order.

This means you can see layout-view attribute on <div> first and later be called with as-element='compose' for the same <div>.

But what is worse is that it's the only callback so you can't know if the second call with as-element attribute is on the same <div> as the first call or another one.

So basically, it's impossible to support as-element with the current parser, we have to switch to another one.

3cp commented 6 years ago

That's unfortunate. The parser looks quite small. It might not be too difficult to bring the code in and customize it, but that has risk on introducing bug.

Personally I will side to one more dep on htmlparser2 for simplicity and flexibility. Just my 2c.