Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute my own script after every webpack's auto build in watch mode of vue-cli 3.x?

My current situation

Now I am using [email protected]. For some reason, I need to watch file change of my source code to run webpack's build, with vue-cli-service build --watch.

My current solution

Currently, I run another Node.js process to watch file change, of webpack's bundle. I really suffered from this terrible development experience.

Compare with vue-cli 2.x

When I used vue-cli 2.x, I actually run webpack(), one native API of webpack, in build/build.js, so I could use webpack().watch() instead to run build and pass my own script as callback function. However in vue-cli 3.x, there's no way and no need to approach the webpack's native API, within my knowledge.

Summary

I wish to run my own script after webpack's every auto build, though I could not find any guidance in vue-cli's official document.

like image 740
Array-Huang Avatar asked Oct 10 '19 14:10

Array-Huang


People also ask

How do I run VueJs project on localhost?

Open in browser To view the project, open a tab and type http://localhost:3000 into the URL bar. That's the address that the Node server is listening to.

What is vue-cli-service lint?

vue-cli-service lintLints and fixes files. If no specific files are given, it lints all files in src and tests , as well as all JavaScript files in the root directory (these are most often config files such as babel. config. js or . eslintrc.

How do I run VueJs code?

With your project open the command palette by using the menu item View > Command Palette or by using the keyboard shortcut Shift + CMD + P or Shift + CTRL + P on Windows. From there type Tasks and click on the Run Task command. This will examine your project and give you a list of the available scripts to run.


2 Answers

From my understanding - you have a Webpack plugin use case. Just like for example webpack-build-notifier sends a notification after a rebuild.

I am not a Webpack plugin author, but this is already working for me:

// vue.config.js
const ArbitraryCodeAfterReload = function(cb) {
  this.apply = function(compiler) {
    if (compiler.hooks && compiler.hooks.done) {
      compiler.hooks.done.tap('webpack-arbitrary-code', cb);
    }
  };
};

const myCallback = function() {
  console.log('Implementing alien intelligence');
};

const plugins = [];
const isDev = process.env.NODE_ENV === 'development';
if (isDev) {
  plugins.push(new ArbitraryCodeAfterReload(myCallback));
}

module.exports = {
  configureWebpack: {
    plugins
  }
};

If this is not the right compilation step - the Webpack documentation should somewhere have the right hook for your use case.

Maybe there is already a plugin available which already does what you need...

like image 197
madflow Avatar answered Oct 18 '22 12:10

madflow


Maybe this can help you. This is just an example. You only need to use &&

npm run start && npm run build

So after the npm run start script execute your npm run build script will run after the first one

Update you can use this package webpack-shell-plugin

const WebpackShellPlugin = require('webpack-shell-plugin');

new WebpackShellPlugin({
  onBuildStart: [''],
  onBuildEnd: ['']
})
like image 41
Tony Ngo Avatar answered Oct 18 '22 14:10

Tony Ngo