Skip to content

Commit 492b4d4

Browse files
committed
More copy-pasting from node's source code; add test coverage for reading package.json exports field
1 parent eda6039 commit 492b4d4

File tree

15 files changed

+49
-24
lines changed

15 files changed

+49
-24
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
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

Lines changed: 8 additions & 11 deletions
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 {internalModuleReadJSON} = require('./node-internal-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);

dist-raw/node-internal-fs.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
const fs = require('fs');
22

33
// In node's core, this is implemented in C
4-
// https://github.com/nodejs/node/blob/e9f293750760d59243020d0376edf242c9a26b67/src/node_file.cc#L845-L939
4+
// https://github.com/nodejs/node/blob/v15.3.0/src/node_file.cc#L891-L985
55
function internalModuleReadJSON(path) {
6+
let string
67
try {
7-
return fs.readFileSync(path, 'utf8')
8+
string = fs.readFileSync(path, 'utf8')
89
} catch (e) {
9-
if (e.code === 'ENOENT') return undefined
10+
if (e.code === 'ENOENT') return []
1011
throw e
1112
}
13+
// Node's implementation checks for the presence of relevant keys: main, name, type, exports, imports
14+
// Node does this for performance to skip unnecessary parsing.
15+
// This would slow us down and, based on our usage, we can skip it.
16+
const containsKeys = true
17+
return [string, containsKeys]
1218
}
1319

1420
module.exports = {

dist-raw/node-package-json-reader.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const cache = new SafeMap();
1111
let manifest;
1212

1313
/**
14-
*
1514
* @param {string} jsonPath
15+
* @return {[string, boolean]}
1616
*/
1717
function read(jsonPath) {
1818
if (cache.has(jsonPath)) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"build-pack": "node ./scripts/build-pack.js",
5151
"test-spec": "mocha dist/**/*.spec.js -R spec --bail",
5252
"test-cov": "nyc mocha -- \"dist/**/*.spec.js\" -R spec --bail",
53-
"test": "npm run build && npm run lint && npm run test-cov",
53+
"test": "npm run build && npm run lint && npm run test-cov --",
5454
"coverage-report": "nyc report --reporter=lcov",
5555
"prepare": "npm run build-nopack"
5656
},

src/index.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ describe('ts-node', function () {
860860
it('should compile and execute as ESM', (done) => {
861861
exec(`${cmd} index.ts`, { cwd: join(__dirname, '../tests/esm') }, function (err, stdout) {
862862
expect(err).to.equal(null)
863-
expect(stdout).to.equal('foo bar baz biff\n')
863+
expect(stdout).to.equal('foo bar baz biff libfoo\n')
864864

865865
return done()
866866
})
@@ -883,15 +883,15 @@ describe('ts-node', function () {
883883
it('via --experimental-specifier-resolution', (done) => {
884884
exec(`${cmd} --experimental-specifier-resolution=node index.ts`, { cwd: join(__dirname, '../tests/esm-node-resolver') }, function (err, stdout) {
885885
expect(err).to.equal(null)
886-
expect(stdout).to.equal('foo bar baz biff\n')
886+
expect(stdout).to.equal('foo bar baz biff libfoo\n')
887887

888888
return done()
889889
})
890890
})
891891
it('via --es-module-specifier-resolution alias', (done) => {
892892
exec(`${cmd} --experimental-modules --es-module-specifier-resolution=node index.ts`, { cwd: join(__dirname, '../tests/esm-node-resolver') }, function (err, stdout) {
893893
expect(err).to.equal(null)
894-
expect(stdout).to.equal('foo bar baz biff\n')
894+
expect(stdout).to.equal('foo bar baz biff libfoo\n')
895895

896896
return done()
897897
})
@@ -905,7 +905,7 @@ describe('ts-node', function () {
905905
}
906906
}, function (err, stdout) {
907907
expect(err).to.equal(null)
908-
expect(stdout).to.equal('foo bar baz biff\n')
908+
expect(stdout).to.equal('foo bar baz biff libfoo\n')
909909

910910
return done()
911911
})

tests/esm-node-resolver/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import {foo} from './foo'
22
import {bar} from './bar'
33
import {baz} from './baz'
44
import {biff} from './biff'
5+
import {libfoo} from 'libfoo'
56

67
if(typeof module !== 'undefined') throw new Error('module should not exist in ESM')
78

8-
console.log(`${foo} ${bar} ${baz} ${biff}`)
9+
console.log(`${foo} ${bar} ${baz} ${biff} ${libfoo}`)

tests/esm-node-resolver/node_modules/libfoo/entrypoint.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/esm-node-resolver/node_modules/libfoo/index.d.ts

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/esm-node-resolver/node_modules/libfoo/package.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/esm/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import {foo} from './foo.js'
22
import {bar} from './bar.js'
33
import {baz} from './baz.js'
44
import {biff} from './biff.js'
5+
import {libfoo} from 'libfoo'
56

67
// Test import builtin modules
78
import {readFileSync} from 'fs';
89
if(typeof readFileSync !== 'function') throw new Error('failed to import builtin module')
910

1011
if(typeof module !== 'undefined') throw new Error('module should not exist in ESM')
1112

12-
console.log(`${foo} ${bar} ${baz} ${biff}`)
13+
console.log(`${foo} ${bar} ${baz} ${biff} ${libfoo}`)

tests/esm/node_modules/libfoo/entrypoint.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/esm/node_modules/libfoo/index.d.ts

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/esm/node_modules/libfoo/package.json

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/esm/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"compilerOptions": {
33
"module": "ESNext",
44
"allowJs": true,
5-
"jsx": "react"
5+
"jsx": "react",
6+
"moduleResolution": "node"
67
}
78
}

0 commit comments

Comments
 (0)