Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do You Run Webpack from a JavaScript file?

As we all know, you can run Webpack from the command line using the CLI by running,

webpack

Which reads the file webpack.config.js and compiles your files accordingly.

Is it possible to do this same thing from within a file? In other words, is it possible to run Webpack without using the CLI?

like image 475
Code Whisperer Avatar asked Feb 25 '17 18:02

Code Whisperer


People also ask

How do I run a webpack locally?

To run the local installation of webpack you can access its binary version as node_modules/. bin/webpack . Alternatively, if you are using npm v5. 2.0 or greater, you can run npx webpack to do it.

How do I include a JavaScript file in webpack?

You can bundle your JavaScript using the CLI command by providing an entry file and output path. Webpack will automatically resolve all dependencies from import and require and bundle them into a single output together with your app's script.

What is the use of webpack in JavaScript?

This is why webpack exists. It's a tool that lets you bundle your JavaScript applications (supporting both ESM and CommonJS), and it can be extended to support many different assets such as images, fonts and stylesheets.


1 Answers

Yes.

https://webpack.js.org/api/node/

const webpack = require("webpack");

webpack({
  // Configuration Object
}, (err, stats) => {
  if (err || stats.hasErrors()) {
    // Handle errors here
  }
  // Done processing
});
like image 91
Paul Avatar answered Sep 20 '22 12:09

Paul