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

Use requirejs vs. require #1165

Merged
merged 6 commits into from
Dec 7, 2017
Merged

Use requirejs vs. require #1165

merged 6 commits into from
Dec 7, 2017

Conversation

gnestor
Copy link
Contributor

@gnestor gnestor commented Nov 30, 2017

@jcb91
Copy link
Member

jcb91 commented Nov 30, 2017

Thanks @gnestor. Most of these I think actually don't need changing, since the require they use is actually a local variable provided by the define call's require dependency, so they should be unaffected by jupyter/notebook#155. The exceptions are toc2 & collapsible_headings, which use require.specified in the global context. Unfortunately, I think altering all of the instances would be a little more complicated than just using requirejs everywhere, since 'require' specified as a dependency for a define call gets handled as a 'special' module (see require.js#L573-L579 and require.js#L1157-L1162), and this wouldn't be the case if 'requirejs' is specified...

@@ -41,7 +41,7 @@ define(function(require, exports, module) {
"postfix": ", output=FALSE)[['text.tidy']], collapse='\n')))"
},
"javascript": {
"library": "jsbeautify = require(" + "'js-beautify')",
"library": "jsbeautify = requirejs(" + "'js-beautify')",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure about this, it's a javascript kernel which is running this require call, so it likely doesn't want requirejs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Correct.

@gnestor
Copy link
Contributor Author

gnestor commented Dec 1, 2017

@jcb91 Thanks for the feedback. I haven't used requirejs or amd outside of the notebook project, so I don't understand the intricacies. I'm totally open to suggestions about how to best update this repo to reflect the changes in jupyter/notebook#155.

fix unintended change to skill keywords.
Third-party code from/adapted from codemirror and jquery should function ok on node anyway, because they actually test for

    typeof define == "function" && define.amd

then use define (ratehr than require), which is what we're aiming for.
jcb91 added a commit to jcb91/nbextension-scratchpad that referenced this pull request Dec 4, 2017
* use requirejs over require variable name to avoid confusion on node.
  See jupyter/notebook#155 for details.

* don't use the requirejs simplified commonjs wrapper
  ipython-contrib/jupyter_contrib_nbextensions#1165
  which is slower than a dependency array, and relies on toString parsing.
  See ipython-contrib/jupyter_contrib_nbextensions#1165
  for details.
jcb91 added a commit to jcb91/nbextension-scratchpad that referenced this pull request Dec 4, 2017
* use requirejs over require variable name to avoid confusion on node.
  See jupyter/notebook#155 for details.

* don't use the requirejs simplified commonjs wrapper
  ipython-contrib/jupyter_contrib_nbextensions#1165
  which is slower than a dependency array, and relies on toString parsing.
  See ipython-contrib/jupyter_contrib_nbextensions#1165
  for details.
jcb91 added a commit to jcb91/nbextension-scratchpad that referenced this pull request Dec 4, 2017
* use requirejs over require variable name to avoid confusion on node.
  See jupyter/notebook#155 for details.

* don't use the requirejs simplified commonjs wrapper
  ipython-contrib/jupyter_contrib_nbextensions#1165
  which is slower than a dependency array, and relies on toString parsing.
  See ipython-contrib/jupyter_contrib_nbextensions#1165
  for details.
jcb91 added a commit to jcb91/nbextension-scratchpad that referenced this pull request Dec 4, 2017
* use requirejs over require variable name to avoid confusion on node.
  See jupyter/notebook#155 for details.

* don't use the requirejs simplified commonjs wrapper
  http://requirejs.org/docs/api.html#cjsmodule
  which is slower than a dependency array, and relies on toString parsing.
  See ipython-contrib/jupyter_contrib_nbextensions#1165
  for details.
jcb91 added a commit to jcb91/nbextension-scratchpad that referenced this pull request Dec 4, 2017
* use requirejs over require variable name to avoid confusion on node.
  See jupyter/notebook#155 for details.

* don't use the requirejs simplified commonjs wrapper
  http://requirejs.org/docs/api.html#cjsmodule
  which is slower than a dependency array, and relies on toString parsing.
  See ipython-contrib/jupyter_contrib_nbextensions#1165
  for details.
jcb91 added a commit to jcb91/nbextension-scratchpad that referenced this pull request Dec 4, 2017
* use requirejs over require variable name to avoid confusion on node.
  See jupyter/notebook#155 for details.

* don't use the requirejs simplified commonjs wrapper
  http://requirejs.org/docs/api.html#cjsmodule
  which is slower than a dependency array, and relies on toString parsing.
  See ipython-contrib/jupyter_contrib_nbextensions#1165
  for details.
as it works by searching through the module definition function's toString representation, the commonjs style requirejs wrapper is slower, and prone to subtle errors. We don't actually need it anywhere, so use regular requirejs define calls instead.
jcb91 added a commit to jcb91/jupyter_highlight_selected_word that referenced this pull request Dec 4, 2017
@jcb91
Copy link
Member

jcb91 commented Dec 4, 2017

@gnestor no problem.

I haven't used requirejs or amd outside of the notebook project, so I don't understand the intricacies

Nor have I really, but I've been bitten by a few of its intricacies in the commonjs wrapper style that got me to dig into how it behaves 😆

For almost all cases, I think we could get away with just doing nothing, given require is a local variable set as a function argument. But, I agree that switching to using requirejs as the variable name (as you've done) is probably a good idea, just to reduce confusion in comparison to notebook usage.

I've made a few fixes which I've pushed into this PR branch, hope that's ok. Essentially they are

  1. to revert changes to third-party code items (codemirror & jqueryui modules, each of which have their own idioms for dealing with requirejs satisfactorily)

  2. to revert a couple of inadvertent changes (requireModules got replaced with requirejsModules, which won't work).

  3. Finally, there's one more 'magic' implementation detail which can bite here, which relates to the aforementioned commonjs wrapper define style. Explanation below...

The simplified commonjs wrapper is used when you have a call without explicit dependencies, like

define(function (require, exports, module) {
    var other_module = require('./my_custom_module');  // synchronous require call
}); 

in this format, requirejs actually searches the toString content of the definition function for calls like require('module_id_string') to infer dependencies that need to be available to the module. But, (because of the regex it uses), it won't find any using requirejs('module_id_string'), so in order to use the cjs style, we'd need to keep the locally-defined variable name as require. I think though, that the best option for nbextensions in such cases is simply to not use the commonjs style at all, since we don't actually need it, and it's more work & bug-prone for requirejs to do the string parsing anyway. I discovered this after trying to do something like

var my_dependency = './my_files/' + some_other_var;
require(my_dependency);

which also doesn't match the requirejs regex 🐛

@gnestor
Copy link
Contributor Author

gnestor commented Dec 6, 2017

Thanks for making these changes, @jcb91!

in such cases is simply to not use the commonjs style at all

Does this require further refactoring then? Should it be done in this PR?

@jcb91
Copy link
Member

jcb91 commented Dec 7, 2017

@gnestor np! I think I've covered everything for this repo in this PR already in d668614 - there were only a few instances anyway.

There are a couple of dependencies which should also be dealt with, but they'll be handled in their respective repos anyway (e.g. minrk/nbextension-scratchpad#19, jfbercher/jupyter_latex_envs, jcb91/jupyter_highlight_selected_word#22).

In summary, I think this is ok to merge as-is, if anything breaks (hopefully not super likely!), we should hear about it soon enough before any pip/conda releases.

@jcb91 jcb91 merged commit ac92192 into ipython-contrib:master Dec 7, 2017
@gnestor gnestor deleted the requirejs branch December 7, 2017 00:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants