-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support es modules without .mjs extension #34
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
*.tgz | ||
node_modules | ||
!tests/fixtures/**/node_modules | ||
*.log | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = { | ||
'unknown bundle': 'Unknown bundle "%s"', | ||
'unknown config': 'Unknown config "%s" in bundle "%s"', | ||
'unknown cache data': 'Unknown cache data with config "%s" in bundle "%s"', | ||
'missing dimensions': 'Failed to find a dimensions.json file', | ||
'parse error': 'Failed to parse "%s"\n%s', | ||
'missing time': 'No time dimension, %s, in context, %s, during time aware mode' | ||
}; |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
var utils = require('./utils'); | ||
|
||
/** | ||
* Import a CommonJS config | ||
* @param {String} id module name or path | ||
* @param {Function} [callback] Called once the config has been added to the helper. | ||
* @param {Error|null} callback.err If an error occurred, then this parameter will | ||
* contain the error. If the operation succeeded, then `err` will be null. | ||
* @param {Object} callback.contents The contents of the config file, as a | ||
* JavaScript object. | ||
*/ | ||
module.exports = function(id, callback) { | ||
try { | ||
var mod = utils.interopDefault(require(id)); | ||
callback(null, mod); | ||
} catch (e) { | ||
callback(new utils.ParseError(e, id)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const { interopDefault, ParseError } = require('./utils'); | ||
|
||
/** | ||
* Import an ESModule config. | ||
* @param {String} id module name or path | ||
* @param {Function} [callback] Called once the config has been added to the helper. | ||
* @param {Error|null} callback.err If an error occurred, then this parameter will | ||
* contain the error. If the operation succeeded, then `err` will be null. | ||
* @param {Object} callback.contents The contents of the config file, as a | ||
* JavaScript object. | ||
*/ | ||
module.exports = async (id, callback) => { | ||
try { | ||
const mod = interopDefault(await import(id)); | ||
callback(null, mod.default); | ||
} catch (e) { | ||
callback(new ParseError(e, id)); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This file and the one above it are intentionally formatted to look nearly identical. The only real difference between the two is that when doing a dynamic import we have to dereference the |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
var MESSAGES = require('../messages'); | ||
var util = require('util'); | ||
|
||
/** | ||
* Return the export of a CJS module or default export of a transpiled ESModule | ||
* See https://babeljs.io/docs/en/babel-plugin-transform-modules-commonjs | ||
* @param {Object} mod module | ||
* @returns {*} exported module content | ||
*/ | ||
module.exports.interopDefault = function interopDefault(mod) { | ||
if (mod && mod.__esModule) { | ||
return mod.default; | ||
} | ||
return mod; | ||
} | ||
|
||
module.exports.ParseError = (function () { | ||
function ParseError(e, id) { | ||
this.message = util.format(MESSAGES['parse error'], id, e.message) | ||
} | ||
ParseError.prototype = new Error; | ||
return ParseError; | ||
})(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/yahoo/ycb-config/pull/34/files?w=1