Jackie
Jackie

Reputation: 23519

How do I output webpack as a string using Node

I am trying to use Webpack to bundle a bunch of files. I have the following in my node code...

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

This works fine creating a file called bundle.js but I can't figure out how to output as a string instead.

Upvotes: 4

Views: 865

Answers (1)

Hosar
Hosar

Reputation: 5292

Basically what you can do is to read the file, and then work with it as you want.
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'
  }
}

Hope this help.

Upvotes: 2

Related Questions