mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 08:11:30 -06:00
Key validity of import=require in module: node off of module and not target (#49222)
This commit is contained in:
parent
7c6521e8fb
commit
194a2aea0d
@ -72,7 +72,7 @@ namespace ts {
|
||||
// Though an error in es2020 modules, in node-flavor es2020 modules, we can helpfully transform this to a synthetic `require` call
|
||||
// To give easy access to a synchronous `require` in node-flavor esm. We do the transform even in scenarios where we error, but `import.meta.url`
|
||||
// is available, just because the output is reasonable for a node-like runtime.
|
||||
return getEmitScriptTarget(compilerOptions) >= ModuleKind.ES2020 ? visitImportEqualsDeclaration(node as ImportEqualsDeclaration) : undefined;
|
||||
return getEmitModuleKind(compilerOptions) >= ModuleKind.Node16 ? visitImportEqualsDeclaration(node as ImportEqualsDeclaration) : undefined;
|
||||
case SyntaxKind.ExportAssignment:
|
||||
return visitExportAssignment(node as ExportAssignment);
|
||||
case SyntaxKind.ExportDeclaration:
|
||||
|
||||
@ -0,0 +1,21 @@
|
||||
//// [tests/cases/compiler/moduleNodeImportRequireEmit.ts] ////
|
||||
|
||||
//// [package.json]
|
||||
{
|
||||
"type": "module"
|
||||
}
|
||||
//// [mod.d.ts]
|
||||
declare module "foo";
|
||||
//// [index.ts]
|
||||
/// <reference path="./mod.d.ts" />
|
||||
// This should emit a call to createRequire(import.meta.url)
|
||||
import foo = require("foo");
|
||||
foo;
|
||||
|
||||
//// [index.js]
|
||||
import { createRequire as _createRequire } from "module";
|
||||
const __require = _createRequire(import.meta.url);
|
||||
/// <reference path="./mod.d.ts" />
|
||||
// This should emit a call to createRequire(import.meta.url)
|
||||
const foo = __require("foo");
|
||||
foo;
|
||||
@ -0,0 +1,13 @@
|
||||
=== tests/cases/compiler/index.ts ===
|
||||
/// <reference path="./mod.d.ts" />
|
||||
// This should emit a call to createRequire(import.meta.url)
|
||||
import foo = require("foo");
|
||||
>foo : Symbol(foo, Decl(index.ts, 0, 0))
|
||||
|
||||
foo;
|
||||
>foo : Symbol(foo, Decl(index.ts, 0, 0))
|
||||
|
||||
=== tests/cases/compiler/mod.d.ts ===
|
||||
declare module "foo";
|
||||
>"foo" : Symbol("foo", Decl(mod.d.ts, 0, 0))
|
||||
|
||||
@ -0,0 +1,13 @@
|
||||
=== tests/cases/compiler/index.ts ===
|
||||
/// <reference path="./mod.d.ts" />
|
||||
// This should emit a call to createRequire(import.meta.url)
|
||||
import foo = require("foo");
|
||||
>foo : any
|
||||
|
||||
foo;
|
||||
>foo : any
|
||||
|
||||
=== tests/cases/compiler/mod.d.ts ===
|
||||
declare module "foo";
|
||||
>"foo" : any
|
||||
|
||||
@ -0,0 +1,21 @@
|
||||
//// [tests/cases/compiler/moduleNodeImportRequireEmit.ts] ////
|
||||
|
||||
//// [package.json]
|
||||
{
|
||||
"type": "module"
|
||||
}
|
||||
//// [mod.d.ts]
|
||||
declare module "foo";
|
||||
//// [index.ts]
|
||||
/// <reference path="./mod.d.ts" />
|
||||
// This should emit a call to createRequire(import.meta.url)
|
||||
import foo = require("foo");
|
||||
foo;
|
||||
|
||||
//// [index.js]
|
||||
import { createRequire as _createRequire } from "module";
|
||||
const __require = _createRequire(import.meta.url);
|
||||
/// <reference path="./mod.d.ts" />
|
||||
// This should emit a call to createRequire(import.meta.url)
|
||||
const foo = __require("foo");
|
||||
foo;
|
||||
@ -0,0 +1,13 @@
|
||||
=== tests/cases/compiler/index.ts ===
|
||||
/// <reference path="./mod.d.ts" />
|
||||
// This should emit a call to createRequire(import.meta.url)
|
||||
import foo = require("foo");
|
||||
>foo : Symbol(foo, Decl(index.ts, 0, 0))
|
||||
|
||||
foo;
|
||||
>foo : Symbol(foo, Decl(index.ts, 0, 0))
|
||||
|
||||
=== tests/cases/compiler/mod.d.ts ===
|
||||
declare module "foo";
|
||||
>"foo" : Symbol("foo", Decl(mod.d.ts, 0, 0))
|
||||
|
||||
@ -0,0 +1,13 @@
|
||||
=== tests/cases/compiler/index.ts ===
|
||||
/// <reference path="./mod.d.ts" />
|
||||
// This should emit a call to createRequire(import.meta.url)
|
||||
import foo = require("foo");
|
||||
>foo : any
|
||||
|
||||
foo;
|
||||
>foo : any
|
||||
|
||||
=== tests/cases/compiler/mod.d.ts ===
|
||||
declare module "foo";
|
||||
>"foo" : any
|
||||
|
||||
@ -0,0 +1,21 @@
|
||||
//// [tests/cases/compiler/moduleNodeImportRequireEmit.ts] ////
|
||||
|
||||
//// [package.json]
|
||||
{
|
||||
"type": "module"
|
||||
}
|
||||
//// [mod.d.ts]
|
||||
declare module "foo";
|
||||
//// [index.ts]
|
||||
/// <reference path="./mod.d.ts" />
|
||||
// This should emit a call to createRequire(import.meta.url)
|
||||
import foo = require("foo");
|
||||
foo;
|
||||
|
||||
//// [index.js]
|
||||
import { createRequire as _createRequire } from "module";
|
||||
var __require = _createRequire(import.meta.url);
|
||||
/// <reference path="./mod.d.ts" />
|
||||
// This should emit a call to createRequire(import.meta.url)
|
||||
var foo = __require("foo");
|
||||
foo;
|
||||
@ -0,0 +1,13 @@
|
||||
=== tests/cases/compiler/index.ts ===
|
||||
/// <reference path="./mod.d.ts" />
|
||||
// This should emit a call to createRequire(import.meta.url)
|
||||
import foo = require("foo");
|
||||
>foo : Symbol(foo, Decl(index.ts, 0, 0))
|
||||
|
||||
foo;
|
||||
>foo : Symbol(foo, Decl(index.ts, 0, 0))
|
||||
|
||||
=== tests/cases/compiler/mod.d.ts ===
|
||||
declare module "foo";
|
||||
>"foo" : Symbol("foo", Decl(mod.d.ts, 0, 0))
|
||||
|
||||
@ -0,0 +1,13 @@
|
||||
=== tests/cases/compiler/index.ts ===
|
||||
/// <reference path="./mod.d.ts" />
|
||||
// This should emit a call to createRequire(import.meta.url)
|
||||
import foo = require("foo");
|
||||
>foo : any
|
||||
|
||||
foo;
|
||||
>foo : any
|
||||
|
||||
=== tests/cases/compiler/mod.d.ts ===
|
||||
declare module "foo";
|
||||
>"foo" : any
|
||||
|
||||
@ -0,0 +1,21 @@
|
||||
//// [tests/cases/compiler/moduleNodeImportRequireEmit.ts] ////
|
||||
|
||||
//// [package.json]
|
||||
{
|
||||
"type": "module"
|
||||
}
|
||||
//// [mod.d.ts]
|
||||
declare module "foo";
|
||||
//// [index.ts]
|
||||
/// <reference path="./mod.d.ts" />
|
||||
// This should emit a call to createRequire(import.meta.url)
|
||||
import foo = require("foo");
|
||||
foo;
|
||||
|
||||
//// [index.js]
|
||||
import { createRequire as _createRequire } from "module";
|
||||
const __require = _createRequire(import.meta.url);
|
||||
/// <reference path="./mod.d.ts" />
|
||||
// This should emit a call to createRequire(import.meta.url)
|
||||
const foo = __require("foo");
|
||||
foo;
|
||||
@ -0,0 +1,13 @@
|
||||
=== tests/cases/compiler/index.ts ===
|
||||
/// <reference path="./mod.d.ts" />
|
||||
// This should emit a call to createRequire(import.meta.url)
|
||||
import foo = require("foo");
|
||||
>foo : Symbol(foo, Decl(index.ts, 0, 0))
|
||||
|
||||
foo;
|
||||
>foo : Symbol(foo, Decl(index.ts, 0, 0))
|
||||
|
||||
=== tests/cases/compiler/mod.d.ts ===
|
||||
declare module "foo";
|
||||
>"foo" : Symbol("foo", Decl(mod.d.ts, 0, 0))
|
||||
|
||||
@ -0,0 +1,13 @@
|
||||
=== tests/cases/compiler/index.ts ===
|
||||
/// <reference path="./mod.d.ts" />
|
||||
// This should emit a call to createRequire(import.meta.url)
|
||||
import foo = require("foo");
|
||||
>foo : any
|
||||
|
||||
foo;
|
||||
>foo : any
|
||||
|
||||
=== tests/cases/compiler/mod.d.ts ===
|
||||
declare module "foo";
|
||||
>"foo" : any
|
||||
|
||||
13
tests/cases/compiler/moduleNodeImportRequireEmit.ts
Normal file
13
tests/cases/compiler/moduleNodeImportRequireEmit.ts
Normal file
@ -0,0 +1,13 @@
|
||||
// @target: es5,es2016,es2020,esnext
|
||||
// @module: nodenext
|
||||
// @filename: package.json
|
||||
{
|
||||
"type": "module"
|
||||
}
|
||||
// @filename: mod.d.ts
|
||||
declare module "foo";
|
||||
// @filename: index.ts
|
||||
/// <reference path="./mod.d.ts" />
|
||||
// This should emit a call to createRequire(import.meta.url)
|
||||
import foo = require("foo");
|
||||
foo;
|
||||
Loading…
x
Reference in New Issue
Block a user