How to structure large Polymer projects with 100+ elements - polymer
The Polymer Starter Kit is a good reference to start a Polymer project. You simply put all your elements into the app/elements folder. This works pretty neat for small to midsize projects.
It becomes messy when you have more than ~30 elements. Then you want to refactor the flat elements folder into a deeper folder structure like this one:
- elements
-- my-module-1
--- my-element-1-1
--- my-element-1-2
-- my-module-2
--- my-submodule-2-1
---- my-element-2-1-1
---- my-element-2-1-2
--- my-submodule-2-2
---- ...
There are a couple of issues:
when you want to demo and test submodules you need to import all dependencies above every element definition
you break the pattern of "all elements are siblings"
your relative paths become messy (lots of ../../../my-module-x/my-module-y)
you either have lots of paths like ../../../../bower_components or you use /bower_components, then you need to have a redirect in your dev-server and the absolute paths will mess up gulp-vulcanize.
With a demo and test folder for every element your directory structure grows very quickly
Here is a nice article describing the problem and pointing out two solutions:
Separate Elements Repository
Building Reusable Elements
Separate Elements Repository like in the Topeka App works for ~30 elements, but once you are up to 100+ elements, you run into the same issues.
Building Reusable Elements seems like a good idea at first, because you can encapsulate everything nicely. But working on hundreds of repos is painful, and the standard pattern breaks when you want to have more than one single element in your repo.
So I am wondering, what are good practices on how to build large Polymer apps?
Are there any examples of projects with way more than 30 elements?
What are good practices for reusable elements repos that contain multiple elements?
What is a good structure for multiple entry points?
In general: How do you scale Polymer projects?
We have a production application serving up 100's of elements. We found it useful to group multiple elements inside folders to cut down on the number of repos and folders. We still make all elements siblings.
There is a bit of precedent for this inside Google's own elements. If you take a look at app-layout and polymerfire, they both contain multiple custom elements.
The mind shift is to remember that not every element should exist in it's own directory.
It is useful to your issue to think about the difference between the file structure and the url structure. Making the web server map the files to the same place. This is what polyserve does, and you can configure wct to set up its server like it too.
So, for instance, when you are creating a single element to test, its normally sitting directly in the project directory, but the web server is mapping this parent directly directly into /components. Its doing the same with bower_components, so they appear at browser level to be at the same place. That is why references like ../polymer/polymer.html work
I'm thinking in the scenario you propose above, that you repeatedly do something similar at each submodule level so that each element can reference polymer, or its other web components at ../polymer/polymer.html
The net result would be an overall project "URL" structure that looked like
-components (elements mapped to this as well as bower_components)
--mymodule1
---(mapped so all directories under bower_components are mapped in here)
---myelement1.1
---myelement1.2
--mymodule2
---(mapped so all directories under bower_components are mapped in here)
---myelement2.1
---myslement2,2
IMO the key to organize Polymer elements goes all the way to designing the solution to problems / pages. Think of designing Lego pieces for a particular model. Yes, you can build many pieces that have very specific functions, but it will be better to focus on re-usability and design fewer pieces that will cover most of the structure (base elements) and then add some touch up pieces (touch-up elements) to finish the model. Base elements should be simple and very generic.
While the touch-up elements are not likely to be recycled, they can stay with the page. Base elements on the other hand should be placed in a folder very close to the root with very descriptive names. As development progresses, the rate of new base elements will decrease as well as time needed on developing new pages.
If the design is correct, then how you categorize elements doesn't matter, just to place them in the bower_components folder unless you are building public polymer elements. Everything in bower_components should be controlled by bower.json so you will delete the whole folder and use bower install to re-populate it.
Related
Can you have 2 app.css files or do you have to combine?
is it possible to have a website that contains 2 app.css files? I have a website and want to build a new (but related) website under the same domain. So I guess you can consider the main hub more like a landing page of sorts, and then the two related "websites" would then branch off of the main domain. Each of the websites are going to be styled differently though. So I'm unsure of what is best - to create a single combined app.css file and list each style according to each website, or can I create a new one that sits beside the original?
You can do a separate css file for each site, or have them both point to the same file. Since you want different styling between the 2 sites, I'd recommend a different css file for each site.
You can create and use as many css file as you want. If both sites will be style differently, create a file (or even multiple files, them import) for each site.
Though the websites are related, it's better to create two separate css files as it will be easy to make changes(in future) to styles and debug incase of any issues. It also increases readability of the code so that in future if you may have to refer this code for any modifications, you can understand it easily and make required changes :)
Has anyone tried to use jekyll to generate dashboard pages?
I'm using static html pages and incline js to generate dashboard. The backend is go. However, as number of pages growing, I have to maintain a lot of html pages, and many of the block elements are duplicated. I'm wondering whether there're some cases to use jekyll to generate dashboard pages, since jekyll can convert pieces of elements into complete htmls? So that I can modify one part and the modification take effect on every html pages that generated. Are there better framework to do so? Thank you!
If you're duplicating elements, then those are the perfect pieces of markup to put into a layout and/or include. That's exactly what those are for. Each individual page you need can reference a specific layout in its front matter. Includes can be used within the individual pages or within the layouts or both (and can be nested). The Jekyll Documentation is ok for this, but to wrap your head around it I find it easiest to look at the documentation together with a few existing Jekyll templates to understand how it all fits together.
after a bit more dig. I found that packages such as Grunt and Gulp can be used to generate htmls from separate parts.
Explaining the polymer designer code - The simple file system
I have been working on polymer/designer off late. I wanted to mess around, add more components etc. I am still learning polymer and I need some help. I need to know the difference between the components and the elements. I know what components are. Clone this: https://github.com/Polymer/designer Run it locally. I need to understand how the code works for this. Check the components and the elements folders. And also the index file. A simple explanation is to how it is all connected and rendered etc.
As far as I understand it components are the building blocks for elements. As you can see in the elements folder you need at least one component for your element. In general I think the term might be interchangeable since components have dependencies on other components as well so they are actually elements as well. I think you should worry too much about the terminology :)
Design Pattern for PolymerJS
Currently i have seen many links and videos of polymer and I am very exciting about polymer but the only thing which keeps me doubtful is the performance problem with using polymer in current date for example if i import 5 external components with each one having a link to external css which means i have to make 5 http request for css files .So what design pattern should I follow to increase the web performance should I remove css links from imported components and concatenate all css files into one file and use on my main page.
When it comes time to optimize for production, you can use the vulcanizer to concatenate imports and move your stylesheets inline, or perform various other transformations (e.g. forcing all JavaScript to be external for CSP).
Relative Paths for Includes in HTML? A Throwback To The Old Days?
[This question is somewhat related to this question, but the answers are not...] I have always used relative paths in HTML and scripting languages (PHP/ASP/JSP) to refer to EVERYTHING. I think the justification had to do with 'what if the website gets hosted in some weird subdirectory.' But my coworker has started throwing absolute paths into a PHP site we're working on. At first I was appalled, but then I thought, "why not? We'll never be hosted in a subdirectory." Nowadays, getting hosted in the root is not an uncommon necessity. Is it still necessary to "code" (markup, really) with relative paths? I think it's probably an antiquated practice by now.
Definitely. You never know where your code is going to end up, or for what purpose. Suppose you build a new version of a site, but to help your users transition, you move the old version to a "/classic" subdirectory. The same often happens the other way around where sites will host a beta version of the new redesign in a "/new" directory. Building it properly the first time shouldn't be a hassle, and you'll thank yourself for it one day.
Well, it's probably not the end of the world but it's building in a dependency that you don't need. If for example you ever want to put a number of versions in subdirectories in a test harness, or subdirectories help to work around some other issue (such as combining this with some other app that insists on being in the root), you may find it harder. Sure, you can probably always work around it in some other way - but the problem is you now have to.
Edit: Killed my opening paragraph as it was based on a miss-interpretation of the referenced article. I always use an absolute path for the following reasons: Includes: 1. When you use a templating system, you never know the directory structure that a document is going to have when it's calling the include. 2. If documents are going to be moved around, you can be pretty sure it's going to be the documents themselves rather than the includes. If it is the includes, then they're being moved my someone who knows what going on. Added to this, if it is someone not familiar with absolute/relative path, they'll understand absolute a lot better than relative. css: (as well as the items above) 1. When editing with Web Developer, relative backgrounds disappear when your style sheet isn't in the same directory as the doc. 2. By spec, when you put something on the 'Net, it should be there for the duration. When I'm building something new, I build it in a new folder, or with a new file name and leave all the old docs alone.
If you're using dynamically-generated pages, dynamically generate your URLs. For example, with JSTL use <c:url>. Not only does this allow you to move your pages in the future, it also ensures that your URLs will be properly escaped (tell the truth: how often do you build query strings with &? if you want the W3C validator to accept them, you need to). For my personal website, which uses PHP to build pages, I've created several methods: insertPageRef(), insertCodeRef(), and so on, that create valid HREFs. As a result, I don't hesitate to re-arrange my site structure as it evolves.
The case of being moved to a subdirectory is fairly unusual, I'd say. Far more usual is being hosted on different servers: you create your site for testing on "dev.yourcompany.com" then go live by moving it to "www.yourcompany.com". In that case, you need to use relative links for obvious reasons.