Writing code for polymer elements is easy but the consumption and development workflows
can be quite tricky to understand at first. The objective of this guide is to break
down this workflow and explain it, the target audience of this article is somebody who
has novice level experience in web technologies like npm and bower.
All the polymer elements are written to be consumed by the bower dependency manager.
bower is a flat dependency manager, this can be contrasted with npm which is a
hierarchichal dependency manager. This can be seen very easily by observing the directory
structure of bower_components and node_modules, the dependencies of a package are
installed alongside it by the bower while npm installs those dependencies inside
a separate directory node_modules located inside the package's directory. This
nature of bower is important to understand while developing Polymer components, you should
have a look at the structure of Polyemer's seed-element, note how the dependency polymer consumed.
All the components should be consumed in above fashion.
Above section mentions how should a dependency be consumed in your code but it doesn't
specify how to install the dependencies themselves. Writing link tag referencing the
dependency doesn't install the dependency, you have to install it using the following
command.
bower install --save PolymerElements/seed-element
A breakdown of parts of above command is provided below.
bower installinstructs bower to install a dependency--saveadd the dependency inbower.jsonfile. Runbower initif you don't have the file.PolymerElements/seed-elementinstructs bower to findseed-eleementrepository in the organizationPolymerElementsongithub.com. Refer docs for more information.
Now you can use the server of your choice to serve the bower_components directory.
I prefer a simple http server called http-server, it can be installed and used with following
commands.
npm install -g http-server
cd <your application root directory>
http-server
Now you might wonder how would you go about developing your own components? or you want to clone an existing component and develop that? Following steps will outline the workflow for develop.
- Clone an existing component or create a new one following the directory structure of
seed-element. Note that you can also use theyeomangenerator provided by Polymer team for creating a new element. - Run
bower installto install the dependencies listed inbower.jsonfile. BothdependenciesanddevDependenciesare installed. - Install the
polyserveusing commandnpm i -g polyserve. - Run the command
polyservein the root directory of your component.
Note the need to use polyserve to browse the component. Each component references
its dependencies as if they exist in the same directory as the component. This is true
when the component is consumed in an application, but when developing the component all
its dependencies will installed in a directory bower_components located in the root
of the component repository. This is a problem, if you use http-server to serve the
component instead of polyserve the browser will throw dependency not found errors.