loopback-boot icon indicating copy to clipboard operation
loopback-boot copied to clipboard

Support ES2015 modules in boot scripts

Open BerkeleyTrue opened this issue 10 years ago • 11 comments

We've been using ES2015 modules for a while now using babel to transpile, which worked great until a fix came down the pipe for a transform that technically breaks spec.

In Babel 6, it has been pointed out that previous behavior of a module that only has an default exports is incorrect. While we've moved on to Babel 6, we are using a hacked version of the module transformer so that we can continue using loopback-boot. It would be great if this project supported reading ES2015 modules.

This would involve checking if the __esModule flag and loading the default property.



function runScripts(app, list, callback) {
  list = list || [];
  var functions = [];
  list.forEach(function(filepath) {
    debug('Requiring script %s', filepath);
    try {
      var exports = require(filepath);
      // check for ES2015 modules
      finalFunction = exports && exports.__esModule ? exports.default : exports;
      if (typeof finalFunction === 'function') {
        debug('Exported function detected %s', filepath);
        functions.push({
          path: filepath,
          func: finalFunction
        });
      }
    } catch (err) {
      console.error('Failed loading boot script: %s\n%s', filepath, err.stack);
      throw err;
    }
  });
 // ....

BerkeleyTrue avatar Feb 10 '16 03:02 BerkeleyTrue

I believe @bajtos is the best person to answer this question about our future plans. @bajtos could you please take a look?

Thanks!

Amir-61 avatar Feb 22 '16 19:02 Amir-61

Related reading: http://stackoverflow.com/q/33505992/69868 and https://medium.com/@kentcdodds/misunderstanding-es6-modules-upgrading-babel-tears-and-a-solution-ad2d5ab93ce0#.mrwnw2f67

@BerkeleyTrue sure, I am happy to add a small fix to support Babel/ES2015 users.

exports && exports.__esModule ? exports.default : exports;

It makes me wonder whether this will support other ES2015 transpilers too? Isn't this Babel-specific? I don't mind adding Babel-specific fix, but it would be nicer to support other ES2015 transpilers too. TypeScript compiler would be at the top of the list.

I also think we will need to make this change in multiple places, not only for boot scripts. Model files (common/models/my-model.js) are a good example.

@BerkeleyTrue could you please contribute this enhancement yourself and submit a pull request? We can discuss implementation details there.

bajtos avatar Feb 23 '16 17:02 bajtos

@bajtos That line of code should work for all transpilers that follow ES2015 module exports spec.

I'll see if I can find the time to contribute a PR.

BerkeleyTrue avatar Feb 24 '16 19:02 BerkeleyTrue

This hasn't been done? Should I submit a PR then?

zamb3zi avatar Jun 18 '17 12:06 zamb3zi

@zamb3zi yes please, a pull request will be welcome. You may want to target the 2.x branch, the master branch is tracking the upcoming 3.0 release.

Please check https://github.com/strongloop/loopback-boot/pull/203 first. It aims to add support for TypeScript, but I think it's relevant to ES2015 modules in general.

bajtos avatar Jun 21 '17 09:06 bajtos

@bajtos PR submitted. #203 handles model source files and boot scripts only. #250 encompasses that and adds mixin, component and middleware source files, more unit tests and model and mixin sources in the compiler. I have the same applied to the master branch as well, but there are no unit tests for that one. Should I submit that as is in another PR?

zamb3zi avatar Jun 22 '17 07:06 zamb3zi

By the way, I had problems with the linting of this PR because of interaction with my global eslint and with an unrelated .eslintrc.yml file in my ../.. directory. One solution is to change the package.json lint command to ./node_modules/.bin/eslint . and add "root": true to the .eslintrc file. Unless there's some reason you don't want this to be the root of the config, should I add that to the PR?

zamb3zi avatar Jun 22 '17 08:06 zamb3zi

PR submitted.

Great, thank you! I'll take a look soon.

+1 for setting root: true in our eslint config. Please open a new pull request for that change though.

I don't understand the point of changing lint script in package.json. AFAIK, npm prepends ./node_modules/.bin to PATH before running the scripts, therefore it should invoke the local eslint version. Additionally, I am not sure if ./node_modules/.bin/eslint works on Windows. I vaguely remember that npm uses eslint.cmd and possibly a different directory than .bin on Windows.

bajtos avatar Jun 23 '17 14:06 bajtos

I have the same applied to the master branch as well, but there are no unit tests for that one. Should I submit that as is in another PR?

Sure. We can ask @davidcheung and @raymondfeng to help you to figure out how to test this new feature.

bajtos avatar Jun 23 '17 14:06 bajtos

Yes, you're right about the local path for npm run. I was getting mixed up by the root config problem. I'll do a separate PR on the weekend.

I've updated #250 and I'll submit a master branch PR in the next few days.

zamb3zi avatar Jun 23 '17 15:06 zamb3zi

The feature is available in [email protected]. Enjoy :)

bajtos avatar Jun 26 '17 13:06 bajtos