Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Support building anonymous + named define AMD modules #337

Closed
finom opened this issue Oct 2, 2015 · 12 comments
Closed

Support building anonymous + named define AMD modules #337

finom opened this issue Oct 2, 2015 · 12 comments

Comments

@finom
Copy link

finom commented Oct 2, 2015

After I've built the project via buildStatic I've got an error in JS file:

Uncaught Module 2 not present.

What I am trying to do is to include to the project a source of MatreshkaJS framework. Config:

map: {
        matreshka_dir: 'lib/matreshka',
        matreshka: 'lib/matreshka/matreshka',
        balalaika: 'lib/matreshka/matreshka',
        xclass: 'lib/matreshka/matreshka',
}

File lib/matreshka/matreshka contains definitions of named modules:

define('matreshka', [
    'matreshka_dir/matreshka.class',
    'matreshka_dir/matreshka-object.class',
    'matreshka_dir/matreshka-array.class'
], function(MK, MK_Object, MK_Array, MK_binders) {
    return MK;
});

define('balalaika', ['matreshka_dir/core/dom-lib/balalaika-extended'], function($b) {
    return $b;
});

define('xclass', ['matreshka_dir/xclass'], function(Class) {
    return Class;
});

There isnot anonymous module definition. After all I can import named modules by the app:

import MK from 'matreshka';
import $ from 'balalaika';

It works great in development. But after compile I'm getting the error described above. The module 'lib/matreshka/matreshka isn't presented in resulting file.

I tried to modify lib/matreshka/matreshka by adding anonymous definition:

define(['matreshka'], function(MK) {
    return MK;
});

And this module appeared in compiled JS but I've got almost the same error:

Uncaught Module matreshka_dir/matreshka.class not present.

This logic worked fine with RequireJS builder.

I hope somebody looks at the issues.

@guybedford
Copy link
Member

Named defines do not support bundling as they are already bundles. I would suggest ensuring you bundle anonymous defines.

@finom
Copy link
Author

finom commented Oct 3, 2015

@guybedford that's sad: I have to create 4 more files for uncommon use. Ideally I'd like to see an error or warning about bad logic when I use client-side SystemJS. There are too many differences between builder and SystemJS, it's annoying.

@guybedford
Copy link
Member

@finom certainly we could construct a suitable error message in this case.

@finom
Copy link
Author

finom commented Oct 7, 2015

@guybedford it looks like there is a bug.
I import a file which contains one anonymous definition and few named definitions:

define(function() {
  return 'foo';
});

define('bar', function() {
  return 'bar';
});

define('baz', function() {
  return 'baz';
});

But still getting "Uncaught Module 2 not present."

@guybedford
Copy link
Member

@finom yes we do not support loading mixed bundle and anonymous modules like that. If you run it dynamically with SystemJS in the browser you should get a better error message.

@finom
Copy link
Author

finom commented Oct 7, 2015

@guybedford but what should I do with libraries which use the same logic (one default definition and some named definitions).

One weird thing for me is that library is included in a bundle but don't work.

@guybedford
Copy link
Member

Try ensuring that all the defines are named to load it as a bundle and then require the name you want after loading the bundle.

Alternatively, use the version of the library that loads as a proper module.

@finom
Copy link
Author

finom commented Oct 7, 2015

@guybedford

Try ensuring that all the defines are named to load it as a bundle and then require the name you want after loading the bundle.

Sorry, I don't know what do you mean. Can you give me an example or a link? Do you mean I need to use 2 files on a page (bundle + library)?

Alternatively, use the version of the library that loads as a proper module.

It sounds like you think that this is not a bug. Requirejs + r.js optimizer works with such stuff very well.

@finom
Copy link
Author

finom commented Oct 7, 2015

If I understood correctly, if one of the files contains named definition (eg for internal purpose), the builder will not work. Which means the builder doesn't work with named definitions at all, right?

@finom
Copy link
Author

finom commented Oct 7, 2015

Ugh. I've tried to fix this by removing named modules but it still throws "Uncaught Module 2 not present.". Then I've removed all library code and kept only UMD stuff and there was no error. UPD: #344

@finom
Copy link
Author

finom commented Oct 7, 2015

I've fixed it by this hacky trick:

var d = "define";
define(function() {
    return 'foo';
});
if(__root[d]) {
    __root[d]('bar', function() {
       return 'bar';
    });
    __root[d]('baz', function() {
       return 'baz';
    });
}

It's fine because I can make changes to my library. In other libraries anonymous+named modules will not work.

@guybedford guybedford changed the title Uncaught Module X not present. Support building anonymous + named define AMD modules Oct 7, 2015
@guybedford
Copy link
Member

This has been implemented and released in Released in 0.15.10.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants