0

Dеar wеbpack gurus.

Is there a way to bundle my project using webpack without writing output to a file, but rather get the resulting bundle module as a variable in my js scope?

Something like that would be perfect:

const webpack = require('webpack');
webpackConfig.outputFormat = 'localVariable';
const compiler = webpack(webpackConfig);
compiler.compileLocally((err, locallyBundledApp) => {
    const methodResult = locallyBundledApp.someAppMethod();
    console.log('app method result: ', methodResult);
});

I was looking for this in the docs, but the only compilation output available in js scope mentioned there was stats.

My use case: I need to run a jest test that tests a code depending on a non-standard js flavour (PEG.js) scripts. There is a webpack loader for that, but without webpack this code can't be run - so I need to build the project bundle at the start of the test with webpack and then run my test assertions on the compiled bundle.

I can of course output to something like /tmp/ololo-bundle.js and then do

const bundledApp = require('/tmp/ololo-bundle.js');

but is this really the only way? Can't writes to file system be avoided in a webpack build?

Klesun
  • 12,280
  • 5
  • 59
  • 52
  • Somewhat related [Require JS files dynamically on runtime using webpack](https://stackoverflow.com/questions/30575060/require-js-files-dynamically-on-runtime-using-webpack) – Klesun Dec 18 '20 at 19:06
  • Nah, I ended up writing bundle to the temporary file after all, seems like the simplest solution after all the googling... – Klesun Dec 18 '20 at 19:28

0 Answers0