在 webpack AMD 输出中重命名定义函数

Rename define function in webpack AMD output

从 webpack 输出 AMD 模块时,我需要更改输出包中使用的 define 函数的名称。

这是因为我正在向库提供一个脚本,该库将自己的 define 函数命名为 Foo.define

给定 AMD 输出 webpack 配置:

module.exports = {
  //...
  output: {
    library: 'MyLibrary',
    libraryTarget: 'amd'
  }
};

而不是输出:

define('MyLibrary', [], function() {
    return _entry_return_;
});

Webpack 会输出如下内容:

Foo.define('MyLibrary', [], function() {
    return _entry_return_;
});

谷歌搜索 2 小时后,我仍然找不到 webpack 或相关插件的正确配置,所以我在这里问。

感谢帮助!

编辑:

由于以下答案,最终这就是有效的方法:

plugins: [].concat(plugins, [
  new ReplaceInFileWebpackPlugin([
    {
      dir: path.resolve(__dirname, 'public/questions-flow-chart'),
      test: [/bundle\.js$/],
      rules: [
        {
          search: /^define/,
          replace: 'Foo.define'
        }
      ]
    }
  ])
])

尝试使用replace-in-file-webpack-plugin

类似的东西

    plugins: [
        new ReplaceInFileWebpackPlugin([{
            test: [/\.js$/, /\.ts/],
            rules: [{
                search: /define/ig,
                replace: 'foo.define'
            }]
        }])
    ]