2017-02-17 8 views
0

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

これは細かいbundle.jsという名前のファイルを作成するに動作しますが、私が代わりに文字列としてどのように出力に把握することはできません。

答えて

1

基本的には、ファイルを読んでから、必要に応じて作業してください。

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

このヘルプが必要です。

+0

ありがとう、私はディスクに出力する必要はありませんでしたが、今のところこれが機能するソリューションを探していました。ありがとう! – Jackie

関連する問題