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

requirejs amd fix #388

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

requirejs amd fix #388

wants to merge 1 commit into from

Conversation

qpi
Copy link

@qpi qpi commented Apr 27, 2016

if I define the rangy in the requirejs's main.js file like this:

shim: {
    'rangy-selectionsaverestore': {
        deps: ['rangy']
    },
}
paths: {
    'rangy': 'vendor/rangy/lib/rangy-core',
    'rangy-selectionsaverestore': 'vendor/rangy/lib/rangy-selectionsaverestore'
}

In my code I use it like this:

define(['rangy', 'rangy-selectionsaverestore'], function(rangy) {
// here the rangy is available with the selectionsaverestore module
});

The rangy-selectionsaverestore has dependency on the rangy. So we can not use the AMD as it is righ now, becasuse it forces the define(["./rangy-core"], factory); call, which is wrong in this case becase set the path in the main.js
Please test and merge my fix, thanks

Copy link

@lddubeau lddubeau left a comment

Choose a reason for hiding this comment

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

Your configuration does not do what you think it does. shim is only for code that does not call define. If the module for which you define a shim calls define then the shim is ignored.

That rangy-selectionsaverestore refers to "./rangy-core" may be inconvenient because if you refer to Rangy as rangy you're going to have to provide some configuration to tell RequireJS that rangy and rangy-core are the same, but it is not wrong. The proper way to configure RequireJS to handle the fact that your project refers to the rangy-core module as rangy would be to configure it this way:

require.config({
  paths: {
    "rangy": "https://cdnjs.cloudflare.com/ajax/libs/rangy/1.3.0/rangy-core",
    "rangy-selectionsaverestore": "https://cdnjs.cloudflare.com/ajax/libs/rangy/1.3.0/rangy-selectionsaverestore",
  },
  map: {
    "rangy-selectionsaverestore": {
      "rangy-core": "rangy"
    },
  },
});

Here's a plunker.

And see the comment I made on the code in the PR. This PR would turn an inconvenience into code that is flat out wrong.

@@ -166,7 +166,12 @@ function copyModuleScripts() {
'(function(factory, root) {',
' if (typeof define == "function" && define.amd) {',
' // AMD. Register as an anonymous module with a dependency on Rangy.',
' define(["./rangy-core"], factory);',
' if ( require && require.s.contexts._.defined.rangy ) {',
Copy link

Choose a reason for hiding this comment

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

No way.

require.s is private to RequireJS. If you want to mess with it in your own code, that's on you. But making widely-used library mess with RequireJS' private parts is a non-starter.

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