Files
ts-node/dist-raw/node-internal-modules-package_json_reader.js
Andrew BradleyandGitHub 7af22f7a15 Implement file extension resolution for CJS, same as ESM; add exhaustive tests for resolver (#1727)
* WIP

* restore .d.ts file

* add notes and todos

* strip down node-internal-modules-cjs-loader to match main branch; will build it back up from here

* committing all local stuff

* update

* it kinda works!

* fix CI?

* fix?

* fix?

* fix

* bump up to node 18 cuz why not

* test matrix: replace node17 with node18

* fix node12 test failure

* fix for prior hooks API

* tweak

* fix

* teach node environment resetter to re-sort require.extensions

* fix

* fix

* fix

* windows fix?

* Addressing TODOs

* sync another raw file with upstream

* Improve reuse / DI within node ESM stuff

* cleanup node internalBinding stuff

* Adding hint when ts-node is ignoring a file that you might want it to compile; addressing todos; adding a new one

* Add tests for self-imports and empty package.json manifests importing a root index.* file

* fix

* fix node12
2022-05-15 16:11:25 -04:00

45 lines
1.2 KiB
JavaScript

// copied from https://github.com/nodejs/node/blob/v15.3.0/lib/internal/modules/package_json_reader.js
'use strict';
const { SafeMap } = require('./node-primordials');
const { internalModuleReadJSON } = require('./node-internalBinding-fs');
const { pathToFileURL } = require('url');
const { toNamespacedPath } = require('path');
// const { getOptionValue } = require('./node-options');
const cache = new SafeMap();
let manifest;
/**
* @param {string} jsonPath
* @return {{string: string, containsKeys: boolean}}
*/
function read(jsonPath) {
if (cache.has(jsonPath)) {
return cache.get(jsonPath);
}
const [string, containsKeys] = internalModuleReadJSON(
toNamespacedPath(jsonPath)
);
const result = { string, containsKeys };
if (string !== undefined) {
if (manifest === undefined) {
// manifest = getOptionValue('--experimental-policy') ?
// require('internal/process/policy').manifest :
// null;
// disabled for now. I am not sure if/how we should support this
manifest = null;
}
if (manifest !== null) {
const jsonURL = pathToFileURL(jsonPath);
manifest.assertIntegrity(jsonURL, string);
}
}
cache.set(jsonPath, result);
return result;
}
module.exports = { read };