Skip to content

Precompiled handlebars template - #26021

Closed
PVince81 wants to merge 2 commits into
masterfrom
handlebars-approach
Closed

PVince81 wants to merge 2 commits into
masterfrom
handlebars-approach

Conversation

@PVince81

@PVince81 PVince81 commented Sep 2, 2016

Copy link
Copy Markdown
Contributor

Description

Introduce Makefile rule to build handlebars templates for every app in core, if applicable.
It compiles all templates from "apps/$APP/templates/*.handlebars" into a single JS file "apps/$APP/js/compiled-templates.js" that must be excluded from version control.

As an example, the template of the DetailsView in the files app has been ported to an external handlebars template file.

Related Issue

Fixes #12848

Motivation and Context

See #12848

How Has This Been Tested?

Run make clean then make test-js. All tests will pass.
Do a manual test in the files UI and see that the right sidebar still works.

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Some concerns

  • I'm not sure whether it's ok to have the template building instructions inside the main Makefile. I thought about having one Makefile per app instead, but could cause a bigger deal of redundancy.
  • Not sure if the location "js/compiled-templates.js" is ok. But can't put it inside a build dir since it won't be loadable by addScript()
  • Had to use a generic namespace name "OC.template[appName][templateName]" because we can't guess the app's JS namespace from outside (having a separate makefile per app could help)
  • If one forgets to run "make build-apps", the templates are not refreshed, might need some kind of watcher...

Please let me know what you think @DeepDiver1975 @butonic @jvillafanez @VicDeo

@PVince81 PVince81 added this to the 9.2 milestone Sep 2, 2016
@DeepDiver1975

Copy link
Copy Markdown
Contributor

youre-awesome

@DeepDiver1975

Copy link
Copy Markdown
Contributor

👍

@jvillafanez

Copy link
Copy Markdown
Member

❗ Documentation required alert ❗

  • From what I see, the templates must be in the "templates" folder of each app. If I put a ".handlebars" file inside a directory ("templates/only-admin/settings.handlebars", for example), that file won't be compiled. If it can't be done easily, this should be documented.
  • The template is assigned from the OC.templates.files.detailsview variable in the example. Following the code changes I can figure out that you'll need a "detailsview.handlebars" files in the template directory of the app, but this might be hidden in a pile of code. This is another candidate to be documented.
  • As you've said, this only applies to core apps. I don't know if there are plans to extends this to all apps, but this needs to be clear.

@PVince81

PVince81 commented Sep 6, 2016

Copy link
Copy Markdown
Contributor Author

@jvillafanez thanks for your comment. This is indeed a bit obscure and I'm not too happy with the approach at the moment. Any suggestion to make it more explicit or better ?

@jvillafanez

Copy link
Copy Markdown
Member

I think one problem is that the app code relies on core to have the templates compiled. If somehow the templates aren't compiled, the app will likely crash.

It's probably better if the app just use a core function to compile the template on-the-fly. An OC.template.compileHandlebars('template') core function, for example. At the first stage, the function just compiles the template on-the-fly (the same way the app could be doing), but then the function could use already-compiled templates if available.
The coupling should be minimum because the function can be reused in any app that is using handlebars.

Now, when we should compile the templates?. Taking the easiest approach, we could add an occ command (or something similar) to compile the templates of an specific app. Then the OC.template.compileHandlebars('template_file') core function will prioritize the compiled file over the raw template. This means that whether the compiled file is up-to-date or not is admin's responsability.

Some advantages over the current approach:

  • All the apps can safely use the function even though the templates aren't compiled. We could add an additional parameter to the function to force the usage of the raw template instead of the compiled one (maybe for debugging)
  • All the apps can take advantage of the template compilation, not just a few core ones.

PD: I'm thinking in a js approach, fetching the template by using an ajax call. I'm not sure if this can be "converted" to PHP.

@PVince81

PVince81 commented Sep 6, 2016

Copy link
Copy Markdown
Contributor Author

fetching the template by using an ajax call.

@jvillafanez this is the main problem here. Fetching with ajax is asynchronous, and most apps aren't and except the code to just "be there" when they are loaded.

There is however a super-dirty possibily for this: if we assume that runtime compilation of templates is only used at development time, we could afford to use synchronous ajax calls. Note that these hang the browser...

I agree with @jvillafanez that the fallback is important, so I think next time I touch this I'll try with the synchronous ajax calls to fetch template code.

@DeepDiver1975

Copy link
Copy Markdown
Contributor

so I think next time I touch this I'll try with the synchronous ajax calls to fetch template code.

well - this will result in some/many additional calls to load the template - I prefer to have them in js where we have the capability to minify and concat ....

@PVince81

PVince81 commented Sep 6, 2016

Copy link
Copy Markdown
Contributor Author

additional calls to load the template

the call would only be done if the template isn't there yet. If precompiled, then the template exists so no load call needed. And this situation is only in dev environments.

On productive envs we expect the templates to all be precompiled/minified.

@jvillafanez

Copy link
Copy Markdown
Member

What about something like https://github.com/owncloud/core/blob/master/apps/files_external/js/statusmanager.js#L128 ? Fetch the template, cache it and call a callback / promise?

@DeepDiver1975

Copy link
Copy Markdown
Contributor

And this situation is only in dev environments.

it's acceptable for devs to ask them ti run make ... in addition we could think about a watch command which runs tasks based on file changes - see https://github.com/owncloud/contacts/blob/master/Makefile#L176-L183

@DeepDiver1975

Copy link
Copy Markdown
Contributor

needs rebase

@PVince81

PVince81 commented Sep 6, 2016

Copy link
Copy Markdown
Contributor Author

Rebased, but needs further work.
I'm not yet happy to merge this.

Both @DeepDiver1975 @jvillafanez and have good points and there are open questions about:

  • where to store the template source code, always "templates" folder ?
  • where to store the compiled template, always "js" mixed with others ? (not too happy here)
  • should core apps have their own Makefiles or rely on core's Makefile to build their templates ? (external apps are supposed to have their own Makefiles anyway, this here is about core apps from git)
    • core makefile cannot guess template namespaces, so having it separate could help with this
    • having separate makefiles would duplicate makefile code, but core could provide includes "include build/core.mk"
  • to add or not to add a fallback ? (at least I agree to add a public utility JS method to get the template, just in case we want to override it some day)

@PVince81

Copy link
Copy Markdown
Contributor Author

I give this Makefile thing another thought and I think that often we'll have core apps have their own bower.json to include app-specific libraries (for ex: upload lib for files). So it makes sense to have one Makefile per core app.
This also means that every app is free to store and compile its handlebars templates however they want.

@PVince81

Copy link
Copy Markdown
Contributor Author

Okay, I've pushed the following changes:

  • the files app now has its own Makefile that compiles its handlebars templates
  • handlebars templates are stored in apps/files/js/templates/*.handlebars and so are the compiled ones as *.handlebars.js
  • the main Makefile has a new command "build-apps" and "clean-apps" that will iterate over all apps and run their makefiles if there is one
  • added "build-core" target, unused for now but will be useful once "core" and "settings" have makefiles too
  • added make watch command which rebuilds apps when a file changed. This means that if you edit the handlebars template it will automatically recompile, so refresh the browser and it's there!

@jvillafanez @DeepDiver1975 what do you think ?

@jvillafanez

Copy link
Copy Markdown
Member

The only thing that bugs me a bit is that we lose control of whatever the app is doing with the Makefile. I'm not sure if this is good or bad.

On the one hand we have the main purpose of this PR which is compile handlebars templates. This is something that core can easily do for any installed app without adding the burden to the app. On the other hand we have a "free to use" Makefile for each file, and "free to use" includes "evil".

In addition, I'm not sure if we want to create the burden for the developers to create and maintain their own Makefile. The good point is that this is optional.

@PVince81

Copy link
Copy Markdown
Contributor Author

This only applies to core developers, not separate apps.

As for the burden of Makefiles, we could provide an include file "app-rules.mk" which contains most of the common rules for apps like compiling templates, running bower.

Maybe I'll do that right away in this PR so it's ready. For that I'll change additional templates in:

  • files_sharing
  • core
  • settings

this way we have enough examples.

As for the "freedom", we still review PRs to core so we can make sure that people don't do crazy things with the makefiles in the core apps.

@PVince81

Copy link
Copy Markdown
Contributor Author

There was a discussion last week and we decided that every app needs its own Makefile, even the bundled ones. So the Makefile from code should just call $(MAKE) on every app's subfolder.

I'd still like to find a way to reuse Makefile code between apps using .mk files, but not sure where to put them. The same problem exists for every app repo out there.

@PVince81

Copy link
Copy Markdown
Contributor Author

Rebased and adjusted.

So now what we have:

  • every app can have a Makefile, ex: apps/files/Makefile
  • app's Makefile must also have "clean" and "dist" and put their dist into apps/$appname/dist/$appname for core to pick up
  • app's make must have the app runnable with current code (ex: handlebars template is loaded from the JS dir still, but copied over on dist)
  • core's make dist picks up all dist folders
  • core only considers known apps as specified in makefile variable, so if anyone has third party apps in their dev env, these are ignored

To be discussed for the future (separate PRs):

  • should we run only core tests or have core's make test run every app's tests separately ?
  • find a way to reuse rules (ex: "build/rules/handlebars.mk")
  • compile "core" and "settings" in the same way once we start using handlebars there

If tests pass we can likely merge this. Then we need to check that the daily build still works correctly before backporting.

@DeepDiver1975 @jvillafanez review ?

@PVince81

Copy link
Copy Markdown
Contributor Author

also cc @patrickjahns as this is interesting from the general build perspective

Using detailsview as an example
@PVince81
PVince81 force-pushed the handlebars-approach branch from d10324f to b484155 Compare December 18, 2017 11:20
@codecov

codecov Bot commented Dec 19, 2017 •

Copy link
Copy Markdown

Codecov Report

Merging #26021 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master   #26021      +/-   ##
============================================
+ Coverage     60.56%   60.56%   +<.01%     
  Complexity    18395    18395              
============================================
  Files          1091     1091              
  Lines         60980    60981       +1     
============================================
+ Hits          36935    36936       +1     
  Misses        24045    24045
Impacted Files Coverage Δ Complexity Δ
apps/files/lib/Controller/ViewController.php 88.97% <100%> (+0.08%) 26 <0> (ø) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 554f606...e953a6c. Read the comment docs.

@PVince81 PVince81 closed this Feb 15, 2018
@PVince81
PVince81 deleted the handlebars-approach branch February 15, 2018 09:24
@lock

lock Bot commented Aug 1, 2019

Copy link
Copy Markdown

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock Bot locked as resolved and limited conversation to collaborators Aug 1, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handlebars + JS unit testing issue

4 participants