gpt4 book ai didi

webpack - 有没有办法将 webpack、Node API 的输出作为字符串获取?

转载 作者:行者123 更新时间:2023-12-05 00:17:34 25 4
gpt4 key购买 nike

是否有保存已编译包的 stats 或编译器的属性?

var compiler = require('webpack')({
entry: entry_point,
resolve: {
modulesDirectories: modules,
extensions: ['', '.js']
},
stats: {
colors: true,
progress: true,
hash: true
}
}, function(err, stats) {
// compiler
// stats

});

最佳答案

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

这是一个样本 compile()返回将解析输出文件内容的 promise 的函数:

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})
})
})
}

关于webpack - 有没有办法将 webpack、Node API 的输出作为字符串获取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39923743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com