Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何使用Node将webpack输出为字符串_Node.js_Webpack - Fatal编程技术网

Node.js 如何使用Node将webpack输出为字符串

Node.js 如何使用Node将webpack输出为字符串,node.js,webpack,Node.js,Webpack,我正在尝试使用Webpack捆绑一堆文件。我的节点代码中有以下内容 webpack({ entry: "./src/test", output: { path: __dirname, filename: "bundle.js" }, }, function(err, stats){ console.log("I would like to output the created js here"); }) 创建一个名为b

我正在尝试使用Webpack捆绑一堆文件。我的节点代码中有以下内容

  webpack({
    entry: "./src/test",
    output: {
        path: __dirname,
        filename: "bundle.js"
    },
  }, function(err, stats){
    console.log("I would like to output the created js here");
  })

创建一个名为bundle.js的文件很好,但我不知道如何以字符串形式输出。

基本上,您可以读取该文件,然后根据需要使用它
e、 g


希望对您有所帮助。

谢谢,我正在寻找一种解决方案,该解决方案不需要我将其输出到磁盘,但目前可以正常工作。谢谢
import webpack from 'webpack';
const config = require('../webpack.config');

const compiler = webpack(config);

compiler.run((err, stats) => {  
    const data = stats.toJson();

    const app = data.assetsByChunkName.app[0] //here you can get the file name  
    // if you don't have chunks then you should use data.assets;

    const file = fs.readFileSync('path to your output ' + app); //read the file
    //now you can work with the file as you want.
});

//Basic webpack.config.js
module.exports = {
  devtool: 'source-map',
  entry: {
    app: 'Some path' // you can have different entries.
    entrie2 : ''
    .... more entries
  },
  output: {
    path: 'Some path'
  }
}