Skip to content

Suppress error "Mismatched anonymous define()"#1354

Closed
devote wants to merge 2 commits intorequirejs:masterfrom
devote:master
Closed

Suppress error "Mismatched anonymous define()"#1354
devote wants to merge 2 commits intorequirejs:masterfrom
devote:master

Conversation

@devote
Copy link
Copy Markdown

@devote devote commented May 19, 2015

This fix allows you to suppress error "Mismatched anonymous define ()"

It is often necessary to determine the independent modules, for example such as "jQuery". What would be a possibility to declare the module without tying it tightly to a single name.

it is not flexible:

define('jquery', [], function () {});

It is also as above, but more flexibly:

define(null, [], function () {});

For example, if you create a library:

mylib.js:

(function(factory) {
  if (typeof define === 'function' && define.amd) {
    // null - it is also an anonymous module, except that in determining the
    // module out of context requirejs, an error will be suppressed.
    define(null, [], factory());
  } else {
    factory();
  }
})(function() {
  // library code here
  return window.exportObject = exportObject;
});

@jrburke
Copy link
Copy Markdown
Member

jrburke commented May 26, 2015

No plans to suppress this error since it is an indication of a larger problem with setup with the loader: for the resources to be useful in the loader, they should either be loaded with the loader or be built scripts with names in them.

I agree that it is annoying, but suppressing the error will lead to harder to debug issues around project setup that will likely generate support requests and given the subtle nature of the issue will make it hard to provide timely support about it.

@jrburke jrburke closed this May 26, 2015
@devote
Copy link
Copy Markdown
Author

devote commented May 26, 2015

I agree that it is annoying, but suppressing the error will lead to harder to debug issues around project

Not many fewer questions arise about: "Mismatched anonymous define ()"

But because of the lack of such a possibility, to have to write such a horrible code:

(function(factory) {
  if (typeof define === 'function' && define['amd']) {
    var rndKey = '[library' + (new Date()).getTime() + ']';
    var onError = requirejs['onError'];
    factory.toString = function() {
      return rndKey;
    };
    requirejs['onError'] = function(err) {
      if (err.message.indexOf(rndKey) === -1) {
        onError.call(requirejs, err);
      }
    };
    define([], factory);
  } else {
    factory();
  }
})(function() {
  // library code here
  return window.exportObject = exportObject;
});

Do you think such an option you think would be better?

@jrburke
Copy link
Copy Markdown
Member

jrburke commented May 27, 2015

What is the higher level goal you want to accomplish? Is it using the registered 'jquery' module by a different name?

@devote
Copy link
Copy Markdown
Author

devote commented Jun 4, 2015

There is a library https://github.com/devote/HTML5-History-API. Allowed to execute code library in different ways. For example:

Connect using SCRIPT tag:

<script src="history.js"></script>

or connecting via requirejs:

<script src="require.js"></script>
<script>
  require(['/history'], function(history) {
    // ...
  });
</script>

the above two methods work fine, but if you connect as follows:

<script src="require.js"></script>
<script src="history.js"></script>

An error will occur: "Mismatched anonymous define()"

This problem has forced me to write the code that I gave you in my previous comments.

@jrburke
Copy link
Copy Markdown
Member

jrburke commented Jun 5, 2015

For the last one, if you want to use manual script tags, place it before the require.js tag:

<script src="history.js"></script>
<script src="require.js"></script>

Otherwise, the idea with the module system, if you want to use history.js to be loaded as a module, the require(['history']... example is the way to go.

@devote
Copy link
Copy Markdown
Author

devote commented Jun 6, 2015

place it before the require.js tag:

I understand. I am writing here is not because I am struggling with this. That's because the others are constantly faced with this. And ask me questions on this issue.

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