mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
esm: fix emit deprecation on legacy main resolve
PR-URL: https://github.com/nodejs/node/pull/48664 Refs: https://github.com/nodejs/node/pull/48325 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
This commit is contained in:
1 parent
e2688c8d79
commit
d9a800ee5c
2 files changed
+134
-12
No files matched your search
@@ -118,7 +118,7 @@ function emitInvalidSegmentDeprecation(target, request, match, pjsonUrl, interna
|
||||
* @param {URL} url
|
||||
* @param {URL} packageJSONUrl
|
||||
* @param {string | URL | undefined} base
|
||||
* @param {string} main
|
||||
* @param {string} [main]
|
||||
* @returns {void}
|
||||
*/
|
||||
function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
|
||||
@@ -128,17 +128,7 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
|
||||
const path = fileURLToPath(url);
|
||||
const pkgPath = fileURLToPath(new URL('.', packageJSONUrl));
|
||||
const basePath = fileURLToPath(base);
|
||||
if (main)
|
||||
process.emitWarning(
|
||||
`Package ${pkgPath} has a "main" field set to ${JSONStringify(main)}, ` +
|
||||
`excluding the full filename and extension to the resolved file at "${
|
||||
StringPrototypeSlice(path, pkgPath.length)}", imported from ${
|
||||
basePath}.\n Automatic extension resolution of the "main" field is ` +
|
||||
'deprecated for ES modules.',
|
||||
'DeprecationWarning',
|
||||
'DEP0151',
|
||||
);
|
||||
else
|
||||
if (!main) {
|
||||
process.emitWarning(
|
||||
`No "main" or "exports" field defined in the package.json for ${pkgPath
|
||||
} resolving the main entry point "${
|
||||
@@ -147,6 +137,17 @@ function emitLegacyIndexDeprecation(url, packageJSONUrl, base, main) {
|
||||
'DeprecationWarning',
|
||||
'DEP0151',
|
||||
);
|
||||
} else if (resolve(pkgPath, main) !== path) {
|
||||
process.emitWarning(
|
||||
`Package ${pkgPath} has a "main" field set to "${main}", ` +
|
||||
`excluding the full filename and extension to the resolved file at "${
|
||||
StringPrototypeSlice(path, pkgPath.length)}", imported from ${
|
||||
basePath}.\n Automatic extension resolution of the "main" field is ` +
|
||||
'deprecated for ES modules.',
|
||||
'DeprecationWarning',
|
||||
'DEP0151',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
import { spawnPromisified } from '../common/index.mjs';
|
||||
import * as tmpdir from '../common/tmpdir.js';
|
||||
|
||||
import assert from 'node:assert';
|
||||
import { mkdir, writeFile } from 'node:fs/promises';
|
||||
import * as path from 'node:path';
|
||||
import { execPath } from 'node:process';
|
||||
import { describe, it, before } from 'node:test';
|
||||
|
||||
describe('ESM in main field', { concurrency: true }, () => {
|
||||
before(() => tmpdir.refresh());
|
||||
|
||||
it('should handle fully-specified relative path without any warning', async () => {
|
||||
const cwd = path.join(tmpdir.path, Math.random().toString());
|
||||
const pkgPath = path.join(cwd, './node_modules/pkg/');
|
||||
await mkdir(pkgPath, { recursive: true });
|
||||
await writeFile(path.join(pkgPath, './index.js'), 'console.log("Hello World!")');
|
||||
await writeFile(path.join(pkgPath, './package.json'), JSON.stringify({
|
||||
main: './index.js',
|
||||
type: 'module',
|
||||
}));
|
||||
const { code, stdout, stderr } = await spawnPromisified(execPath, [
|
||||
'--input-type=module',
|
||||
'--eval', 'import "pkg"',
|
||||
], { cwd });
|
||||
|
||||
assert.strictEqual(stderr, '');
|
||||
assert.match(stdout, /^Hello World!\r?\n$/);
|
||||
assert.strictEqual(code, 0);
|
||||
});
|
||||
it('should handle fully-specified absolute path without any warning', async () => {
|
||||
const cwd = path.join(tmpdir.path, Math.random().toString());
|
||||
const pkgPath = path.join(cwd, './node_modules/pkg/');
|
||||
await mkdir(pkgPath, { recursive: true });
|
||||
await writeFile(path.join(pkgPath, './index.js'), 'console.log("Hello World!")');
|
||||
await writeFile(path.join(pkgPath, './package.json'), JSON.stringify({
|
||||
main: path.join(pkgPath, './index.js'),
|
||||
type: 'module',
|
||||
}));
|
||||
const { code, stdout, stderr } = await spawnPromisified(execPath, [
|
||||
'--input-type=module',
|
||||
'--eval', 'import "pkg"',
|
||||
], { cwd });
|
||||
|
||||
assert.strictEqual(stderr, '');
|
||||
assert.match(stdout, /^Hello World!\r?\n$/);
|
||||
assert.strictEqual(code, 0);
|
||||
});
|
||||
|
||||
it('should emit warning when "main" and "exports" are missing', async () => {
|
||||
const cwd = path.join(tmpdir.path, Math.random().toString());
|
||||
const pkgPath = path.join(cwd, './node_modules/pkg/');
|
||||
await mkdir(pkgPath, { recursive: true });
|
||||
await writeFile(path.join(pkgPath, './index.js'), 'console.log("Hello World!")');
|
||||
await writeFile(path.join(pkgPath, './package.json'), JSON.stringify({
|
||||
type: 'module',
|
||||
}));
|
||||
const { code, stdout, stderr } = await spawnPromisified(execPath, [
|
||||
'--input-type=module',
|
||||
'--eval', 'import "pkg"',
|
||||
], { cwd });
|
||||
|
||||
assert.match(stderr, /\[DEP0151\]/);
|
||||
assert.match(stdout, /^Hello World!\r?\n$/);
|
||||
assert.strictEqual(code, 0);
|
||||
});
|
||||
it('should emit warning when "main" is falsy', async () => {
|
||||
const cwd = path.join(tmpdir.path, Math.random().toString());
|
||||
const pkgPath = path.join(cwd, './node_modules/pkg/');
|
||||
await mkdir(pkgPath, { recursive: true });
|
||||
await writeFile(path.join(pkgPath, './index.js'), 'console.log("Hello World!")');
|
||||
await writeFile(path.join(pkgPath, './package.json'), JSON.stringify({
|
||||
type: 'module',
|
||||
main: '',
|
||||
}));
|
||||
const { code, stdout, stderr } = await spawnPromisified(execPath, [
|
||||
'--input-type=module',
|
||||
'--eval', 'import "pkg"',
|
||||
], { cwd });
|
||||
|
||||
assert.match(stderr, /\[DEP0151\]/);
|
||||
assert.match(stdout, /^Hello World!\r?\n$/);
|
||||
assert.strictEqual(code, 0);
|
||||
});
|
||||
it('should emit warning when "main" is a relative path without extension', async () => {
|
||||
const cwd = path.join(tmpdir.path, Math.random().toString());
|
||||
const pkgPath = path.join(cwd, './node_modules/pkg/');
|
||||
await mkdir(pkgPath, { recursive: true });
|
||||
await writeFile(path.join(pkgPath, './index.js'), 'console.log("Hello World!")');
|
||||
await writeFile(path.join(pkgPath, './package.json'), JSON.stringify({
|
||||
main: 'index',
|
||||
type: 'module',
|
||||
}));
|
||||
const { code, stdout, stderr } = await spawnPromisified(execPath, [
|
||||
'--input-type=module',
|
||||
'--eval', 'import "pkg"',
|
||||
], { cwd });
|
||||
|
||||
assert.match(stderr, /\[DEP0151\]/);
|
||||
assert.match(stdout, /^Hello World!\r?\n$/);
|
||||
assert.strictEqual(code, 0);
|
||||
});
|
||||
it('should emit warning when "main" is an absolute path without extension', async () => {
|
||||
const cwd = path.join(tmpdir.path, Math.random().toString());
|
||||
const pkgPath = path.join(cwd, './node_modules/pkg/');
|
||||
await mkdir(pkgPath, { recursive: true });
|
||||
await writeFile(path.join(pkgPath, './index.js'), 'console.log("Hello World!")');
|
||||
await writeFile(path.join(pkgPath, './package.json'), JSON.stringify({
|
||||
main: pkgPath + 'index',
|
||||
type: 'module',
|
||||
}));
|
||||
const { code, stdout, stderr } = await spawnPromisified(execPath, [
|
||||
'--input-type=module',
|
||||
'--eval', 'import "pkg"',
|
||||
], { cwd });
|
||||
|
||||
assert.match(stderr, /\[DEP0151\]/);
|
||||
assert.match(stdout, /^Hello World!\r?\n$/);
|
||||
assert.strictEqual(code, 0);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user