babel-plugin-transform-flow-enums
raw JSON → 0.0.2 verified Sat Apr 25 auth: no javascript
Transforms Flow Enum declarations (EnumDeclaration) into runtime calls using the flow-enums-runtime package. Current stable version is 0.0.2. Updated as part of Flow releases (e.g., v0.308.0 changed enum AST node shape). This plugin is essential for projects using Flow Enums and Babel, providing a compile-time transformation. Differentiators: official Facebook package, tightly coupled with Flow's enum syntax and runtime, supports boolean/number/string/symbol/bigint enum types. Requires @babel/plugin-syntax-flow with {enums: true} and the flow-enums-runtime package.
Common errors
error SyntaxError: Support for the experimental syntax 'Flow' isn't currently enabled ↓
cause Missing @babel/plugin-syntax-flow plugin.
fix
Add '@babel/plugin-syntax-flow' (with { enums: true } if using enums) to your Babel plugins.
error Cannot find module 'flow-enums-runtime' ↓
cause Missing runtime dependency in transformed code.
fix
Run 'npm install flow-enums-runtime' and ensure it's in your dependencies.
error Error: [BABEL] unknown: .plugins[1][1] must be an object, false, or undefined (BCP 6) ↓
cause Passing options as a string instead of an array or object.
fix
Use array form for plugin options: ['babel-plugin-transform-flow-enums', { getRuntime: ... }]
Warnings
breaking Flow v0.308.0 changed the enum AST node structure from separate body types (EnumBooleanBody, etc.) to a single EnumBody node. Ensure your plugin version is compatible with your Flow version. ↓
fix Update both plugin and Flow to compatible versions. If using Flow >=0.308.0, ensure babel-plugin-transform-flow-enums is updated to support new AST.
gotcha The @babel/plugin-syntax-flow plugin must be configured with {enums: true} as the plugin option array, not as a top-level Babel option. ↓
fix Correct configuration: plugins: [['@babel/plugin-syntax-flow', { enums: true }]]
gotcha The runtime dependency flow-enums-runtime must be installed in the project; this plugin will require it at runtime in the transformed code. ↓
fix Add flow-enums-runtime to your project dependencies: npm install flow-enums-runtime
gotcha This plugin only handles EnumDeclaration nodes; other flow enum-related syntax (e.g., enum member access) is left for the runtime. ↓
fix No fix needed; ensure flow-enums-runtime is used in your project.
Install
npm install babel-plugin-transform-flow-enums yarn add babel-plugin-transform-flow-enums pnpm add babel-plugin-transform-flow-enums Imports
- default export (plugin function) wrong
import plugin from 'babel-plugin-transform-flow-enums'; // Wrong: plugin is CommonJS, not ESMcorrectmodule.exports = require('babel-plugin-transform-flow-enums'); - babel.config.js usage wrong
plugins: [['babel-plugin-transform-flow-enums', { getRuntime: ... }]] // Wrong: passing options as array is correct only when options are provided; the string form without options is also validcorrectplugins: ['babel-plugin-transform-flow-enums'] - getRuntime option wrong
plugins: [['babel-plugin-transform-flow-enums', { getRuntime: 'flow-enums-runtime' }]] // Wrong: getRuntime must be a function, not a stringcorrectplugins: [['babel-plugin-transform-flow-enums', { getRuntime: (t) => t.memberExpression(t.identifier('require'), t.stringLiteral('flow-enums-runtime')) }]]
Quickstart
// Install dependencies:
// npm install --save-dev babel-plugin-transform-flow-enums @babel/plugin-syntax-flow flow-enums-runtime
// babel.config.js file:
module.exports = {
plugins: [
['@babel/plugin-syntax-flow', { enums: true }],
'babel-plugin-transform-flow-enums'
]
};
// Input file (test.js):
// @flow
enum Status { Active, Inactive }
// After transform:
// const Status = require('flow-enums-runtime').create(['Active', 'Inactive']);