Skip to content

Support ES2015 modules in boot scripts #173

@BerkeleyTrue

Description

@BerkeleyTrue

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;
    }
  });
 // ....

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions