Skip to content

Instantly share code, notes, and snippets.

@dherman
Last active September 24, 2015 22:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dherman/c35e968991b67ae98423 to your computer and use it in GitHub Desktop.
Save dherman/c35e968991b67ae98423 to your computer and use it in GitHub Desktop.

Module metadata

API

  • import.named: dynamic relative import of namespace object
  • import(...): dynamic relative import of default export
  • import.context: loader-specific contextual information about this module
    • loader may want to expose canonicalized name
    • loader may want to provide package-relative path info
    • Node: filename, dirname, maybe module and exports

Examples

Dynamic Relative Loading

Dynamically load a relative module when it's only needed rarely:

// lib/index.js

configButton.onclick = async function() {
  // corresponds to: `import { readFile, writeFile } from 'fs';`
  let { readFile, writeFile } = await import.named('fs');
  // corresponds to: `import config from './config.js';`
  let config = await import('./config.js');
  ...
};

Context-Sensitive Metadata

Compute a Node project's root directory path:

import { resolve } from 'path';

let root = resolve(import.context.dirname, '..');
...
@littledan
Copy link

Proposed: s/import(/import.default(/ so that import.default and import.named refer to first-class functions which close over their scope.

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