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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate moduleNames for AMD/UMD #176

Merged
merged 1 commit into from
Nov 16, 2014
Merged

Generate moduleNames for AMD/UMD #176

merged 1 commit into from
Nov 16, 2014

Conversation

darvelo
Copy link
Contributor

@darvelo darvelo commented Nov 16, 2014

references #158, references #44

This PR allows for generating user-defined module names for AMD and UMD.

Users can define a root prefix for each module, which allows for more flexible application routing (e.g. js-client/moduleA), and can strip the source file's directory root so that module names can be relative (e.g. module name is moduleA rather than /home/user/myApp/es6/moduleA).

This meant a couple of new user-defined opts, which for now I've named:

  • sourceRoot - the JS source directory to be stripped from the path
  • moduleRoot - an optional prefix for each module. I've got a couple of lines for gulp-6to5 for this part that I will submit a PR for if these changes look good to go. For gulp-6to5 the changeset would be something like this:
try {
  var fileOpts = objectAssign({}, opts, {
    filename: file.path,
+    filenameRelative: file.relative,
    sourceMap: !!file.sourceMap
  });

With sourceRoot: __dirname + '/js' in gulpfile.js and moduleRoot: 'client', you'll see something like this:

Modules

// /root/js/moduleA
import moduleB from 'client/moduleB';

export default function () {
    console.log('moduleA! ' + moduleB());
}
// /root/js/moduleB
import moduleC from 'client/subdir/moduleC';

export default function () {
    return 'moduleB! ' + moduleC();
}
// /root/js/subdir/moduleC
export default function () {
    return 'moduleC!';
}

Output

define("client/moduleA", ["exports", "client/moduleB"], function (exports, _clientModuleB) {
  "use strict";

  var moduleB = _clientModuleB["default"];
  exports["default"] = function () {
    console.log("moduleA! " + moduleB());
  };
});
define("client/moduleB", ["exports", "client/subdir/moduleC"], function (exports, _clientSubdirModuleC) {
  "use strict";

  var moduleC = _clientSubdirModuleC["default"];
  exports["default"] = function () {
    return "moduleB! " + moduleC();
  };
});
define("client/subdir/moduleC", ["exports"], function (exports) {
  "use strict";

  exports["default"] = function () {
    return "moduleC!";
  };
});

index.html

<script>require('client/moduleA').default()</script> // 'moduleA! moduleB! moduleC!'

I'm not totally sure how sourcemaps work here so I haven't done anything on that front yet.

Let me know what you think! 馃槈

sebmck added a commit that referenced this pull request Nov 16, 2014
Generate moduleNames for AMD/UMD
@sebmck sebmck merged commit dadab64 into babel:master Nov 16, 2014
@sebmck
Copy link
Contributor

sebmck commented Nov 16, 2014

Looks good! Thanks! 馃憤

Sorry about those houndci comments that I've since deleted, I forgot I added it.

@darvelo
Copy link
Contributor Author

darvelo commented Nov 16, 2014

Wow @sebmck, thanks for merging! I've added a bunch more documentation to this thread so if you need me to do anything more please let me know. I'm still working my way through the codebase. I see the build is failing so I'll have to update the tests, but it's 3AM here so I need some sleep first. 馃樃

@sebmck
Copy link
Contributor

sebmck commented Nov 16, 2014

Let me know if you have any questions (https://gitter.im/6to5/6to5), happy to have extra contributors! Pushed out a new release 1.12.17 I updated the tests/documentation and added sourceRoot and filenameRelative support to source maps.

@darvelo
Copy link
Contributor Author

darvelo commented Nov 16, 2014

@sebmck Awesome, happy to pitch in where I can. Thanks for the amazing work.

@mattma
Copy link

mattma commented Nov 17, 2014

@darvelo Thank you very much. It is truly awesome!

@darvelo
Copy link
Contributor Author

darvelo commented Nov 17, 2014

@mattma Glad it works for you! @sebmck gave it the interop and polish it needed, so hats off to him too. 馃帺

@mattma mattma mentioned this pull request Nov 23, 2014
sebmck pushed a commit that referenced this pull request Mar 16, 2015
ramasilveyra pushed a commit to ramasilveyra/babel that referenced this pull request Sep 30, 2017
Add test for invalid electron version
existentialism pushed a commit that referenced this pull request Oct 5, 2017
Add test for invalid electron version
JacopKane pushed a commit to JacopKane/babel that referenced this pull request Jan 11, 2018
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 9, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants