Showing posts with label vulcanize. Show all posts
Showing posts with label vulcanize. Show all posts

Sunday, April 6, 2014

Vulcanize Inline… Into Polymer.dart?


Up tonight, a bit of Polymer deployment sanity checking. Does the approach that I have taken in polymer-one-script for deployment of Polymer.dart elements make sense?

I have enjoyed exploring the issue of deploying Dart Polymer element the past few days, but I begin to realize that it may not wind up being relevant in Patterns in Polymer. In that book, I am trying to find patterns and approaches that work well in both the JavaScript and Dart versions of the project. That is, I hope to identify patterns that transcend individual language implementations and instead cut to the core of what it means to be a Polymer element.

Herein lies my problem: deployment of JavaScript Polymers is trivial thanks to Vulcanize. I cannot very well write a “transcendent” chapter in the two different versions of the book where it takes me 20 pages to describe a thing in one language and one paragraph in the other. That is hardly transcendent.

Still, it seems worth taking a look. The default vulcanize command indeed does not produce what I have polymer-one-script doing in Dart. It produces a deployable HTML description of the element with external <script> references:
➜  js git:(master) ✗ vulcanize elements/x-pizza.html       
Default output to vulcanized.html in the input directory.
➜  js git:(master) ✗ grep src elements/vulcanized.html 
<script src="../bower_components/polymer/polymer.js"></script>
  <script src="x_pizza_toppings.js"></script>
  <script src="x_pizza.js"></script>
It does, however, support the --inline option to accomplish more or less (actually much less size-wise) the same thing that polymer-one-script does:
➜  js git:(master) ✗ vulcanize --inline elements/x-pizza.html
Default output to vulcanized.html in the input directory.
➜  js git:(master) ✗ grep src elements/vulcanized.html       
➜  js git:(master) ✗ 
Actually, that is not quite the same thing that polymer-one-script does. My script produces a JavaScript source file for the element instead of an HTML description of the element with the code embedded in it. Ah well, I am not going to sweat that difference much.

With that question answered, I ought to call it a night, but… Darn it I am curious about something else now. The example on which I have been experimenting, <x-pizza>, is actually comprised of two elements: the parent <x-pizza> and several child <x-pizza-toppings> elements that enable the discerning pizza buyer to add toppings:



This is the same in both the Dart and JavaScript versions of my element. I wonder, can I create a single, inline version of the <x-pizza-toppings> element in JavaScript and use it in Dart? There is one way to find out!
➜  js git:(master) ✗ vulcanize --inline elements/x-pizza-toppings.html 
Default output to vulcanized.html in the input directory.
➜  js git:(master) ✗ cd ../dart      
➜  dart git:(master) ✗ cp ../js/elements/vulcanized.html lib/elements
Giving this a try with the pub serve built-in web server and Dartium, I find… an error:
Uncaught ReferenceError: PolymerExpressions is not defined
That would seem to indicate that I need some lovely JavaScript libraries, which make me think that this is going to wind up being prohibitively hard. Still, I give it a shot by copying the Bower configuration from my JavaScript sample and installing Polymer.js into my Polymer.dart application:
➜  web git:(master) ✗ cp ../../js/bower.json .
➜  web git:(master) ✗ bower install
After adding that to my containing page, however. I still get failures. I add the platform as:
<script src="bower_components/platform/platform.js"></script>
That ought to load the core of Polymer.JS—enough, at least, to define PolymerExpressions and anything else needed. Unfortunately, this seems to interact very badly with the JavaScript coming from pub build as I get crazy long error messages:
TypeError {stack: "TypeError: Cannot read property 'getAttribute' of …0/bower_components/platform/platform.js:35:22390)", message: "Cannot read property 'getAttribute' of undefined"}
 "TypeError: Cannot read property 'getAttribute' of undefined
    at eval [as getAttribute] (eval at getMethod (http://localhost:8080/packages/shadow_dom/shadow_dom.debug.js:1796:49), <anonymous>:2:17)
    at d.createdCallback (http://localhost:8080/index.html:85:22502)
    at k (http://localhost:8080/bower_components/platform/platform.js:35:27681)
    at h (http://localhost:8080/bower_components/platform/platform.js:35:27258)
    at Object.s [as upgrade] (http://localhost:8080/bower_components/platform/platform.js:35:28650)
    at h (http://localhost:8080/bower_components/platform/platform.js:35:22897)
    at e (http://localhost:8080/bower_components/platform/platform.js:35:22605)
    at http://localhost:8080/bower_components/platform/platform.js:35:22667
    at http://localhost:8080/bower_components/platform/platform.js:35:22555
    at b (http://localhost:8080/bower_components/platform/platform.js:35:22390)" shadow_dom.debug.js:2816
TypeError {stack: "TypeError: Cannot read property 'polymerShadowRoot…0/bower_components/platform/platform.js:35:25116)", message: "Cannot read property 'polymerShadowRoot_' of undefined"}
 "TypeError: Cannot read property 'polymerShadowRoot_' of undefined
    at shadowRoot (http://localhost:8080/packages/shadow_dom/shadow_dom.debug.js:4345:23)
    at c (http://localhost:8080/bower_components/platform/platform.js:35:22474)
    at http://localhost:8080/bower_components/platform/platform.js:35:22568
    at b (http://localhost:8080/bower_components/platform/platform.js:35:22390)
    at b (http://localhost:8080/bower_components/platform/platform.js:35:22403)
    at b (http://localhost:8080/bower_components/platform/platform.js:35:22403)
    at d (http://localhost:8080/bower_components/platform/platform.js:35:22532)
    at f (http://localhost:8080/bower_components/platform/platform.js:35:22644)
    at g (http://localhost:8080/bower_components/platform/platform.js:35:22711)
    at Object.y [as upgradeDocument] (http://localhost:8080/bower_components/platform/platform.js:35:25116)"
I suppose I should have left well enough alone, but this seems like something that might be nice. I may give this another try tomorrow—with the dev channel version of Dart and Polymer.dart.


Day #26

Saturday, January 11, 2014

Day 993: Vulcanize (with Exclusions)


Deployment of Polymer.dart is causing me a bit of bother. In a good way. I continue to stretch the boundaries of what Polymer can do—and probably should do. I have to admit that I am struggling with including data in Polymers—especially when it comes to deployment. I am so very close to having a working solution in Dart, but I begin to suspect that I am fighting the framework just a little too much. So I am switching apps, back to the JavaScript equivalent to see if I have similar problems.

I am still trying to read configuration from both the Polymer definition and when using the Polymer. I have been placing the configuration in a <link> import before the Polymer definition:
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="hello-you.json">
<polymer-element name="hello-you">
  <!-- ... -->
</polymer-element>
The configuration when using the element goes inside the Polymer tags:
<hello-you>
  <link rel="import" href="scripts/hello-you.json">
  <p>And good-bye…</p>
</hello-you>
And, thanks to the magic of Polymer, that works just fine—when the Polymers are separate from the web and not combined into a single deployment asset. But I recognize that this might not be ideal for real-world usage, which is where the deployment-minded vulcanize comes in.

I start by installing vulcanize:
$ npm install -g vulcanize
Then I run it on the entry page to my JavaScript Polymer application:
$ vulcanize index.html
Default output to vulcanized.html in the input directory.
When I look at my JSON configured Polymer pre-vulcanize, I see:



When I load up the vulcanized version, I find:



Well, that does not bode well for my attempts at external configuration.

There are a couple of options that vulcanize supports. The --csp creates an external JavaScript file, but my configuration imports are still slurped directly into the page.

The only other option that I can think to use is the --config. In a configuration file, I find that I can specify imports that I want vulcanize to ignore. That sounds promising. So I create vulcanize_conf.js with the following content:
{
  "excludes": {
    "imports": [
      "\\.json$"
    ]
  }
}
The documentation suggests that I can exclude by regular expression, which is what I do above. I then use the configuration:
$ vulcanize --config=vulcanize_conf.js index.html 
Default output to vulcanized.html in the input directory.
And that actually works:



Well, not quite. The default configuration in the Polymer definition is still imported, but the configuration when I use the Polymer, while still in the HTML, is not imported properly.

Still, this seems promising. While not ideal to have to exclude JSON configuration in Polymers, it seems doable and seems, if anything, slightly easier than in the Dart equivalent.



Day #993