Skip to content

Commit 55b6b27

Browse files
committed
Feedback loop
1 parent d1a7640 commit 55b6b27

3 files changed

Lines changed: 16 additions & 23 deletions

File tree

doc/api/packages.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,8 +1022,8 @@ Each package entry has the following fields:
10221022
When a bare specifier is encountered:
10231023

10241024
1. Node.js determines which package performs the resolution request.
1025-
* If possible the package ID for the importer file should be provided to the resolution algorithm.
1026-
* Failing that, the resolution will check if the file path is within any package's `path`.
1025+
* If possible the package ID for the importer file should be provided to the resolution algorithm.
1026+
* Failing that, the resolution will check if the file path is within any package's `path`.
10271027
2. If no package ID is provided and the importing file is not within any mapped package, an
10281028
[`ERR_PACKAGE_MAP_EXTERNAL_FILE`][] error is thrown.
10291029
3. Node.js looks up the specifier's package name in the importing package's

lib/internal/modules/esm/resolve.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const { BuiltinModule } = require('internal/bootstrap/realm');
2828
const { realpathSync } = require('fs');
2929
const { getOptionValue } = require('internal/options');
3030
// Do not eagerly grab .manifest, it may be in TDZ
31-
const { sep, posix: { relative: relativePosixPath }, resolve } = require('path');
31+
const { sep, posix: { relative: relativePosixPath }, resolve, join } = require('path');
3232
const { URL, pathToFileURL, fileURLToPath, isURL, URLParse } = require('internal/url');
3333
const { getCWDURL, setOwnProperty } = require('internal/util');
3434
const { canParse: URLCanParse } = internalBinding('url');
@@ -771,7 +771,7 @@ function packageResolve(specifier, base, conditions) {
771771
throw new ERR_MODULE_NOT_FOUND(specifier, fileURLToPath(base), null);
772772
}
773773
const { packagePath, subpath } = mapped;
774-
packageJSONPath = packagePath + '/package.json';
774+
packageJSONPath = join(packagePath, 'package.json');
775775
packageJSONUrl = pathToFileURL(packageJSONPath);
776776
packageSubpath = subpath;
777777
} else {

lib/internal/modules/package_map.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const {
1010
} = primordials;
1111

1212
const assert = require('internal/assert');
13-
const { getLazy } = require('internal/util');
13+
const { emitExperimentalWarning, getLazy } = require('internal/util');
1414
const { resolve: pathResolve, dirname } = require('path');
1515
const { fileURLToPath } = require('internal/url');
1616

@@ -30,7 +30,6 @@ const PROTOCOL_REGEXP = /^[a-zA-Z][a-zA-Z0-9+\-.]*:\/\//;
3030
// Singleton - initialized once on first call
3131
let packageMap;
3232
let packageMapPath;
33-
let emittedWarning = false;
3433

3534
/**
3635
* @typedef {object} PackageMapEntry
@@ -208,7 +207,7 @@ function parsePackageName(specifier) {
208207
function loadPackageMap(configPath) {
209208
let content;
210209
try {
211-
content = fs.readFileSync(configPath, 'utf8');
210+
content = fs.readFileSync(configPath);
212211
} catch (err) {
213212
if (err.code === 'ENOENT') {
214213
throw new ERR_PACKAGE_MAP_INVALID(configPath, 'file not found');
@@ -226,19 +225,6 @@ function loadPackageMap(configPath) {
226225
return new PackageMap(configPath, data);
227226
}
228227

229-
/**
230-
* Emit experimental warning on first use.
231-
*/
232-
function emitExperimentalWarning() {
233-
if (!emittedWarning) {
234-
emittedWarning = true;
235-
process.emitWarning(
236-
'Package map resolution is an experimental feature and might change at any time',
237-
'ExperimentalWarning',
238-
);
239-
}
240-
}
241-
242228
/**
243229
* Get the singleton package map, initializing on first call.
244230
* @returns {PackageMap|null}
@@ -252,8 +238,15 @@ function getPackageMap() {
252238
return null;
253239
}
254240

255-
emitExperimentalWarning();
256-
packageMap = loadPackageMap(pathResolve(packageMapPath));
241+
emitExperimentalWarning('Package maps');
242+
243+
try {
244+
packageMap = loadPackageMap(pathResolve(packageMapPath));
245+
} catch (err) {
246+
packageMap = new PackageMap(packageMapPath, { packages: {} });
247+
throw err;
248+
}
249+
257250
return packageMap;
258251
}
259252

@@ -262,7 +255,7 @@ function getPackageMap() {
262255
* @returns {boolean}
263256
*/
264257
function hasPackageMap() {
265-
return getPackageMap() !== null;
258+
return !!getPackageMapPath();
266259
}
267260

268261
/**

0 commit comments

Comments
 (0)