Last active
December 9, 2017 03:49
-
-
Save jkrems/ad42a0e2e8a156670239286b5fab0807 to your computer and use it in GitHub Desktop.
dynamic-import-require
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node_modules |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('./b.js'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Object.assign to force same output between node master and @std/esm | |
import('./c.mjs').then(ns => console.log(Object.assign({}, ns))); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// just exports an empty namespace |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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=" | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "esm-indirect-dynamic-import", | |
"version": "1.0.0", | |
"description": "", | |
"main": "a.js", | |
"scripts": { | |
"test": "node test.js" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "git+ssh://[email protected]/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" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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); | |
})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For reference using latest node nightly: