FormidableLabs / webpack-dashboard

A CLI dashboard for webpack dev server
MIT License
13.87k stars 363 forks source link

Run webpack dashboard from .js file. #339

Open Saikedo opened 2 years ago

Saikedo commented 2 years ago

Is it possible to use this plugin if I am executing my webpack from non-cli environment? I run my webpack compiler from a .ts file without cli scripts and it seems like in those cases DashboardPlugin does not output any info.

ryan-roemer commented 2 years ago

Could you provide a minimal sample repository showing what you’re trying / would want to work?

Saikedo commented 2 years ago

So basically this is what I am trying (Not my actual setup, just a bare minimum to show the steps)

//webpack.prod.ts
const DashboardPlugin = require('webpack-dashboard/plugin');
...

const prodConfig: webpack.Configuration = {
  mode: 'production',
  devtool: 'source-map',
  entry: {
    ...
  },
  output: {
   ...
  },
  plugins: [
    new DashboardPlugin(),
  ],
};

export default prodConfig;

and then I have this file that handles the build process(Not using a cli because the actual file does much more than shown here)

//buildWeb.ts

webpack(webpackConfig, (error, stats) => {
     console.warn('Success\n', stats.toString());
});

And in my package.json, I execture my buildWeb.ts by doing

"build-web": "npx ts-node src/scripts/buildWeb.ts",

This structure currently does not display anything inside the console.

one possible approach would be to be able to pass a callback to DashboardPlugin that will return something that I can use to construct the dashboard in the console window. I was initially thinking handler option is exactly for that but I was unable to leverage that in any meaningful way.

Saikedo commented 2 years ago

I actually just realized that adding the following to my buildWeb.ts starts displaying the dashboard, but the dashboard just shows up empty and does not update the elements.

const Dashboard = require('webpack-dashboard/dashboard/index');
const dashboard = new Dashboard ();

// and then use the plugin with new DashboardPlugin(dashboard.setData),

ryan-roemer commented 2 years ago

I have an example running the dashboard from a TS config webpack config file for this PR: https://github.com/FormidableLabs/webpack-dashboard/pull/341

Just checkout the repository and run yarn dev-ts. You can look at what that command has in it as well as the example TS config file there for your reference.

If you're still hitting an error, can you please open up a minimal repository with what you'd expect (or like) to work as an example, so I can install + build and see the failure myself? This makes it much easier to debug rather than guessing as to your setup... Thanks!

ryan-roemer commented 2 years ago

Oh, reading this more -- the dashboard currently relies on inter-process communication between a webpack process and the dashboard (really a websocket server that pretty prints stuff).

Maybe what you need is something like:

"build-web": "webpack-dashboard -- ts-node src/scripts/buildWeb.ts"