posthtml / posthtml-cli

CLI for PostHTML
MIT License
29 stars 18 forks source link

Plugin order when merging CLI opts and config file #318

Closed giuseppeg closed 4 years ago

giuseppeg commented 4 years ago

If you have a config file say posthtml.config.js

{
  plugins: {
    'posthtml-a': { someOption: true }
  }
}

and set another plugin via CLI and pass other options for posthtml-a

posthtml *.html -u posthtml-d -u posthtml-a --posthtml-a.foo 3 -c posthtml.config.js

I'd expect the CLI plugins to be applied first but instead posthtml-cli merges the config and the CLI options in the wrong order:

{
  plugins: {
    'posthtml-a': { someOption: true, foo: 3 },
    'posthtml-d': {}
  }
}

Notice that posthtml-a now comes before posthtml-d. This is because the merging target is the config file so the keys orders is mandated by the config file. The desiderable ordered would be

{
  plugins: {
    'posthtml-d': {},
    'posthtml-a': { someOption: true, foo: 3 }    
  }
}

because that's the order in the CLI arguments.

possible solutions

Merge everything manually and in order here https://github.com/posthtml/posthtml-cli/blob/461615213ccd1bb66817582f9f42572d34946cfb/src/cfg-resolve.js#L21-L23

and then delete config.plugins so that when at the end of the function we do the final merging config doesn't have plugins.

Scrum commented 4 years ago

@giuseppeg Thanks for this great request 👍