Skip to content

Instantly share code, notes, and snippets.

@jkrems
Last active December 9, 2017 03:49
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 jkrems/ad42a0e2e8a156670239286b5fab0807 to your computer and use it in GitHub Desktop.
Save jkrems/ad42a0e2e8a156670239286b5fab0807 to your computer and use it in GitHub Desktop.
dynamic-import-require
node_modules
require('./b.js');
// Object.assign to force same output between node master and @std/esm
import('./c.mjs').then(ns => console.log(Object.assign({}, ns)));
// just exports an empty namespace
{
"name": "esm-indirect-dynamic-import",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@std/esm": {
"version": "0.18.0",
"resolved": "https://npm.groupondev.com:443/api/npm/npm-release/@std/esm/-/esm-0.18.0.tgz",
"integrity": "sha1-4hK1Zcdl+Tsp7FIqSToZLOeuK6Y="
}
}
}
{
"name": "esm-indirect-dynamic-import",
"version": "1.0.0",
"description": "",
"main": "a.js",
"scripts": {
"test": "node test.js"
},
"repository": {
"type": "git",
"url": "git+ssh://git@gist.github.com/ad42a0e2e8a156670239286b5fab0807.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://gist.github.com/ad42a0e2e8a156670239286b5fab0807"
},
"homepage": "https://gist.github.com/ad42a0e2e8a156670239286b5fab0807",
"dependencies": {
"@std/esm": "^0.18.0"
}
}
'use strict';
const assert = require('assert');
const execFile = require('child_process').execFile;
function mustCall(fn) {
let wasCalled = false;
process.on('exit', () => {
if (!wasCalled) throw new Error('should have called function');
});
return (...args) => {
wasCalled = true;
return fn(...args);
};
}
execFile(process.execPath, [
'-r', '@std/esm', 'b.js',
], {
cwd: __dirname,
}, mustCall((err, stdout) => {
if (err) throw err;
assert.strictEqual('{}\n', stdout);
}));
execFile(process.execPath, [
'-r', '@std/esm', 'a.js',
], {
cwd: __dirname,
}, mustCall((err, stdout) => {
if (err) throw err;
assert.strictEqual('{}\n', stdout);
}));
@jkrems
Copy link
Author

jkrems commented Dec 9, 2017

For reference using latest node nightly:

> node --version
v10.0.0-nightly201712084e65f9d504

> node --experimental-modules --harmony a.js
(node:16387) ExperimentalWarning: The ESM module loader is experimental.
{}

> node --experimental-modules --harmony b.js
(node:16399) ExperimentalWarning: The ESM module loader is experimental.
{}

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