Skip to content
This repository has been archived by the owner on Dec 8, 2024. It is now read-only.

Latest version breaks template-compiler usage in the browser #350

Open
boris-petrov opened this issue Feb 28, 2021 · 9 comments
Open

Latest version breaks template-compiler usage in the browser #350

boris-petrov opened this issue Feb 28, 2021 · 9 comments
Labels

Comments

@boris-petrov
Copy link

Using version 4.4.1 leads to the following when running ember test:

tests/helpers/templates.ts: hbs should be invoked with at least a single argument: the template string
  16 |     return {
  17 |       value: template,
> 18 |       precompiled: JSON.parse(precompileTemplate(template)),
     |                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  19 |     };
  20 |   }
  21 | }

With the import:

import { compileTemplate, precompileTemplate } from '@ember/template-compilation';

4.2.1 works fine.

Using Ember.HTMLBars.compile and Ember.HTMLBars.precompile instead also works.

Related Discord discussion.

cc @rwjblue

@rwjblue
Copy link
Member

rwjblue commented Mar 1, 2021

In order for us to make this work, we have to leave the code untranspiled when given a dynamic value.

@rwjblue rwjblue added the bug label Mar 1, 2021
@pzuraq
Copy link

pzuraq commented Mar 15, 2021

When used dynamically in the browser, users should use compileTemplate instead of precompileTemplate. I don't think the precompileTemplate was ever guaranteed to work in at runtime - the point is that it runs ahead of time.

@rwjblue
Copy link
Member

rwjblue commented Mar 15, 2021

ya, that makes sense

@boris-petrov - Can you double check?

@boris-petrov
Copy link
Author

We use precompile from the template compiler on the backend, send the result to the frontend where we use createTemplateFactory(precompiledTemplate) and create a template-only component that we use. In our frontend (Ember) tests we want to mock the backend's implementation - hence we have to do the precompile part at runtime during the tests. So we need Ember.HTMLBars.precompile (or import { precompileTemplate } from '@ember/template-compilation'; when it still worked a few weeks ago).

@rwjblue
Copy link
Member

rwjblue commented Mar 16, 2021

Hmm, I see. Why not use a middleware (in testing) and have the same "backend compiles the template" workflow? Wouldn't it be much better to avoid having it behave so differently than production?

@boris-petrov
Copy link
Author

Well, we have integration tests for that (which are slow). We want the frontend tests fast, so we don't want to run the backend server. Besides, mocking what the backend returns is literally 2 lines of code. I'm fine with the different behavior in the Ember tests.

@boris-petrov
Copy link
Author

The state as of today is that compileTemplate can be used from the @ember/template-compilation import, however precompileTemplate gives the error from my first post. I guess, because the workaround uses the global Ember variable, that won't work on Ember 4? @rwjblue - what can I do? Or is that just something that you have to fix?

@boris-petrov
Copy link
Author

A little update on that. precompileTemplate still can't be imported from the correct place and on Ember 4 one has to use require('ember').default.HTMLBars.precompile(template).

@boris-petrov
Copy link
Author

Still happens on Ember v5.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants