@benjavicente/router-utils 1.161.6 → 1.161.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/ast.cjs +19 -7
- package/dist/cjs/ast.cjs.map +1 -1
- package/dist/cjs/ast.d.cts +2 -1
- package/dist/esm/ast.d.ts +2 -1
- package/dist/esm/ast.js +19 -7
- package/dist/esm/ast.js.map +1 -1
- package/package.json +1 -1
- package/src/ast.ts +38 -7
package/dist/cjs/ast.cjs
CHANGED
|
@@ -6,18 +6,30 @@ let _babel_types = require("@babel/types");
|
|
|
6
6
|
_babel_types = require_runtime.__toESM(_babel_types);
|
|
7
7
|
let babel_dead_code_elimination = require("babel-dead-code-elimination");
|
|
8
8
|
//#region src/ast.ts
|
|
9
|
-
function parseAst({ code, ...opts }) {
|
|
9
|
+
function parseAst({ code, filename, sourceFilename, plugins, ...opts }) {
|
|
10
10
|
return (0, _babel_parser.parse)(code, {
|
|
11
|
-
plugins:
|
|
12
|
-
"jsx",
|
|
13
|
-
"typescript",
|
|
14
|
-
"explicitResourceManagement",
|
|
15
|
-
"decorators"
|
|
16
|
-
],
|
|
11
|
+
plugins: plugins ?? getDefaultParserPluginsForFilename(filename ?? sourceFilename),
|
|
17
12
|
sourceType: "module",
|
|
13
|
+
sourceFilename,
|
|
18
14
|
...opts
|
|
19
15
|
});
|
|
20
16
|
}
|
|
17
|
+
function getDefaultParserPluginsForFilename(filename) {
|
|
18
|
+
const plugins = [
|
|
19
|
+
"typescript",
|
|
20
|
+
"explicitResourceManagement",
|
|
21
|
+
"importAttributes",
|
|
22
|
+
"deprecatedImportAssert",
|
|
23
|
+
["decorators", { decoratorsBeforeExport: true }],
|
|
24
|
+
"decoratorAutoAccessors"
|
|
25
|
+
];
|
|
26
|
+
if (!isPlainTypeScriptFile(filename)) plugins.unshift("jsx");
|
|
27
|
+
return plugins;
|
|
28
|
+
}
|
|
29
|
+
function isPlainTypeScriptFile(filename) {
|
|
30
|
+
if (!filename) return false;
|
|
31
|
+
return /\.[cm]?ts(?:$|[?#])/.test(filename);
|
|
32
|
+
}
|
|
21
33
|
var generate = _babel_generator.default;
|
|
22
34
|
if ("default" in generate) generate = generate.default;
|
|
23
35
|
function generateFromAst(ast, opts) {
|
package/dist/cjs/ast.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ast.cjs","names":[],"sources":["../../src/ast.ts"],"sourcesContent":["import { parse } from '@babel/parser'\nimport _generate from '@babel/generator'\nimport * as t from '@babel/types'\nimport {\n deadCodeElimination as _deadCodeElimination,\n findReferencedIdentifiers,\n} from 'babel-dead-code-elimination'\nimport type { GeneratorOptions, GeneratorResult } from '@babel/generator'\nimport type { ParseResult, ParserOptions } from '@babel/parser'\nimport type * as _babel_types from '@babel/types'\n\nexport type ParseAstOptions = ParserOptions & {\n code: string\n}\n\nexport type ParseAstResult = ParseResult<_babel_types.File>\nexport function parseAst({
|
|
1
|
+
{"version":3,"file":"ast.cjs","names":[],"sources":["../../src/ast.ts"],"sourcesContent":["import { parse } from '@babel/parser'\nimport _generate from '@babel/generator'\nimport * as t from '@babel/types'\nimport {\n deadCodeElimination as _deadCodeElimination,\n findReferencedIdentifiers,\n} from 'babel-dead-code-elimination'\nimport type { GeneratorOptions, GeneratorResult } from '@babel/generator'\nimport type { ParseResult, ParserOptions } from '@babel/parser'\nimport type * as _babel_types from '@babel/types'\n\nexport type ParseAstOptions = ParserOptions & {\n code: string\n filename?: string\n}\n\nexport type ParseAstResult = ParseResult<_babel_types.File>\nexport function parseAst({\n code,\n filename,\n sourceFilename,\n plugins,\n ...opts\n}: ParseAstOptions): ParseAstResult {\n const inferredFilename = filename ?? sourceFilename\n return parse(code, {\n plugins: plugins ?? getDefaultParserPluginsForFilename(inferredFilename),\n sourceType: 'module',\n sourceFilename,\n ...opts,\n })\n}\n\nfunction getDefaultParserPluginsForFilename(\n filename: string | undefined,\n): NonNullable<ParserOptions['plugins']> {\n const plugins: NonNullable<ParserOptions['plugins']> = [\n 'typescript',\n 'explicitResourceManagement',\n 'importAttributes',\n 'deprecatedImportAssert',\n ['decorators', { decoratorsBeforeExport: true }],\n 'decoratorAutoAccessors',\n ]\n\n if (!isPlainTypeScriptFile(filename)) {\n plugins.unshift('jsx')\n }\n\n return plugins\n}\n\nfunction isPlainTypeScriptFile(filename: string | undefined): boolean {\n if (!filename) {\n return false\n }\n\n return /\\.[cm]?ts(?:$|[?#])/.test(filename)\n}\n\nlet generate = _generate\n\nif ('default' in generate) {\n generate = generate.default as typeof generate\n}\ntype GenerateFromAstOptions = GeneratorOptions &\n Required<Pick<GeneratorOptions, 'sourceFileName' | 'filename'>>\nexport function generateFromAst(\n ast: _babel_types.Node,\n opts?: GenerateFromAstOptions,\n): GeneratorResult {\n return generate(\n ast,\n opts\n ? { importAttributesKeyword: 'with', sourceMaps: true, ...opts }\n : undefined,\n )\n}\nexport type { GeneratorResult } from '@babel/generator'\n\n/**\n * Strips TypeScript type-only exports and imports from an AST.\n *\n * This is necessary because babel-dead-code-elimination doesn't handle\n * TypeScript type exports/imports. When a type export references an import\n * that pulls in server-only code, the dead code elimination won't remove\n * that import because it sees the type as still referencing it.\n *\n * This function removes:\n * - `export type Foo = ...`\n * - `export interface Foo { ... }`\n * - `export type { Foo } from './module'`\n * - `export type * from './module'`\n * - Type specifiers in mixed exports: `export { value, type Foo }` -> `export { value }`\n * - `import type { Foo } from './module'`\n * - Type specifiers in mixed imports: `import { value, type Foo } from './module'` -> `import { value }`\n *\n * Note: Non-exported type/interface declarations are preserved as they may be\n * used as type annotations within the code.\n *\n * @param ast - The Babel AST (or ParseResult) to mutate\n */\nexport function stripTypeExports(ast: ParseResult<_babel_types.File>): void {\n // Filter the program body to remove type-only nodes\n ast.program.body = ast.program.body.filter((node) => {\n // Handle export declarations\n if (t.isExportNamedDeclaration(node)) {\n // Remove entire export if it's a type-only export\n // e.g., `export type Foo = string`, `export interface Bar {}`, `export type { X } from './y'`\n if (node.exportKind === 'type') {\n return false\n }\n\n // For value exports with mixed specifiers, filter out type-only specifiers\n // e.g., `export { value, type TypeOnly }` -> `export { value }`\n if (node.specifiers.length > 0) {\n node.specifiers = node.specifiers.filter((specifier) => {\n if (t.isExportSpecifier(specifier)) {\n return specifier.exportKind !== 'type'\n }\n return true\n })\n\n // If all specifiers were removed, remove the entire export declaration\n // (unless it has a declaration like `export const x = 1`)\n if (node.specifiers.length === 0 && !node.declaration) {\n return false\n }\n }\n }\n\n // Handle type-only export-all declarations\n // e.g., `export type * from './module'`\n if (t.isExportAllDeclaration(node)) {\n if (node.exportKind === 'type') {\n return false\n }\n }\n\n // Handle import declarations\n if (t.isImportDeclaration(node)) {\n // Remove entire import if it's a type-only import\n // e.g., `import type { Foo } from './module'`\n if (node.importKind === 'type') {\n return false\n }\n\n // For value imports with mixed specifiers, filter out type-only specifiers\n // e.g., `import { value, type TypeOnly } from './module'` -> `import { value }`\n if (node.specifiers.length > 0) {\n node.specifiers = node.specifiers.filter((specifier) => {\n if (t.isImportSpecifier(specifier)) {\n return specifier.importKind !== 'type'\n }\n return true\n })\n\n // If all specifiers were removed, remove the entire import declaration\n if (node.specifiers.length === 0) {\n return false\n }\n }\n }\n\n return true\n })\n}\n\n// Re-export findReferencedIdentifiers from babel-dead-code-elimination\nexport { findReferencedIdentifiers }\n\n/**\n * Performs dead code elimination on the AST, with TypeScript type stripping.\n *\n * This is a wrapper around babel-dead-code-elimination that first strips\n * TypeScript type-only exports and imports. This is necessary because\n * babel-dead-code-elimination doesn't handle type exports, which can cause\n * imports to be retained when they're only referenced by type exports.\n *\n * @param ast - The Babel AST to mutate\n * @param candidates - Optional set of identifier paths to consider for removal.\n * If provided, only these identifiers will be candidates for removal.\n * This should be the result of `findReferencedIdentifiers(ast)` called\n * before any AST transformations.\n */\nexport function deadCodeElimination(\n ast: ParseResult<_babel_types.File>,\n candidates?: ReturnType<typeof findReferencedIdentifiers>,\n): void {\n // First strip TypeScript type-only exports and imports\n stripTypeExports(ast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I,KAAK,eAAe,OACtB,QAAO;AAKT,OAAI,KAAK,WAAW,SAAS,GAAG;AAC9B,SAAK,aAAa,KAAK,WAAW,QAAQ,cAAc;AACtD,SAAI,aAAE,kBAAkB,UAAU,CAChC,QAAO,UAAU,eAAe;AAElC,YAAO;MACP;AAGF,QAAI,KAAK,WAAW,WAAW,EAC7B,QAAO;;;AAKb,SAAO;GACP;;;;;;;;;;;;;;;;AAoBJ,SAAgB,oBACd,KACA,YACM;AAEN,kBAAiB,IAAI;AAGrB,EAAA,GAAA,4BAAA,qBAAqB,KAAK,WAAW"}
|
package/dist/cjs/ast.d.cts
CHANGED
|
@@ -4,9 +4,10 @@ import { ParseResult, ParserOptions } from '@babel/parser';
|
|
|
4
4
|
import type * as _babel_types from '@babel/types';
|
|
5
5
|
export type ParseAstOptions = ParserOptions & {
|
|
6
6
|
code: string;
|
|
7
|
+
filename?: string;
|
|
7
8
|
};
|
|
8
9
|
export type ParseAstResult = ParseResult<_babel_types.File>;
|
|
9
|
-
export declare function parseAst({ code, ...opts }: ParseAstOptions): ParseAstResult;
|
|
10
|
+
export declare function parseAst({ code, filename, sourceFilename, plugins, ...opts }: ParseAstOptions): ParseAstResult;
|
|
10
11
|
type GenerateFromAstOptions = GeneratorOptions & Required<Pick<GeneratorOptions, 'sourceFileName' | 'filename'>>;
|
|
11
12
|
export declare function generateFromAst(ast: _babel_types.Node, opts?: GenerateFromAstOptions): GeneratorResult;
|
|
12
13
|
export type { GeneratorResult } from '@babel/generator';
|
package/dist/esm/ast.d.ts
CHANGED
|
@@ -4,9 +4,10 @@ import { ParseResult, ParserOptions } from '@babel/parser';
|
|
|
4
4
|
import type * as _babel_types from '@babel/types';
|
|
5
5
|
export type ParseAstOptions = ParserOptions & {
|
|
6
6
|
code: string;
|
|
7
|
+
filename?: string;
|
|
7
8
|
};
|
|
8
9
|
export type ParseAstResult = ParseResult<_babel_types.File>;
|
|
9
|
-
export declare function parseAst({ code, ...opts }: ParseAstOptions): ParseAstResult;
|
|
10
|
+
export declare function parseAst({ code, filename, sourceFilename, plugins, ...opts }: ParseAstOptions): ParseAstResult;
|
|
10
11
|
type GenerateFromAstOptions = GeneratorOptions & Required<Pick<GeneratorOptions, 'sourceFileName' | 'filename'>>;
|
|
11
12
|
export declare function generateFromAst(ast: _babel_types.Node, opts?: GenerateFromAstOptions): GeneratorResult;
|
|
12
13
|
export type { GeneratorResult } from '@babel/generator';
|
package/dist/esm/ast.js
CHANGED
|
@@ -3,18 +3,30 @@ import _generate from "@babel/generator";
|
|
|
3
3
|
import * as t from "@babel/types";
|
|
4
4
|
import { deadCodeElimination, findReferencedIdentifiers } from "babel-dead-code-elimination";
|
|
5
5
|
//#region src/ast.ts
|
|
6
|
-
function parseAst({ code, ...opts }) {
|
|
6
|
+
function parseAst({ code, filename, sourceFilename, plugins, ...opts }) {
|
|
7
7
|
return parse(code, {
|
|
8
|
-
plugins:
|
|
9
|
-
"jsx",
|
|
10
|
-
"typescript",
|
|
11
|
-
"explicitResourceManagement",
|
|
12
|
-
"decorators"
|
|
13
|
-
],
|
|
8
|
+
plugins: plugins ?? getDefaultParserPluginsForFilename(filename ?? sourceFilename),
|
|
14
9
|
sourceType: "module",
|
|
10
|
+
sourceFilename,
|
|
15
11
|
...opts
|
|
16
12
|
});
|
|
17
13
|
}
|
|
14
|
+
function getDefaultParserPluginsForFilename(filename) {
|
|
15
|
+
const plugins = [
|
|
16
|
+
"typescript",
|
|
17
|
+
"explicitResourceManagement",
|
|
18
|
+
"importAttributes",
|
|
19
|
+
"deprecatedImportAssert",
|
|
20
|
+
["decorators", { decoratorsBeforeExport: true }],
|
|
21
|
+
"decoratorAutoAccessors"
|
|
22
|
+
];
|
|
23
|
+
if (!isPlainTypeScriptFile(filename)) plugins.unshift("jsx");
|
|
24
|
+
return plugins;
|
|
25
|
+
}
|
|
26
|
+
function isPlainTypeScriptFile(filename) {
|
|
27
|
+
if (!filename) return false;
|
|
28
|
+
return /\.[cm]?ts(?:$|[?#])/.test(filename);
|
|
29
|
+
}
|
|
18
30
|
var generate = _generate;
|
|
19
31
|
if ("default" in generate) generate = generate.default;
|
|
20
32
|
function generateFromAst(ast, opts) {
|
package/dist/esm/ast.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ast.js","names":[],"sources":["../../src/ast.ts"],"sourcesContent":["import { parse } from '@babel/parser'\nimport _generate from '@babel/generator'\nimport * as t from '@babel/types'\nimport {\n deadCodeElimination as _deadCodeElimination,\n findReferencedIdentifiers,\n} from 'babel-dead-code-elimination'\nimport type { GeneratorOptions, GeneratorResult } from '@babel/generator'\nimport type { ParseResult, ParserOptions } from '@babel/parser'\nimport type * as _babel_types from '@babel/types'\n\nexport type ParseAstOptions = ParserOptions & {\n code: string\n}\n\nexport type ParseAstResult = ParseResult<_babel_types.File>\nexport function parseAst({
|
|
1
|
+
{"version":3,"file":"ast.js","names":[],"sources":["../../src/ast.ts"],"sourcesContent":["import { parse } from '@babel/parser'\nimport _generate from '@babel/generator'\nimport * as t from '@babel/types'\nimport {\n deadCodeElimination as _deadCodeElimination,\n findReferencedIdentifiers,\n} from 'babel-dead-code-elimination'\nimport type { GeneratorOptions, GeneratorResult } from '@babel/generator'\nimport type { ParseResult, ParserOptions } from '@babel/parser'\nimport type * as _babel_types from '@babel/types'\n\nexport type ParseAstOptions = ParserOptions & {\n code: string\n filename?: string\n}\n\nexport type ParseAstResult = ParseResult<_babel_types.File>\nexport function parseAst({\n code,\n filename,\n sourceFilename,\n plugins,\n ...opts\n}: ParseAstOptions): ParseAstResult {\n const inferredFilename = filename ?? sourceFilename\n return parse(code, {\n plugins: plugins ?? getDefaultParserPluginsForFilename(inferredFilename),\n sourceType: 'module',\n sourceFilename,\n ...opts,\n })\n}\n\nfunction getDefaultParserPluginsForFilename(\n filename: string | undefined,\n): NonNullable<ParserOptions['plugins']> {\n const plugins: NonNullable<ParserOptions['plugins']> = [\n 'typescript',\n 'explicitResourceManagement',\n 'importAttributes',\n 'deprecatedImportAssert',\n ['decorators', { decoratorsBeforeExport: true }],\n 'decoratorAutoAccessors',\n ]\n\n if (!isPlainTypeScriptFile(filename)) {\n plugins.unshift('jsx')\n }\n\n return plugins\n}\n\nfunction isPlainTypeScriptFile(filename: string | undefined): boolean {\n if (!filename) {\n return false\n }\n\n return /\\.[cm]?ts(?:$|[?#])/.test(filename)\n}\n\nlet generate = _generate\n\nif ('default' in generate) {\n generate = generate.default as typeof generate\n}\ntype GenerateFromAstOptions = GeneratorOptions &\n Required<Pick<GeneratorOptions, 'sourceFileName' | 'filename'>>\nexport function generateFromAst(\n ast: _babel_types.Node,\n opts?: GenerateFromAstOptions,\n): GeneratorResult {\n return generate(\n ast,\n opts\n ? { importAttributesKeyword: 'with', sourceMaps: true, ...opts }\n : undefined,\n )\n}\nexport type { GeneratorResult } from '@babel/generator'\n\n/**\n * Strips TypeScript type-only exports and imports from an AST.\n *\n * This is necessary because babel-dead-code-elimination doesn't handle\n * TypeScript type exports/imports. When a type export references an import\n * that pulls in server-only code, the dead code elimination won't remove\n * that import because it sees the type as still referencing it.\n *\n * This function removes:\n * - `export type Foo = ...`\n * - `export interface Foo { ... }`\n * - `export type { Foo } from './module'`\n * - `export type * from './module'`\n * - Type specifiers in mixed exports: `export { value, type Foo }` -> `export { value }`\n * - `import type { Foo } from './module'`\n * - Type specifiers in mixed imports: `import { value, type Foo } from './module'` -> `import { value }`\n *\n * Note: Non-exported type/interface declarations are preserved as they may be\n * used as type annotations within the code.\n *\n * @param ast - The Babel AST (or ParseResult) to mutate\n */\nexport function stripTypeExports(ast: ParseResult<_babel_types.File>): void {\n // Filter the program body to remove type-only nodes\n ast.program.body = ast.program.body.filter((node) => {\n // Handle export declarations\n if (t.isExportNamedDeclaration(node)) {\n // Remove entire export if it's a type-only export\n // e.g., `export type Foo = string`, `export interface Bar {}`, `export type { X } from './y'`\n if (node.exportKind === 'type') {\n return false\n }\n\n // For value exports with mixed specifiers, filter out type-only specifiers\n // e.g., `export { value, type TypeOnly }` -> `export { value }`\n if (node.specifiers.length > 0) {\n node.specifiers = node.specifiers.filter((specifier) => {\n if (t.isExportSpecifier(specifier)) {\n return specifier.exportKind !== 'type'\n }\n return true\n })\n\n // If all specifiers were removed, remove the entire export declaration\n // (unless it has a declaration like `export const x = 1`)\n if (node.specifiers.length === 0 && !node.declaration) {\n return false\n }\n }\n }\n\n // Handle type-only export-all declarations\n // e.g., `export type * from './module'`\n if (t.isExportAllDeclaration(node)) {\n if (node.exportKind === 'type') {\n return false\n }\n }\n\n // Handle import declarations\n if (t.isImportDeclaration(node)) {\n // Remove entire import if it's a type-only import\n // e.g., `import type { Foo } from './module'`\n if (node.importKind === 'type') {\n return false\n }\n\n // For value imports with mixed specifiers, filter out type-only specifiers\n // e.g., `import { value, type TypeOnly } from './module'` -> `import { value }`\n if (node.specifiers.length > 0) {\n node.specifiers = node.specifiers.filter((specifier) => {\n if (t.isImportSpecifier(specifier)) {\n return specifier.importKind !== 'type'\n }\n return true\n })\n\n // If all specifiers were removed, remove the entire import declaration\n if (node.specifiers.length === 0) {\n return false\n }\n }\n }\n\n return true\n })\n}\n\n// Re-export findReferencedIdentifiers from babel-dead-code-elimination\nexport { findReferencedIdentifiers }\n\n/**\n * Performs dead code elimination on the AST, with TypeScript type stripping.\n *\n * This is a wrapper around babel-dead-code-elimination that first strips\n * TypeScript type-only exports and imports. This is necessary because\n * babel-dead-code-elimination doesn't handle type exports, which can cause\n * imports to be retained when they're only referenced by type exports.\n *\n * @param ast - The Babel AST to mutate\n * @param candidates - Optional set of identifier paths to consider for removal.\n * If provided, only these identifiers will be candidates for removal.\n * This should be the result of `findReferencedIdentifiers(ast)` called\n * before any AST transformations.\n */\nexport function deadCodeElimination(\n ast: ParseResult<_babel_types.File>,\n candidates?: ReturnType<typeof findReferencedIdentifiers>,\n): void {\n // First strip TypeScript type-only exports and imports\n stripTypeExports(ast)\n\n // Then run the original dead code elimination\n _deadCodeElimination(ast, candidates)\n}\n"],"mappings":";;;;;AAiBA,SAAgB,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cAAc;AACtD,SAAI,EAAE,kBAAkB,UAAU,CAChC,QAAO,UAAU,eAAe;AAElC,YAAO;MACP;AAGF,QAAI,KAAK,WAAW,WAAW,EAC7B,QAAO;;;AAKb,SAAO;GACP;;;;;;;;;;;;;;;;AAoBJ,SAAgB,sBACd,KACA,YACM;AAEN,kBAAiB,IAAI;AAGrB,qBAAqB,KAAK,WAAW"}
|
package/package.json
CHANGED
package/src/ast.ts
CHANGED
|
@@ -11,22 +11,53 @@ import type * as _babel_types from '@babel/types'
|
|
|
11
11
|
|
|
12
12
|
export type ParseAstOptions = ParserOptions & {
|
|
13
13
|
code: string
|
|
14
|
+
filename?: string
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
export type ParseAstResult = ParseResult<_babel_types.File>
|
|
17
|
-
export function parseAst({
|
|
18
|
+
export function parseAst({
|
|
19
|
+
code,
|
|
20
|
+
filename,
|
|
21
|
+
sourceFilename,
|
|
22
|
+
plugins,
|
|
23
|
+
...opts
|
|
24
|
+
}: ParseAstOptions): ParseAstResult {
|
|
25
|
+
const inferredFilename = filename ?? sourceFilename
|
|
18
26
|
return parse(code, {
|
|
19
|
-
plugins:
|
|
20
|
-
'jsx',
|
|
21
|
-
'typescript',
|
|
22
|
-
'explicitResourceManagement',
|
|
23
|
-
'decorators', // required for Angular and other decorator-based code
|
|
24
|
-
],
|
|
27
|
+
plugins: plugins ?? getDefaultParserPluginsForFilename(inferredFilename),
|
|
25
28
|
sourceType: 'module',
|
|
29
|
+
sourceFilename,
|
|
26
30
|
...opts,
|
|
27
31
|
})
|
|
28
32
|
}
|
|
29
33
|
|
|
34
|
+
function getDefaultParserPluginsForFilename(
|
|
35
|
+
filename: string | undefined,
|
|
36
|
+
): NonNullable<ParserOptions['plugins']> {
|
|
37
|
+
const plugins: NonNullable<ParserOptions['plugins']> = [
|
|
38
|
+
'typescript',
|
|
39
|
+
'explicitResourceManagement',
|
|
40
|
+
'importAttributes',
|
|
41
|
+
'deprecatedImportAssert',
|
|
42
|
+
['decorators', { decoratorsBeforeExport: true }],
|
|
43
|
+
'decoratorAutoAccessors',
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
if (!isPlainTypeScriptFile(filename)) {
|
|
47
|
+
plugins.unshift('jsx')
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return plugins
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function isPlainTypeScriptFile(filename: string | undefined): boolean {
|
|
54
|
+
if (!filename) {
|
|
55
|
+
return false
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return /\.[cm]?ts(?:$|[?#])/.test(filename)
|
|
59
|
+
}
|
|
60
|
+
|
|
30
61
|
let generate = _generate
|
|
31
62
|
|
|
32
63
|
if ('default' in generate) {
|