Skip to content
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

Make the current command's name available to plugins #2692

Closed
m0dch3n opened this issue Oct 8, 2018 · 5 comments
Closed

Make the current command's name available to plugins #2692

m0dch3n opened this issue Oct 8, 2018 · 5 comments

Comments

@m0dch3n
Copy link

m0dch3n commented Oct 8, 2018

What problem does this feature solve?

I'm currently developing a vue cli 3 plugin, to add cordova, to a vue project. For the serve and build, I created new commands in my generator.js

api.extendPackage({
    scripts: {
      'cordova-serve-android': 'vue-cli-service cordova-serve-android',
      'cordova-build-android': 'vue-cli-service cordova-build-android',
      'cordova-serve-ios': 'vue-cli-service cordova-serve-ios',
      'cordova-build-ios': 'vue-cli-service cordova-build-ios',
      'cordova-serve-browser': 'vue-cli-service cordova-serve-browser',
      'cordova-build-browser': 'vue-cli-service cordova-build-browser'
    },

And in my index new defaultModes for theses commands:

const defaultModes = {
  'cordova-serve-android': 'cordova-serve-android',
  'cordova-build-android': 'cordova-build-android',
  'cordova-serve-ios': 'cordova-serve-ios',
  'cordova-build-ios': 'cordova-build-ios',
  'cordova-serve-browser': 'cordova-serve-browser',
  'cordova-build-browser': 'cordova-build-browser'
}

The problem is now, if I ran vue-cli-service cordova-build-android, my process.env.NODE_ENV is development, because of loadEnv.

Setting my defaultModes to :

const defaultModes = {
  'cordova-serve-android': 'development',
  'cordova-build-android': 'production',
  'cordova-serve-ios': 'development',
  'cordova-build-ios': 'production',
  'cordova-serve-browser': 'development',
  'cordova-build-browser': 'production'
}

would correctly set the mode, however, in my plugin, during

api.chainWebpack(webpackConfig => {
}

and

api.configureDevServer(app => {
}

I no longer know, which mode (android, ios, browser), I'm using then.

My Plugin is available under https://github.com/m0dch3n/vue-cli-plugin-cordova to better understand, what I'm talking here about.

What does the proposed API look like?

It would be great, to have:

api.service.commandName = 'cordova-build-android'

i.e. if I ran vue-cli-service cordova-build-android

@m0dch3n
Copy link
Author

m0dch3n commented Oct 8, 2018

PS: I know I could create a file .env.cordova-build-android with NODE_ENV=production, however I prefer, if my plugin can solve this, without having to create additional files during generator.js...

What do you think, if the constructor of Service is refactored, to something like :

constructor (context, commandName, { plugins, pkg, inlineOptions, useBuiltIn } = {}) {
  this.commandName = commandName
  ...
}

and then in vue-cli-service.js

const service = new Service(process.env.VUE_CLI_CONTEXT || process.cwd(), command)

@LinusBorg
Copy link
Member

LinusBorg commented Oct 8, 2018

You can call api.chainWebpack inside of the registered command.

That way you have all the info you need...

@m0dch3n
Copy link
Author

m0dch3n commented Oct 9, 2018

Thanks, this was also solving my problem.

I thought, chainWebpack i.e needed to be registered before the command is executed, so I didn't try this.

You can close the issue, if you want, or if you still find this feature useful in other scenarios, you can keep it open...

@LinusBorg
Copy link
Member

LinusBorg commented Oct 9, 2018

I'll still keep it open as I think it really might be useful somewhere.

@LinusBorg LinusBorg changed the title access everywhere to command name in a cli plugin Make the current command's name available to plugins Oct 17, 2018
@LinusBorg
Copy link
Member

So this doesn't gain any traction by uers it seems, and I didn't come across a real usecase myself, so I'll close it.

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

2 participants