-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
esm: fix emit deprecation on legacy main resolve
PR-URL: #48664 Refs: #48325 Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]>
- Loading branch information
Showing
2 changed files
with
135 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
test/es-module/test-esm-extension-lookup-deprecation.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); |