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?

2
  • Somewhat related Require JS files dynamically on runtime using webpack Commented Dec 18, 2020 at 19:06
  • Nah, I ended up writing bundle to the temporary file after all, seems like the simplest solution after all the googling... Commented Dec 18, 2020 at 19:28

0

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.