-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support es modules without .mjs extension
- Loading branch information
Showing
10 changed files
with
78 additions
and
68 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains 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
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)); | ||
} | ||
} |
This file contains 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
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)); | ||
} | ||
} |
This file contains 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
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; | ||
})(); |
This file contains 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
This file contains 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