Exercise 11: NG2 APP Component In a A bundle#

Warning

This exercise requires a working buildout using a fork of the collective.jstraining package.

For this exercise, we will add an Angular 2 application to a Plone bundle.

We have most of the Angular 2 boiler plate code created for you so let us finish up a few things so you can customize it.

You will be working in the exercise11 directory of the collective.jstraining package.

Bootstrap#

Install npm dependencies

cd exercise11/static
npm install

Add Your Angular 2 Component#

In your exercise11/static/app directory, add a file named app.component.ts. Use this file to do anything you would like to the page.

This example will stick with the angular 2 quick start code.

We hope you like TypeScript.

import { Component } from '@angular/core';
@Component({
  selector: '.my-app',
  template: '<h1>NG2 from Exercise 11</h1>'
})
export class AppComponent { }

You can do whatever in this module. However please notice how we changed the selector to .my-app.

In Angular 2, the selector can be anything.

By changing it to a class name, it will be easier for us to choose where we want to bootstrap our Angular 2 component.

Register Static Resource Directory#

Next, let us register the static directory we placed our script into.

To register it, you need to add ZCML registration for the static directory your script is in. Add this to the exercise11/configure.zcml file:

<plone:static
     directory="static"
     type="plone"
     name="exercise11"
     />

Build The File With Webpack#

Our deployment is built using webpack

cd exercise11/static
webpack

Whenever you make a change to your component files, webpack will automatically re-build the distribution.

Register JavaScript Resource As A Bundle#

Register our script as a JavaScript resource with Plone.

In the exercise11/profiles/default/registry.xml file, add configuration to register your script:

<records prefix="plone.bundles/exercise11"
         interface='Products.CMFPlone.interfaces.IBundleRegistry'>
  <value key="merge_with">default</value>
  <value key="enabled">True</value>
  <value key="compile">False</value>
  <value key="jscompilation">++plone++exercise11/exercise11-compiled.min.js</value>
  <value key="csscompilation">++plone++exercise11/exercise11-compiled.css</value>
  <value key="last_compilation">2016-10-04 00:00:00</value>
</records>

A couple notes about this configuration:

  • merge_with tells Plone to combine this file with the default Plone bundles

  • compile is distinguish this bundle as one that is compiled outside of Plone

  • jscompilation and csscompilation are what Plone uses as the final compiled output

Installation#

  1. Start up your Plone instance

  2. Install the Exercise 11 add-on

Warning

To make sure your resource registry configuration changes apply, you will need to be in development mode.

You can also toggle development mode on and off, click save, to force configuration to be re-built after changes instead of keeping development mode on.

Running#

It is up to you how to apply the component class name to an element of your choice. A couple options available to you are:

  1. use TinyMCE source view and add class="my-app" onto any tag

  2. customize the theme on your site and add it to an element in your theme file or use a diazo rule to dynamically add the class to an element.

Development#

To make sure your changes are loaded after every build with webpack, make sure to go into Setup --> Resource registries and enable development mode.

Production#

Production for this is simple when you are no longer in development mode on your Plone site.

Webpack rebuilds the JavaScript distribution on every change.