A Babel plugin that transforms pipeline operator (|>) syntax into standard JavaScript call expressions. This plugin supports multiple proposal variants (minimal, fsharp, hack, and smart) allowing developers to experiment with the proposed pipeline operator while maintaining compatibility with current JavaScript engines through Babel's transpilation process.
npm install --save-dev @babel/plugin-proposal-pipeline-operatorThis plugin is not imported directly in your code. Instead, it's configured in your Babel configuration:
// babel.config.js
module.exports = {
plugins: [
["@babel/plugin-proposal-pipeline-operator", { proposal: "minimal" }]
]
};// .babelrc.json
{
"plugins": [
["@babel/plugin-proposal-pipeline-operator", { "proposal": "minimal" }]
]
}Once configured, the plugin transforms pipeline operators in your code:
// For hack proposal, topicToken is also required
// babel.config.js
module.exports = {
plugins: [
["@babel/plugin-proposal-pipeline-operator", {
proposal: "hack",
topicToken: "#"
}]
]
};Input code (using pipeline operator):
// Minimal/F# proposal
const result = value |> func1 |> func2;
// Hack proposal (using topic token)
const result = value |> func1(#) |> func2(#, other);Output code (transformed):
// Minimal/F# proposal transformation
const result = func2(func1(value));
// Hack proposal transformation
const result = ((_) => func2(_, other))(func1(value));The plugin implements the pipeline operator (|>) transformation for JavaScript/TypeScript by:
@babel/plugin-syntax-pipeline-operatorThe plugin is configured through Babel with a required proposal option and optional topicToken for hack proposal.
/**
* Plugin options interface defining proposal variant and topic token
*/
interface Options {
/** The pipeline operator proposal variant to use */
proposal: "minimal" | "fsharp" | "hack" | "smart";
/** Topic token for hack proposal (required when proposal is "hack") */
topicToken?: "^^" | "@@" | "^" | "%" | "#";
}The plugin supports four different pipeline operator proposals, each with different syntax and behavior:
Simple left-to-right function application where the left operand becomes the first argument:
// Transforms: value |> func
// Into: func(value)
const result = "hello" |> toUpperCase |> reverse;
// Becomes: reverse(toUpperCase("hello"))Similar to minimal but supports await expressions in pipelines:
// Transforms: value |> func and value |> await
// Into: func(value) and await value
const result = fetchData() |> await |> processData;
// Becomes: processData(await fetchData())Uses topic tokens (placeholder symbols) for more complex expressions:
// Requires topicToken option, e.g., topicToken: "#"
// Transforms: value |> func(#, other)
// Into: func(value, other)
const result = data |> filter(#, predicate) |> #.length;
// Becomes: ((_temp) => _temp.length)(filter(data, predicate))Combines features of minimal and hack proposals. Use "hack" instead as this is deprecated in Babel 8.
The plugin validates configuration and provides helpful error messages:
proposal value is providedtopicToken option when using "hack" proposal