Skip to content

Commit

Permalink
Enable explicit .m.js intent for ESM
Browse files Browse the repository at this point in the history
As discussed in the [.mjs extension trade-offs](nodejs/node-eps#57 (comment)) post, it would be great if NodeJS could provide a way to explicitly opt-in as ESM, without needing to use a different extension.

The purpose of this PR is to enable a universal convention for ESM that would work out of the box in both browsers and other JavaScript environment including SpiderMonkey and JSC.

If the file is imported with a fully qualified path name, and such path name uses the `.m.js` convention, the format will be ESM and it will throw if such file does not respect such format.

The current NodeJS diversion from the rest of the JavaScript environments is alarming for various reasons and consistency, as well as code reliability, is currently potentially compromised, as described in details in [this blog post](https://codeburst.io/the-javascript-modules-limbo-585eedbb182e).

If there is anything else I could do in order to land this PR while ESM is still behind an experimental flag, please let me know, thank you.
  • Loading branch information
WebReflection committed Oct 13, 2017
1 parent 668f4cf commit b8239d2
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 2 deletions.
5 changes: 4 additions & 1 deletion doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ and implementation is ready. Error messages are still being polished.
The `--experimental-modules` flag can be used to enable features for loading
ESM modules.

Once this has been set, files ending with `.mjs` will be able to be loaded
Once this has been set, files ending with `.mjs` or `.m.js` will be able to be loaded
as ES Modules.

```sh
node --experimental-modules my-app.mjs

node --experimental-modules other-app.m.js
```

## Features
Expand All @@ -41,6 +43,7 @@ points into ESM graphs at run time.
| Feature | Reason |
| --- | --- |
| `require('./foo.mjs')` | ES Modules have differing resolution and timing, use language standard `import()` |
| `require('./foo.m.js')` | same as above, use language standard `import()` |
| `import()` | pending newer V8 release used in Node.js |
| `import.meta` | pending V8 implementation |
| Loader Hooks | pending Node.js EP creation/consensus |
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/loader/ModuleRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ exports.resolve = (specifier, parentURL) => {
case '.node':
return { url: `${url}`, format: 'addon' };
case '.js':
return { url: `${url}`, format: 'cjs' };
const format = url.pathname.slice(-5) === '.m.js' ? 'esm' : 'cjs';
return { url: `${url}`, format };
default:
throw new errors.Error('ERR_UNKNOWN_FILE_EXTENSION',
internalURLModule.getPathFromURL(url));
Expand Down
3 changes: 3 additions & 0 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,9 @@ Module.prototype._compile = function(content, filename) {

// Native extension for .js
Module._extensions['.js'] = function(module, filename) {
if (experimentalModules && filename.slice(-5) === '.m.js') {
return Module._extensions['.mjs'](module, filename);
}
var content = fs.readFileSync(filename, 'utf8');
module._compile(internalModule.stripBOM(content), filename);
};
Expand Down
5 changes: 5 additions & 0 deletions test/es-module/test-es-m-basic-imports.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Flags: --experimental-modules
import assert from 'assert';
import ok from './test-esm-ok.m.js';

assert(ok === true);
9 changes: 9 additions & 0 deletions test/es-module/test-es-m-failing-require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Flags: --experimental-modules
const assert = require('assert');

try {
require('./test-esm-ok.m.js');
assert(false);
} catch(error) {
assert(/es\s*m(?:odule)?|\.m\.?js/i.test(error.message));
}
5 changes: 5 additions & 0 deletions test/es-module/test-esm-ok.m.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Flags: --experimental-modules
/* eslint-disable required-modules */

const isJs = true;
export default isJs;

0 comments on commit b8239d2

Please sign in to comment.