Skip to content
This repository has been archived by the owner on Apr 16, 2020. It is now read-only.

Commit

Permalink
esm: remove .js support
Browse files Browse the repository at this point in the history
  • Loading branch information
MylesBorins committed Oct 2, 2018
1 parent 73bdb35 commit 3c75a81
Show file tree
Hide file tree
Showing 19 changed files with 58 additions and 38 deletions.
20 changes: 14 additions & 6 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ property:

## Notable differences between `import` and `require`

### Only Support for .mjs

ESM must have the `.mjs` extension.

### No NODE_PATH

`NODE_PATH` is not part of resolving `import` specifiers. Please use symlinks
Expand Down Expand Up @@ -95,12 +99,17 @@ When loaded via `import` these modules will provide a single `default` export
representing the value of `module.exports` at the time they finished evaluating.

```js
// foo.js
module.exports = { one: 1 };
// cjs.js
module.exports = 'cjs;
// esm.mjs
import { createRequireFromPath as createRequire } from 'module';
import { fileURLToPath as fromPath } from 'url';
const require = createRequire(fromPath(import.meta.url));
// bar.js
import foo from './foo.js';
foo.one === 1; // true
const cjs = require('./cjs');
foo.cjs === 'cjs'; // true
```
Builtin modules will provide named exports of their public API, as well as a
Expand Down Expand Up @@ -174,7 +183,6 @@ module. This can be one of the following:
| `format` | Description |
| --- | --- |
| `'esm'` | Load a standard JavaScript module |
| `'cjs'` | Load a node-style CommonJS module |
| `'builtin'` | Load a node builtin CommonJS module |
| `'dynamic'` | Use a [dynamic instantiate hook][] |

Expand Down
3 changes: 1 addition & 2 deletions lib/internal/modules/esm/default_resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ function search(target, base) {

const extensionFormatMap = {
'__proto__': null,
'.mjs': 'esm',
'.js': 'cjs'
'.mjs': 'esm'
};

function resolve(specifier, parentURL) {
Expand Down
2 changes: 1 addition & 1 deletion src/module_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ using v8::TryCatch;
using v8::Undefined;
using v8::Value;

static const char* const EXTENSIONS[] = {".mjs", ".js"};
static const char* const EXTENSIONS[] = {".mjs"};

ModuleWrap::ModuleWrap(Environment* env,
Local<Object> object,
Expand Down
14 changes: 12 additions & 2 deletions test/common/index.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
// Flags: --experimental-modules
/* eslint-disable node-core/required-modules */
import common from './index.js';

import { createRequireFromPath } from 'module';
import { fileURLToPath as toPath } from 'url';

function createRequire(metaUrl) {
return createRequireFromPath(toPath(metaUrl));
}

const require = createRequire(import.meta.url);
const common = require('./index.js');

const {
isMainThread,
Expand Down Expand Up @@ -97,5 +106,6 @@ export {
getBufferSources,
disableCrashOnUnhandledRejection,
getTTYfd,
runWithInvalidFD
runWithInvalidFD,
createRequire
};
2 changes: 1 addition & 1 deletion test/es-module/test-esm-double-encoding.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ import '../common';

// Assert we can import files with `%` in their pathname.

import '../fixtures/es-modules/test-esm-double-encoding-native%2520.js';
import '../fixtures/es-modules/test-esm-double-encoding-native%2520.mjs';
1 change: 1 addition & 0 deletions test/es-module/test-esm-dynamic-import.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Flags: --experimental-modules

'use strict';
const common = require('../common');
const assert = require('assert');
Expand Down
6 changes: 3 additions & 3 deletions test/es-module/test-esm-error-cache.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict';

// Flags: --experimental-modules

'use strict';

require('../common');
const assert = require('assert');

const file = '../fixtures/syntax/bad_syntax.js';
const file = '../fixtures/syntax/bad_syntax.mjs';

let error;
(async () => {
Expand Down
10 changes: 7 additions & 3 deletions test/es-module/test-esm-require-cache.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Flags: --experimental-modules
import '../common';
import '../fixtures/es-module-require-cache/preload.js';
import '../fixtures/es-module-require-cache/counter.js';
import { createRequire } from '../common';
import assert from 'assert';
//
const require = createRequire(import.meta.url);

require('../fixtures/es-module-require-cache/preload.js');
require('../fixtures/es-module-require-cache/counter.js');

assert.strictEqual(global.counter, 1);
delete global.counter;
6 changes: 4 additions & 2 deletions test/es-module/test-esm-shared-loader-dep.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Flags: --experimental-modules --loader ./test/fixtures/es-module-loaders/loader-shared-dep.mjs
import '../common';
import { createRequire } from '../common';
import assert from 'assert';
import '../fixtures/es-modules/test-esm-ok.mjs';
import dep from '../fixtures/es-module-loaders/loader-dep.js';

const require = createRequire(import.meta.url);
const dep = require('../fixtures/es-module-loaders/loader-dep.js');

assert.strictEqual(dep.format, 'esm');
7 changes: 0 additions & 7 deletions test/es-module/test-esm-snapshot.mjs

This file was deleted.

6 changes: 5 additions & 1 deletion test/fixtures/es-module-loaders/loader-shared-dep.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import dep from './loader-dep.js';
import assert from 'assert';

import {createRequire} from '../../common';

const require = createRequire(import.meta.url);
const dep = require('./loader-dep.js');

export function resolve(specifier, base, defaultResolve) {
assert.strictEqual(dep.format, 'esm');
return defaultResolve(specifier, base);
Expand Down
6 changes: 5 additions & 1 deletion test/fixtures/es-module-loaders/loader-with-dep.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import dep from './loader-dep.js';
import {createRequire} from '../../common';

const require = createRequire(import.meta.url);
const dep = require('./loader-dep.js');

export function resolve (specifier, base, defaultResolve) {
return {
url: defaultResolve(specifier, base).url,
Expand Down
4 changes: 0 additions & 4 deletions test/fixtures/es-modules/esm-snapshot-mutator.js

This file was deleted.

2 changes: 0 additions & 2 deletions test/fixtures/es-modules/esm-snapshot.js

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/es-modules/pjson-main/main.js

This file was deleted.

1 change: 1 addition & 0 deletions test/fixtures/es-modules/pjson-main/main.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'main';
2 changes: 1 addition & 1 deletion test/fixtures/es-modules/pjson-main/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"main": "main.js"
"main": "main.mjs"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
// Trivial test to assert we can load files with `%` in their pathname.
// Imported by `test-esm-double-encoding.mjs`.

module.exports = 42;
export default 42;
1 change: 1 addition & 0 deletions test/fixtures/syntax/bad_syntax.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var foo bar;

0 comments on commit 3c75a81

Please sign in to comment.