-
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
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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
default
export. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#importing_defaults