Change source code of a module via AST from a WebPack 4 plugin

試著忘記壹切 提交于 2019-12-07 17:28:07

问题


I have the following JS file:

// hello-foo.js
console.log('foo')

I want to replace 'foo' with 'bar' with webpack. I have the following WebPack plugin for that:

class MyPlugin {

  constructor() {}
  apply(compiler) {

    compiler
      .hooks
      .compilation
      .tap('MyPlugin', (compilation, {normalModuleFactory}) => {

        normalModuleFactory
          .hooks
          .parser
          .for('javascript/auto')
          .tap('MyPlugin', (parser) => {

            parser
              .hooks
              .program
              .tap('MyPlugin', (ast, comments) => {

                ast.body[0].expression.arguments[0].value = 'bar'

                // ast.body[0].expression.arguments[0].raw = 'bar' // does not make any difference :(

              })

          })

      })
  }
}

I debugged webpack/lib/Parser.js, it got back the updated AST, but it just got ignored when emitting the bundle.

I know that for the above trivial example a loader might be a better option, but I am specifically interested in reusing WebPack's AST, if it is possible. In other words, I don't want to parse the module with Babel first, then have it re-parsed again by WebPack/Acorn.

There is a similar question here already, but I believe that it relates to an earlier version of WebPack.

来源:https://stackoverflow.com/questions/51659421/change-source-code-of-a-module-via-ast-from-a-webpack-4-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!