如何编写将输出传递给其他插件的 babel js 插件?

Mic*_*ill 3 javascript babeljs babel-plugin

我正在尝试编写一个 babel 插件,将内容添加到文件中 - 例如;添加行console.log("start of " + __filename);console.log("end of " + __filename);到每个文件。

到目前为止,我已经设法编写了一个完全执行此操作的访问者,但是在我的插件运行之前或之后,任何其他插件都不会修改现有代码。

例如,我有以下文件:

import * as foo from 'foo';
import * as bar from 'bar';

console.dir({ foo, bar });
Run Code Online (Sandbox Code Playgroud)

env单独使用预设(即没有我的插件)和targets.node: 'current'我最终得到输出的选项- 请注意,es6 导入已转换为 commonjs 需要:

'use strict';
var _foo = require('foo');
var foo = _interopRequireWildcard(_foo);
var _bar = require('bar');
var bar = _interopRequireWildcard(_bar);
function _interopRequireWildcard(obj) { /* resolve commonjs or es6 module */ }
console.dir({ foo, bar });
Run Code Online (Sandbox Code Playgroud)

但是,一旦我将自己的插件添加到此;似乎env跳过了预设以支持我自己的插件 - 但是我希望应用这两个插件(最好是我的)。


到目前为止,我的插件代码如下所示:

module.exports = function wrapModule(babel) {

  const prepend = transform(`console.log("start of " + __filename);`)
  const append = transform(`console.log("end of " + __filename);`)
  return {
    visitor: {
      Program(path, {opts}) {
        path.replaceWith(t.program([...prepend, ...path.node.body, ...append]))
        path.stop()
      }
    }
  }

  function transform(content) {
    return babel.transform(content).ast.program.body
  }
}
Run Code Online (Sandbox Code Playgroud)

而我.babelrc的只是:

{
  "presets": [[
    "env", { "targets": { "node": "current" } }
  ]],
  "plugins": [
    "./wrapper-babel-plugin"
  ]
}
Run Code Online (Sandbox Code Playgroud)

这是产生输出:

console.log("start of " + __filename);
import * as foo from 'foo';
import * as bar from 'bar';

console.dir({ foo, bar });
console.log("end of " + __filename);
Run Code Online (Sandbox Code Playgroud)

任何人都可以建议我缺少什么让 babel 将我的插件与其他插件链接起来,以允许我组合使用多个插件?

log*_*yth 6

这是产生输出:

鉴于您正在使用eval,并且您正在transform使用错误数量的参数调用您的函数,这不可能是真的:)

写出你要找的东西的正确方法是

export default function wrapModule({ template }) {
  const prepend = template(`console.log("start of " + __filename);`);
  const append = template(`console.log("end of " + __filename);`);

  return {
    visitor: {
      Program(path, {opts}) {
        path.unshiftContainer("body", prepend());
        path.pushContainer("body", append());
      }
    }
  };
}
Run Code Online (Sandbox Code Playgroud)

通过使用unshiftContainerpushContainerBabel 可以将这些节点排队以供其他插件处理。这也用于template为您的两个片段生成 AST。