繁体   English   中英

使用 babel 运行自定义 function 作为构建过程的一部分

[英]Run custom function as part of build process with babel

我想在构建过程中对 JSON 文件进行排序。 基本上我有react-intl定义本地化消息,然后babel-plugin-react-intl将这些消息提取到文件 - 问题是有问题的文件没有排序(这会产生各种烦人的版本控制问题)。

我可以简单地破解插件的实际 dist(有效),但这不是很可扩展。 我查看了babel-plugin-macros ,但似乎其中大部分都涉及遍历应用程序的实际 AST。 基本上我只想说; “在每次构建结束时,运行此函数”,其中将包含:

//Open the file en.json
Object.keys(messages).reduce((accumulator, currentValue) => {
  accumulator[currentValue] = testObj[currentValue];
  return accumulator;
}, {})
// save it again

没有任何简单的方法可以将额外的 function 作为管道的一部分调用 - 而无需过多地钩入 babel 的整个访问者/ast 遍历 API 吗?

Assuming you are calling the webpack build eg using webpack inside of a package.json script like this by using npm run build or yarn build

"scripts": { 
   "build": "webpack"
}

然后你可以很容易地用你的排序任务创建一个 javascript 文件(sortFile.js),然后在同一个构建脚本命令中调用它......例如

"scripts": { 
   "build": "webpack && node ./sortFile.js"
}

&& shell 命令只会在第一步成功后运行第二步。

shell 命令中“&&”的用途是什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM