2016-10-07 19 views

回答

3

应该可以将编译器配置为使用替换的内存中的文件系统,并在编译完成后检索输出。

下面是一个简单compile()函数返回,将解决输出文件的内容的承诺:

const MemoryFs = require('memory-fs') 
const webpack = require('webpack') 

function compile() { 
    const compiler = webpack({ 
    output: { 
     filename: 'bundle.js', 
     path: '/' 
    } 
    }) 

    compiler.outputFileSystem = new MemoryFs() 

    return new Promise((resolve, reject) => { 
    compiler.run((err, stats) => { 
     if (err) return reject(err) 

     if (stats.hasErrors() || stats.hasWarnings()) { 
     return reject(new Error(stats.toString({ 
      errorDetails: true, 
      warnings: true 
     }))) 
     } 

     const result = compiler.outputFileSystem.data['bundle.js'].toString() 
     resolve({result, stats}) 
    }) 
    }) 
} 
相关问题