Skip to content

Commit c4a6a02

Browse files
authored
Update to latest copy-paste of node's ESM resolve implementation (#1167)
* add raw/* copy of latest node's latest esm resolve.js for convenient diffing * auto-applied patch; needs manual application of rejected hunks * Merge latest dist-raw from node v15.3.0 * replace optional chaining operator with legacy replacement compatible with node 13 * fix broken import * fix botched merge * More copy-pasting from node's source code; add test coverage for reading package.json exports field
1 parent c11aa8a commit c4a6a02

21 files changed

+1530
-283
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
node_modules/
1+
/node_modules/
2+
/tests/node_modules/
23
.nyc_output/
34
coverage/
45
.DS_Store

dist-raw/node-cjs-loader-utils.js

+8-22
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
// Each function and variable below must have a comment linking to the source in node's github repo.
44

55
const path = require('path');
6-
const fs = require('fs');
6+
const packageJsonReader = require('./node-package-json-reader');
7+
const {JSONParse} = require('./node-primordials');
78

89
module.exports.assertScriptCanLoadAsCJSImpl = assertScriptCanLoadAsCJSImpl;
910

1011
// copied from Module._extensions['.js']
11-
// https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770/lib/internal/modules/cjs/loader.js#L1211-L1217
12+
// https://github.com/nodejs/node/blob/v15.3.0/lib/internal/modules/cjs/loader.js#L1113-L1120
1213
function assertScriptCanLoadAsCJSImpl(filename) {
1314
const pkg = readPackageScope(filename);
1415
// Function require shouldn't be used in ES modules.
@@ -41,31 +42,27 @@ function readPackageScope(checkPath) {
4142
// Copied from https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770/lib/internal/modules/cjs/loader.js#L249
4243
const packageJsonCache = new Map();
4344

44-
// Copied from https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770/lib/internal/modules/cjs/loader.js#L251-L283
45+
// Copied from https://github.com/nodejs/node/blob/v15.3.0/lib/internal/modules/cjs/loader.js#L275-L304
4546
function readPackage(requestPath) {
4647
const jsonPath = path.resolve(requestPath, 'package.json');
4748

4849
const existing = packageJsonCache.get(jsonPath);
4950
if (existing !== undefined) return existing;
5051

51-
const json = internalModuleReadJSON(path.toNamespacedPath(jsonPath));
52+
const result = packageJsonReader.read(jsonPath);
53+
const json = result.containsKeys === false ? '{}' : result.string;
5254
if (json === undefined) {
5355
packageJsonCache.set(jsonPath, false);
5456
return false;
5557
}
5658

57-
// TODO Related to `--experimental-policy`? Disabling for now
58-
// if (manifest) {
59-
// const jsonURL = pathToFileURL(jsonPath);
60-
// manifest.assertIntegrity(jsonURL, json);
61-
// }
62-
6359
try {
64-
const parsed = JSON.parse(json);
60+
const parsed = JSONParse(json);
6561
const filtered = {
6662
name: parsed.name,
6763
main: parsed.main,
6864
exports: parsed.exports,
65+
imports: parsed.imports,
6966
type: parsed.type
7067
};
7168
packageJsonCache.set(jsonPath, filtered);
@@ -77,17 +74,6 @@ function readPackage(requestPath) {
7774
}
7875
}
7976

80-
// In node's core, this is implemented in C
81-
// https://github.com/nodejs/node/blob/e9f293750760d59243020d0376edf242c9a26b67/src/node_file.cc#L845-L939
82-
function internalModuleReadJSON(path) {
83-
try {
84-
return fs.readFileSync(path, 'utf8')
85-
} catch (e) {
86-
if (e.code === 'ENOENT') return undefined
87-
throw e
88-
}
89-
}
90-
9177
// Native ERR_REQUIRE_ESM Error is declared here:
9278
// https://github.com/nodejs/node/blob/2d5d77306f6dff9110c1f77fefab25f973415770/lib/internal/errors.js#L1294-L1313
9379
// Error class factory is implemented here:

dist-raw/node-errors.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
exports.codes = {
2+
ERR_INPUT_TYPE_NOT_ALLOWED: createErrorCtor('ERR_INPUT_TYPE_NOT_ALLOWED'),
3+
ERR_INVALID_ARG_VALUE: createErrorCtor('ERR_INVALID_ARG_VALUE'),
4+
ERR_INVALID_MODULE_SPECIFIER: createErrorCtor('ERR_INVALID_MODULE_SPECIFIER'),
5+
ERR_INVALID_PACKAGE_CONFIG: createErrorCtor('ERR_INVALID_PACKAGE_CONFIG'),
6+
ERR_INVALID_PACKAGE_TARGET: createErrorCtor('ERR_INVALID_PACKAGE_TARGET'),
7+
ERR_MANIFEST_DEPENDENCY_MISSING: createErrorCtor('ERR_MANIFEST_DEPENDENCY_MISSING'),
8+
ERR_MODULE_NOT_FOUND: createErrorCtor('ERR_MODULE_NOT_FOUND'),
9+
ERR_PACKAGE_IMPORT_NOT_DEFINED: createErrorCtor('ERR_PACKAGE_IMPORT_NOT_DEFINED'),
10+
ERR_PACKAGE_PATH_NOT_EXPORTED: createErrorCtor('ERR_PACKAGE_PATH_NOT_EXPORTED'),
11+
ERR_UNSUPPORTED_DIR_IMPORT: createErrorCtor('ERR_UNSUPPORTED_DIR_IMPORT'),
12+
ERR_UNSUPPORTED_ESM_URL_SCHEME: createErrorCtor('ERR_UNSUPPORTED_ESM_URL_SCHEME'),
13+
}
14+
15+
function createErrorCtor(name) {
16+
return class CustomError extends Error {
17+
constructor(...args) {
18+
super([name, ...args].join(' '))
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)