4

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.

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I was looking for a solution that didn't require me to output it to disk but for now this works. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.