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

Remove default ".json" and ".node" resolution for ESM #3

Closed
wants to merge 4 commits 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
3 changes: 1 addition & 2 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ For now, only modules using the `file:` protocol can be loaded.

## Interop with existing modules

All CommonJS, JSON, and C++ modules can be used with `import`.
CommonJS and C++ modules can be used with `import`.

Modules loaded this way will only be loaded once, even if their query
or fragment string differs between `import` statements.
Expand Down Expand Up @@ -176,7 +176,6 @@ module. This can be one of the following:
| `'esm'` | Load a standard JavaScript module |
| `'cjs'` | Load a node-style CommonJS module |
| `'builtin'` | Load a node builtin CommonJS module |
| `'json'` | Load a JSON file |
| `'addon'` | Load a [C++ Addon][addons] |
| `'dynamic'` | Use a [dynamic instantiate hook][] |

Expand Down
2 changes: 0 additions & 2 deletions lib/internal/modules/esm/default_resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ function search(target, base) {
const extensionFormatMap = {
'__proto__': null,
'.mjs': 'esm',
'.json': 'json',
'.node': 'addon',
'.js': 'cjs'
};

Expand Down
35 changes: 1 addition & 34 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@
const { NativeModule, internalBinding } = require('internal/bootstrap/loaders');
const { ModuleWrap } = internalBinding('module_wrap');
const {
stripShebang,
stripBOM
stripShebang
} = require('internal/modules/cjs/helpers');
const CJSModule = require('internal/modules/cjs/loader');
const internalURLModule = require('internal/url');
const createDynamicModule = require(
'internal/modules/esm/create_dynamic_module');
const fs = require('fs');
const { _makeLong } = require('path');
const { SafeMap } = require('internal/safe_globals');
const { URL } = require('url');
const { debuglog, promisify } = require('util');
const readFileAsync = promisify(fs.readFile);
const readFileSync = fs.readFileSync;
const StringReplace = Function.call.bind(String.prototype.replace);
const JsonParse = JSON.parse;

const debug = debuglog('esm');

Expand Down Expand Up @@ -74,32 +70,3 @@ translators.set('builtin', async (url) => {
reflect.exports.default.set(module.exports);
});
});

// Strategy for loading a node native module
translators.set('addon', async (url) => {
debug(`Translating NativeModule ${url}`);
return createDynamicModule(['default'], url, (reflect) => {
debug(`Loading NativeModule ${url}`);
const module = { exports: {} };
const pathname = internalURLModule.fileURLToPath(new URL(url));
process.dlopen(module, _makeLong(pathname));
reflect.exports.default.set(module.exports);
});
});

// Strategy for loading a JSON file
translators.set('json', async (url) => {
debug(`Translating JSONModule ${url}`);
return createDynamicModule(['default'], url, (reflect) => {
debug(`Loading JSONModule ${url}`);
const pathname = internalURLModule.fileURLToPath(new URL(url));
const content = readFileSync(pathname, 'utf8');
try {
const exports = JsonParse(stripBOM(content));
reflect.exports.default.set(exports);
} catch (err) {
err.message = pathname + ': ' + err.message;
throw err;
}
});
});
13 changes: 0 additions & 13 deletions test/addons/hello-world-esm/binding.cc

This file was deleted.

9 changes: 0 additions & 9 deletions test/addons/hello-world-esm/binding.gyp

This file was deleted.

20 changes: 0 additions & 20 deletions test/addons/hello-world-esm/test.js

This file was deleted.

6 changes: 0 additions & 6 deletions test/addons/hello-world-esm/test.mjs

This file was deleted.

8 changes: 0 additions & 8 deletions test/es-module/test-esm-json.mjs

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/es-modules/json.json

This file was deleted.