Skip to content

Instantly share code, notes, and snippets.

@kirbysayshi
Last active August 29, 2015 14:28
Show Gist options
  • Save kirbysayshi/587b758734d8ae129b71 to your computer and use it in GitHub Desktop.
Save kirbysayshi/587b758734d8ae129b71 to your computer and use it in GitHub Desktop.
Is any variant of this possible in ES6 modules / destructuring?

I have a big fat object that is structured like this (using CJS format to remove all ambiguity):

// big-fat-package.js
module.exports = {
  runtime: {
    compile: function() {},
    partials: {
      'non-valid-identifier-property-0': function() {},
      'non-valid-identifier-property-1': function() {},
      'non-valid-identifier-property-2': function() {},
      'non-valid-identifier-property-3': function() {},
      'non-valid-identifier-property-n': function() {}
    }
  }
}

In a CJS world, to import a single partial I'd do this:

var tpl0 = require('big-fat-package').runtime.partials['non-valid-identifier-property-0'];

And multiple, something like:

var partials = require('big-fat-package/runtime').runtime.partials
var tpl0 = partials['non-valid-identifier-property-0'];
var tpl1 = partials['non-valid-identifier-property-1'];

So far so good. But is it possible to do this using ES6 import aliasing? Something like (note: this isn't valid syntax)...

import { runtime as { partials as { 'non-valid-identifier-property-0' as tpl0 } } } from 'big-fat-package';

Seems like it's impossible to:

  • alias a property more than one level deep using ES6 import.
  • reference a non-valid identifier within an import.

Is this true, or just my lack of experience?

maybe?

The shortest, yet perhaps most incomprehensible due to right-to-left, appears to be:

import { runtime } from 'big-fat-package';
const {
  'non-valid-identifier-property-0': tpl0,
  'non-valid-identifier-property-1': tpl1
} = runtime.partials;

Anyone have better ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment