Skip to content
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

Enable explicit .m.js intent for ESM #16170

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;