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

Problems with "module" special dependency #104

Closed
sprat opened this issue Dec 4, 2015 · 3 comments
Closed

Problems with "module" special dependency #104

sprat opened this issue Dec 4, 2015 · 3 comments

Comments

@sprat
Copy link

sprat commented Dec 4, 2015

I've found some problems when trying to the module special dependency in order to access the module.config().

  1. Module configs variable name clash

Here is an example:

define(['module'], function (module) {
    'use strict';

    return {
        config: module.config()
    };
});

This code is converted to:

var modular4, module = {
    'modular4': {
      'config': function () {
        return { ... };
      }
    }
  };
modular4 = function (module) {
  'use strict';
  return { config: module.config() };
}(module['modular4']);
window.modular4 = modular4;

The module configs are stored in a module variable. It's a pretty bad variable name, as it can break shimmed modules included in the same file. In fact, many libraries have a universal module definition in top of them so that the same code can work in node (CJS) and in the browser (AMD), and they often test the availability of a module variable to detect the module format.

I would suggest renaming this variable to something else... maybe moduleConfigs. Another idea would be to store the configs of each module in the module's variable itself, as in this example:

var modular4 = {
      'config': function () {
        return { ... };
      }
    }
  };
modular4 = function (module) {
  'use strict';
  return { config: module.config() };
}(modular4);
window.modular4 = modular4;
  1. Wrapped CJS modules are completely broken

As an example:

define(function (require, exports, module) {
    'use strict';

    var config = module.config();

    exports.config = config;
});

The code above is converted to:

var modular1 = {}, module = {
    'modular1': {
      'config': function () {
        return { ... };
      }
    }
  };
modular1 = function (exports) {
  'use strict';
  var config = module.config();
  exports.config = config;
  return exports;
}({}, {}, module['modular1']);
window.modular1 = modular1;

This is obviously wrong.

I can probably submit a pull request solving the first point. The second one is more tricky, I'm not sure I can solve the problem by myself.

@sprat
Copy link
Author

sprat commented Dec 5, 2015

Ok, I just made a pull request solving problem 1. Note that I chose the "modules" variable name instead of "moduleConfigs", because I think we can solve #92 by putting all the modules exports in this object instead of declaring one (global) variable per AMD module.

@sprat
Copy link
Author

sprat commented Dec 7, 2015

I think the problem is due to the blacklisting of 'module' dependency', so I propose to generate CJS modules like this:

var amd_modules={
  'my_module': {
    exports: {}
    config: function() {
      return { /*...*/ };
    }
  }
};
amd_modules['my_module'] = function (require, exports, module) {
  /* ... no transforms of module/exports here */
  return exports;
}({},amd_modules['my_module'].exports, amd_modules['my_module']);

If we agree on the output format, I can give it a try.

gfranko added a commit that referenced this issue Feb 9, 2016
@gfranko
Copy link
Owner

gfranko commented Feb 9, 2016

You can use the prefixTransform option to prefix your variables how you like.

@gfranko gfranko closed this as completed Feb 9, 2016
ozouai-6sense pushed a commit to 6si/amdclean that referenced this issue Apr 12, 2022
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

No branches or pull requests

2 participants