ts-transformer-optimize-const-enum
Advanced tools
Comparing version 0.1.0 to 0.1.2
@@ -11,3 +11,2 @@ "use strict"; | ||
return (sourceFile) => { | ||
const ambient = sourceFile.isDeclarationFile; | ||
return typescript_1.default.visitEachChild(sourceFile, visitor, ctx); | ||
@@ -18,12 +17,15 @@ function visitor(node) { | ||
} | ||
const exportModifier = (0, utils_1.getModifier)(node, typescript_1.default.SyntaxKind.ExportKeyword); | ||
if (!exportModifier) | ||
if (!(0, utils_1.hasModifier)(node, typescript_1.default.SyntaxKind.ConstKeyword)) { | ||
return node; | ||
const constModifier = (0, utils_1.getModifier)(node, typescript_1.default.SyntaxKind.ConstKeyword); | ||
if (!constModifier) | ||
return node; | ||
if (ambient) { | ||
} | ||
if (!(0, utils_1.hasModifier)(node, typescript_1.default.SyntaxKind.ExportKeyword)) { | ||
const exportedNames = (0, utils_1.getExportedNamesOfSource)(program, sourceFile); | ||
if (!exportedNames.includes(node.name.text)) { | ||
return node; | ||
} | ||
} | ||
if (sourceFile.isDeclarationFile) { | ||
return typescript_1.default.visitEachChild(node, stripConstKeyword, ctx); | ||
} | ||
return transformEnum(node, [exportModifier, constModifier]); | ||
return transformEnum(node); | ||
} | ||
@@ -35,3 +37,3 @@ }; | ||
} | ||
function transformEnum(node, modifiers) { | ||
function transformEnum(node) { | ||
const members = node.members; | ||
@@ -73,3 +75,3 @@ const known = new Map(); | ||
} | ||
const result = typescript_1.default.factory.createVariableStatement(modifiers, typescript_1.default.factory.createVariableDeclarationList([ | ||
const result = typescript_1.default.factory.createVariableStatement(node.modifiers, typescript_1.default.factory.createVariableDeclarationList([ | ||
typescript_1.default.factory.createVariableDeclaration(node.name, undefined, undefined, typescript_1.default.factory.createAsExpression(typescript_1.default.factory.createObjectLiteralExpression(properties, true), typescript_1.default.factory.createTypeReferenceNode(typescript_1.default.factory.createIdentifier('const'), undefined))), | ||
@@ -76,0 +78,0 @@ ], typescript_1.default.NodeFlags.Const)); |
import ts from 'typescript'; | ||
export declare function evaluate(expr: ts.Expression, known: Map<string, number | string>): string | number; | ||
export declare function getModifier(node: ts.Node, modifier: ts.SyntaxKind): ts.Modifier | undefined; | ||
export declare function hasModifier(node: ts.Node, modifier: ts.SyntaxKind): boolean | undefined; | ||
export declare function getExportedNamesOfSource(program: ts.Program, sourceFile: ts.SourceFile): string[]; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getModifier = exports.evaluate = void 0; | ||
exports.getExportedNamesOfSource = exports.hasModifier = exports.evaluate = void 0; | ||
const typescript_1 = __importDefault(require("typescript")); | ||
@@ -79,6 +79,29 @@ function evaluate(expr, known) { | ||
exports.evaluate = evaluate; | ||
function getModifier(node, modifier) { | ||
return (node.modifiers | ||
&& node.modifiers.find((mod) => mod.kind === modifier)); | ||
function hasModifier(node, modifier) { | ||
var _a; | ||
return ((_a = node.modifiers) === null || _a === void 0 ? void 0 : _a.some((mod) => mod.kind === modifier)); | ||
} | ||
exports.getModifier = getModifier; | ||
exports.hasModifier = hasModifier; | ||
const cachedNames = new WeakMap(); | ||
function getExportedNamesOfSource(program, sourceFile) { | ||
const cached = cachedNames.get(sourceFile); | ||
if (cached) | ||
return cached; | ||
const typeChecker = program.getTypeChecker(); | ||
const sourceSymbol = typeChecker.getSymbolAtLocation(sourceFile); | ||
let names; | ||
if (sourceSymbol) { | ||
names = typeChecker.getExportsOfModule(sourceSymbol).map(s => { | ||
if (s.flags & typescript_1.default.SymbolFlags.Alias) { | ||
return typeChecker.getAliasedSymbol(s).name; | ||
} | ||
return s.name; | ||
}); | ||
} | ||
else { | ||
names = []; | ||
} | ||
cachedNames.set(sourceFile, names); | ||
return names; | ||
} | ||
exports.getExportedNamesOfSource = getExportedNamesOfSource; |
import ts from 'typescript'; | ||
import { evaluate, getModifier } from './utils'; | ||
import { evaluate, getExportedNamesOfSource, hasModifier } from './utils'; | ||
export default function (program, pluginOptions) { | ||
return (ctx) => { | ||
return (sourceFile) => { | ||
const ambient = sourceFile.isDeclarationFile; | ||
return ts.visitEachChild(sourceFile, visitor, ctx); | ||
@@ -12,12 +11,15 @@ function visitor(node) { | ||
} | ||
const exportModifier = getModifier(node, ts.SyntaxKind.ExportKeyword); | ||
if (!exportModifier) | ||
if (!hasModifier(node, ts.SyntaxKind.ConstKeyword)) { | ||
return node; | ||
const constModifier = getModifier(node, ts.SyntaxKind.ConstKeyword); | ||
if (!constModifier) | ||
return node; | ||
if (ambient) { | ||
} | ||
if (!hasModifier(node, ts.SyntaxKind.ExportKeyword)) { | ||
const exportedNames = getExportedNamesOfSource(program, sourceFile); | ||
if (!exportedNames.includes(node.name.text)) { | ||
return node; | ||
} | ||
} | ||
if (sourceFile.isDeclarationFile) { | ||
return ts.visitEachChild(node, stripConstKeyword, ctx); | ||
} | ||
return transformEnum(node, [exportModifier, constModifier]); | ||
return transformEnum(node); | ||
} | ||
@@ -29,3 +31,3 @@ }; | ||
} | ||
function transformEnum(node, modifiers) { | ||
function transformEnum(node) { | ||
const members = node.members; | ||
@@ -67,3 +69,3 @@ const known = new Map(); | ||
} | ||
const result = ts.factory.createVariableStatement(modifiers, ts.factory.createVariableDeclarationList([ | ||
const result = ts.factory.createVariableStatement(node.modifiers, ts.factory.createVariableDeclarationList([ | ||
ts.factory.createVariableDeclaration(node.name, undefined, undefined, ts.factory.createAsExpression(ts.factory.createObjectLiteralExpression(properties, true), ts.factory.createTypeReferenceNode(ts.factory.createIdentifier('const'), undefined))), | ||
@@ -70,0 +72,0 @@ ], ts.NodeFlags.Const)); |
@@ -71,5 +71,27 @@ import ts from 'typescript'; | ||
} | ||
export function getModifier(node, modifier) { | ||
return (node.modifiers | ||
&& node.modifiers.find((mod) => mod.kind === modifier)); | ||
export function hasModifier(node, modifier) { | ||
var _a; | ||
return ((_a = node.modifiers) === null || _a === void 0 ? void 0 : _a.some((mod) => mod.kind === modifier)); | ||
} | ||
const cachedNames = new WeakMap(); | ||
export function getExportedNamesOfSource(program, sourceFile) { | ||
const cached = cachedNames.get(sourceFile); | ||
if (cached) | ||
return cached; | ||
const typeChecker = program.getTypeChecker(); | ||
const sourceSymbol = typeChecker.getSymbolAtLocation(sourceFile); | ||
let names; | ||
if (sourceSymbol) { | ||
names = typeChecker.getExportsOfModule(sourceSymbol).map(s => { | ||
if (s.flags & ts.SymbolFlags.Alias) { | ||
return typeChecker.getAliasedSymbol(s).name; | ||
} | ||
return s.name; | ||
}); | ||
} | ||
else { | ||
names = []; | ||
} | ||
cachedNames.set(sourceFile, names); | ||
return names; | ||
} |
{ | ||
"name": "ts-transformer-optimize-const-enum", | ||
"version": "0.1.0", | ||
"version": "0.1.2", | ||
"description": "A TypeScript transformer that optimize exported const enum into object literal", | ||
@@ -5,0 +5,0 @@ "main": "dist/cjs/transform.js", |
@@ -24,3 +24,3 @@ # ts-transformer-optimize-const-enum | ||
```ts | ||
export const MyEnum { | ||
export const MyEnum = { | ||
A: 0, | ||
@@ -30,4 +30,4 @@ B: 1, | ||
D: 10, | ||
E: 400 | ||
} as const | ||
E: 400, | ||
} as const; | ||
``` | ||
@@ -73,2 +73,4 @@ | ||
Although keys of object literals can't be tree-shaken by webpack, however, the exported object literals have no side effects like enums do. If one of your code-splitting chunks does not use it, it will be completely erased. | ||
# Installation | ||
@@ -131,7 +133,7 @@ | ||
## Rollup (with rollup-plugin-typescript2) | ||
## Rollup (with @rollup/plugin-typescript or rollup-plugin-typescript2) | ||
```js | ||
// rollup.config.js | ||
import typescript from 'rollup-plugin-typescript2'; | ||
import typescript from '@rollup/plugin-typescript'; | ||
import optimizeConstEnum from 'ts-transformer-optimize-const-enum'; | ||
@@ -138,0 +140,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
22271
360
153
0