【问题标题】:How can I capture the output of a webpack build as a string before it is written to disk?如何在将 webpack 构建的输出写入磁盘之前将其捕获为字符串?
【发布时间】:2017-01-03 01:29:24
【问题描述】:

webpack 中似乎没有 output 选项可以将输出作为字符串写入 nodejs Bufferstdout。谷歌搜索没有产生任何有希望的结果。这可能通过配置或 3rd-party-module 实现吗?

【问题讨论】:

  • stat 对象是否足以满足您的需要? github.com/webpack/docs/wiki/node.js-api
  • 我看过了。我的主要目标是避免将文件写入磁盘,然后立即读取它以获取其字符串内容。

标签: node.js webpack babeljs


【解决方案1】:

经过研究,我认为实现此目的的唯一方法是猴子修补 webpack 的 NodeOutputFileSystem.writeFile 以捕获字符串内容。

我最终使用 raw babeljs 而不是 webpack 来获取字符串。

【讨论】:

    【解决方案2】:

    你可以write to an in-memory filesystem and read from it after compilation:

    const compiler = webpack({ / options / });
    const outputFileSystem = new webpack.MemoryOutputFileSystem()
    compiler.outputFileSystem = outputFileSystem;
    compiler.run((err, stats) => {
      // Read the output later:
      const content = outputFileSystem.readFileSync('...');
    });
    

    这涉及通过其 Node API 调用 webpack,但同样的操作也可以完全由 creating a custom webpack pluginwebpack.config.json 中完成,这会更改编译器在 apply 上的 outputFileSystem

    【讨论】:

      猜你喜欢
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多