Re: Contribute requirejs.config.paths to ModuleManager

"Thiago H. de Paula Figueiredo" <[email protected]>
Newsgroups gmane.comp.java.tapestry.user
Message-ID <CAE_88GYg2AXobsRh+OdwEsoKhGCLBKYCkGk1yNF=Pp7puafLBg@mail.gmail.com>
On Fri, Nov 24, 2017 at 11:13 AM, Svein-Erik Løken <[email protected]> wrote:

Hello!

Trying to use TypeScript and React with Tapestry:
>
> In TypeScript I need to use:  import * as React from "react";
>
> I would like to load react: react.min, so I have to set paths: {...}
>
> The configuration should be set at startup, but I cannot figure how to do
> it. I can set requirejs.config.shim, but not paths.
>

Are you using React in all pages or just some of them?


>
> I tried to load it with @Contribute(ModuleManager.class), but could not
> figure out how.
>
> PSEUDO CODE
> @Contribute(ModuleManager.class)
> public static void setupMyBaseModules(MappedConfiguration<String, Object>
> configuration) {
>     JSONObject pathsConfig = new JSONObject(
>             "react", "react.min",
>             "react-dom", "react-dom.min"
>     );
>     AMDResourceConfiguration paths = new AMDResourceConfiguration("paths",
> pathsConfig);
>     configuration.add(null, new JavaScriptModuleConfiguration(paths));
> }
>

MappedConfiguration, as far as I can remember, doesn't accept null as the
contribution id. Even if it does, it's a bad idea.


> Tried to load it this way:
> @Startup
> public void registerToClearPlasticProxyFactoryOnInvalidation(JavaScriptSupport
> javaScriptSupport) {
>     ModuleConfigurationCallback callback = new
>             ModuleConfigurationCallback() {
>                 @Override
>                 public JSONObject configure(JSONObject configuration) {
>                     configuration.put("paths",
>                             new JSONObject(
>                                     "react", "react.min",
>                                     "react-dom", "react-dom.min"
>                             ));
>                     return configuration;
>                 }
>             };
>     javaScriptSupport.addModuleConfigurationCallback(callback);
> }



> [ERROR] ioc.Registry org.apache.tapestry5.ioc.util.UnknownValueException:
> No object of type org.apache.tapestry5.services.javascript.JavaScriptSupport
> is available from the Environment.


I'm afraid this isn't the correct way of using
JavaScriptSupport.addModuleConfigurationCallback(). It should be used while
rendering a page or handling an event. The way you did it above, you're
trying to add JavaScript at application startup. You need to do this in
page or component classes.


> Now I am loading the configuration in @SetupRender, but it should be
> loaded globally.
>

Define "globally", please. All pages, including error ones? Most pages? The
better way of doing this is moving this code to a mixin and apply it to the
pages where you need React. If you want it in all pages, you can contribute
a ComponentClassTransformWorker2 to the service with the same name which
applies the mixin to all pages. See
MutableComponentModel.addMixinClassName().

-- 
Thiago
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.