Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLIP: Reset define in RequireJS after bundles loaded #1529

Closed
vangheem opened this issue Apr 14, 2016 · 10 comments
Closed

PLIP: Reset define in RequireJS after bundles loaded #1529

vangheem opened this issue Apr 14, 2016 · 10 comments

Comments

@vangheem
Copy link
Member

vangheem commented Apr 14, 2016

Proposer : vangheem

Seconder : bloodbare

Abstract

Reset the requirejs define global variable in Plone so add-on developers do not have to learn how to use requirejs if they are including js that expect requirejs to be used.

Motivation

We have integrators that face Uncaught Error: Mismatched anonymous define errors and do not know how to deal with it. While this is documented in RequireJS and there are answers on stackoverflow, it is still annoying and difficult to deal with as an integrator.

Also see: https://community.plone.org/t/third-party-js-add-ons-inside-one-template/1950

Assumptions

  • RequireJS still be used for js dependency resolution in plone

Proposal & Implementation

  • Set define to undefined after the built resources are included
  • Implementation would be in CMFPlone, simply something like this would be added after the resources:
<script>
plone_define = define;
define = undefined;
</script>

Deliverables

  • a PR with this integrated into Products.CMFPlone

Risks

This breaks behavior with normal requirejs usage. Developers will need to change how they are using requirejs in their projects if they are using it without the built bundles as define will no longer be supported OOTB outside of plone's bundles.

@zopyx
Copy link
Member

zopyx commented Apr 15, 2016

+1 for any solution that would allow us to use (in general arbitrary) JS add-ons in Plone.

@rnixx
Copy link
Member

rnixx commented Apr 15, 2016

There has been a similar workaround in plone with jquery by providing "jq" pointing to "jQuery". People then just started to wrap their JS in anonymous functions.

It might be an option to apply this pattern with require as well

(function(define, require) {
  require([
    'mockup-pattern-autotoc'
  ], function(AutoTOC){
    // something
  });
})(plone_define, plone_require);

It would be an option to generate the wrapper around bundles if desired.

So there's no real code changes needed in recent bundles, no "require" and "define" by default and a clear way to write requirejs using code.

@vangheem vangheem changed the title PLIP: Namespace RequireJS use WIP: PLIP: Namespace RequireJS use Apr 26, 2016
@vangheem vangheem changed the title WIP: PLIP: Namespace RequireJS use WIP: PLIP: Reset undefined in RequireJS after bundles loaded Apr 26, 2016
@vangheem vangheem changed the title WIP: PLIP: Reset undefined in RequireJS after bundles loaded WIP: PLIP: Reset define in RequireJS after bundles loaded Apr 26, 2016
@vangheem vangheem changed the title WIP: PLIP: Reset define in RequireJS after bundles loaded PLIP: Reset define in RequireJS after bundles loaded Apr 26, 2016
@ebrehault
Copy link
Member

Does it really have to be a PLIP?
In my opinion, it does not highly impact how Plone works, a PR should be enough.

@pigeonflight
Copy link
Member

+100

@tisto
Copy link
Sponsor Member

tisto commented Jul 5, 2016

+1

@ebrehault
Copy link
Member

@vangheem The PLIP has been approved by the Framework Team.

@jensens
Copy link
Sponsor Member

jensens commented Jul 14, 2016

Is there any implementation/branch already around?

@rnixx
Copy link
Member

rnixx commented Jul 17, 2016

If the title of this PLIP applies it's not what I was suggesting ;)

I was thinking about wrapping each bundle if desired but the "core" bundle, loading the JS basics and immediately undefining define and require.

@thet
Copy link
Member

thet commented Jul 19, 2016

Recently I unset require and define for a JavaScript block in a theme, which is located at the very end of the HTML document. After the JS block I re-set it again.

I got a lot of "define is not a function" errors.

I thought it could have been due to the async behavior, that some JS was just loaded while the browser has unset define. But that's probably unlikely.
Probably it was due to some JS errors in the script (but the console didn't show any) and re-defining define and require couldn't happen anymore.

Whatever the case was, when AMD tried to load its resources, require and define weren't available. I removed the whole block and registered it as legacy JS resources, which worked fine.

Isn't it likely that the same happens when implementing this PLIP?

@jensens
Copy link
Sponsor Member

jensens commented May 21, 2019

I think @thet adressed the issue long time ago and the plip can be closed. Reopen if your of a different opinion and start working on it...

@jensens jensens closed this as completed May 21, 2019
@jensens jensens moved this from In Process (approved) to Merged in PLIPs May 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Merged
PLIPs
Merged
Development

No branches or pull requests

9 participants