Skip to content

Programmatic api for vue-cli-service build/serve that accepts custom webpack config #1551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nklayman opened this issue Jun 12, 2018 · 8 comments

Comments

@nklayman
Copy link

What problem does this feature solve?

For a new version of vue-cli-plugin-electron-builder I need to be able to modify the webpack config only if my build command is called, not for the regular build. I need to customize the config to work with electron, but then it won't work in browser. Being able to api.build(config) is necessary for my plugin. api.serve(config) is not necessary, but would be quite nice. I could possibly add this myself and submit a PR, but I want to make sure this is wanted as a feature.

This kinda relates to this comment.

What does the proposed API look like?

Some other features I would like:

  • events for recompiling on serve command
  • being able to get info such as hosted port, compile errors, and other info like that provided by webpack()
  • bundle data is still shown in UI page

essentially a api similar to that of webpack(), but that includes the extra steps of vue-cli-service build/serve (because webpack(api.resolveWebpackConfig()) throws errors).

@yyx990803
Copy link
Member

yyx990803 commented Jun 12, 2018

You can customize the config by doing:

const config = api.resolveChainableWebpackConfig()

// apply config changes via chaining

const finalConfig = config.toConfig()

// apply raw modifications if necessary

const compiler = webpack(finalConfig)

For recompiling events you can do so by injecting a custom webpack plugin.

There are a number of things of the current build command that is specific to web targets, so you will have to use the lower level APIs to implement your platform specific build command.

@nklayman
Copy link
Author

If I do that I get a bunch of errors. The build by vue-cli includes some extra steps that prevent theses errors from happening.

@yyx990803
Copy link
Member

I think you will have to better explain the exact API you need. It's pretty vague right now.

@nklayman
Copy link
Author

I created a pr for the build command. The api.service.run('serve') works just fine for this plugin.

@LinusBorg
Copy link
Member

Closing as Author closed his own PR.

@m4heshd
Copy link

m4heshd commented Jan 16, 2021

@nklayman is there any way I can invoke serve and launch an Electron instance after dev server starts without writing any complicated Vue plugin level stuff? I've read through your source but still can't figure out. Even posted a question here.

@nklayman
Copy link
Author

@m4heshd
Copy link

m4heshd commented Jan 17, 2021

@nklayman thank you for taking time to look through my issue. I actually went through the vue-cli source and ended up doing this which exactly does what I need to.

const vueService = require('@vue/cli-service');
const {info, done, error} = require('@vue/cli-shared-utils');
const service = new vueService(process.cwd());

service.init("development");
service.run('serve').then(({server, url}) => {
    info('Launching Electron...');

    let electron = spawn(path.join('node_modules', '.bin', process.platform === 'win32' ? 'electron.cmd' : 'electron'), ['electron-main.js'], {stdio: 'inherit'});

    electron.on('exit', function (code) {
        process.exit();
    });
})

I wish this stuff were documented. I loved your plugin but after few weeks I just got extremely frustrated, mostly with webpack and completely gave up on the project I was working on to do more research. Went down this rabbit hole of research and ended up creating my own simplistic, fast & effective boilerplate for Vue + Electron now I'm super comfortable with 😊. Your plugin and the community around it really helped me with understanding most of Vue internals and I'm really grateful for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants