Skip to content

Commit af8afb7

Browse files
authored
Merge pull request #319 from giuseppeg/fix/plugin-order
Fix order for plugin set with config and cli
2 parents 4616152 + bb3973a commit af8afb7

File tree

5 files changed

+55
-19
lines changed

5 files changed

+55
-19
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"build": "rimraf lib && babel src -d lib",
2424
"coverage": "nyc report --reporter=text-lcov | coveralls",
2525
"pretest": "npm run build",
26-
"test": "nyc ava test/test-cli.js --timeout=1m --verbose"
26+
"test": "nyc ava test/test-*.js --timeout=1m --verbose"
2727
},
2828
"files": [
2929
"lib/"

src/cfg-resolve.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,31 @@ export default ({input, flags = {}}) => {
1919
}
2020

2121
if (use) {
22-
use = [].concat(use).reduce((cfg, key) => mergeOptions(cfg, {plugins: {[key]: flags[toCamelCase(key)] || {}}}), {});
22+
const configPluginOptions = config?.plugins ?? {};
23+
24+
// Plugins defined via CLI options take precedence over the ones from config file.
25+
use = [].concat(use).reduce((cfg, name) => {
26+
let cliOptions = flags[toCamelCase(name)];
27+
let configOptions = configPluginOptions[name];
28+
// We merge this way because options can be both strings and objects.
29+
const merged = mergeOptions({ [name]: configOptions }, { [name]: cliOptions || {}});
30+
// Assigning as we loop `use` makes sure that the order in cfg.plugins is correct.
31+
cfg.plugins[name] = merged[name];
32+
if (configOptions) {
33+
delete configPluginOptions[name];
34+
}
35+
return cfg;
36+
}, { plugins: {} });
37+
38+
// Add the remaining plugins if there is any.
39+
if (config && config.plugins) {
40+
for (let name in configPluginOptions) {
41+
use.plugins[name] = configPluginOptions[name];
42+
}
43+
// Now all the plugins are in `use.plugins`.
44+
// Delete `config.plugins` for correct merging later: mergeOptions(config, {...}, use)
45+
delete config.plugins;
46+
}
2347
}
2448

2549
if (!config && !use) {

test/expected/output-bem.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<span class="MadTeaParty myComponent">
2-
<span class="MadTeaParty--march-title myTitle">Super Title</span>
3-
<span class="MadTeaParty--march-text myText">Awesome Text</span>
1+
<span class="myComponent MadTeaParty">
2+
<span class="myTitle MadTeaParty--march-title">Super Title</span>
3+
<span class="myText MadTeaParty--march-text">Awesome Text</span>
44
</span>

test/test-cfg-resolve.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,15 @@ test('should return config when CLI output param priority', t => {
145145

146146
t.deepEqual(config.output, expected);
147147
});
148+
149+
test('should resolve plugins set via config and stdin (use) in order', async t => {
150+
const input = 'input.html';
151+
const flags = {
152+
use: ['posthtml-d', 'posthtml-bem'],
153+
posthtmlBem: { foo: 'after' },
154+
posthtmlD: { bar: 'before' },
155+
config: 'test/config/.config-plugins'
156+
};
157+
const config = cfgResolve({input, flags});
158+
t.deepEqual(Object.keys(config.plugins), ['posthtml-d', 'posthtml-bem']);
159+
});

test/test-cli.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,31 @@ test('Check version', async t => {
2525
t.is(stdout, version);
2626
});
2727

28-
test('Transform html witch config in package.json', async t => {
28+
test('Transform html with config in package.json', async t => {
2929
t.plan(2);
3030
const filename = tempfile('.html');
3131
await execa(cli, ['test/fixtures/input.html', '-o', filename]);
3232
t.true(await pathExists(filename));
3333
t.is((await read('test/expected/output-config-pkg.html')), (await read(filename)));
3434
});
3535

36-
test('Transform html witch indent', async t => {
36+
test('Transform html with indent', async t => {
3737
t.plan(2);
3838
const filename = tempfile('.html');
3939
await execa(cli, ['test/fixtures/input-indent.html', '-o', filename]);
4040
t.true(await pathExists(filename));
4141
t.is((await read('test/expected/output-indent.html')), (await read(filename)));
4242
});
4343

44-
test('Transform html witch config in file', async t => {
44+
test('Transform html with config in file', async t => {
4545
t.plan(2);
4646
const filename = tempfile('.html');
4747
await execa(cli, ['test/fixtures/input.html', '-o', filename, '-c', 'test/fixtures/config.json']);
4848
t.true(await pathExists(filename));
4949
t.is((await read('test/expected/output-config-file.html')), (await read(filename)));
5050
});
5151

52-
test('Transform html witch dot config in file', async t => {
52+
test('Transform html with dot config in file', async t => {
5353
t.plan(2);
5454
const filename = tempfile('.html');
5555
await execa(cli, ['test/fixtures/input.html', '-o', filename, '-c', 'test/fixtures/.config']);
@@ -65,7 +65,7 @@ test('Transform html from two file', async t => {
6565
t.is((await read('test/expected/output-indent.html')), (await read(`${folder}/input-indent.html`)));
6666
});
6767

68-
// test('Transform html witch options replace', async t => {
68+
// test('Transform html with options replace', async t => {
6969
// t.plan(2);
7070
// const folder = await tempfile();
7171
// await copy(['test/fixtures/input.html', 'test/fixtures/input-indent.html'], folder);
@@ -74,7 +74,7 @@ test('Transform html from two file', async t => {
7474
// t.is((await read('test/expected/output-indent.html')), (await read(`${folder}/input-indent.html`)));
7575
// });
7676

77-
test('Transform html witch config in file and stdin options use', async t => {
77+
test('Transform html with config in file and stdin options use', async t => {
7878
t.plan(2);
7979
const filename = tempfile('.html');
8080
await execa(cli, [
@@ -94,7 +94,7 @@ test('Transform html witch config in file and stdin options use', async t => {
9494
t.is((await read('test/expected/output-bem.html')), (await read(filename)));
9595
});
9696

97-
test('Transform html witch stdin options use', async t => {
97+
test('Transform html with stdin options use', async t => {
9898
t.plan(2);
9999
const filename = tempfile('.html');
100100
await execa(cli, [
@@ -110,29 +110,29 @@ test('Transform html witch stdin options use', async t => {
110110
t.is((await read('test/expected/output-custom-elements.html')), (await read(filename)));
111111
});
112112

113-
test('Transform html witch stdin options use two key', async t => {
113+
test('Transform html with stdin options use two key', async t => {
114114
t.plan(2);
115115
const filename = tempfile('.html');
116116
await execa(cli, [
117117
'test/fixtures/input-bem.html',
118118
'-o',
119119
filename,
120120
'-u',
121-
'posthtml-custom-elements',
122-
'--posthtml-custom-elements.defaultTag',
123-
'span',
124-
'-u',
125121
'posthtml-bem',
126122
'--posthtml-bem.elemPrefix=--',
127123
'--posthtml-bem.modPrefix',
128124
'_',
129-
'--posthtml-bem.modDlmtr'
125+
'--posthtml-bem.modDlmtr',
126+
'-u',
127+
'posthtml-custom-elements',
128+
'--posthtml-custom-elements.defaultTag',
129+
'span'
130130
]);
131131
t.true(await pathExists(filename));
132132
t.is((await read('test/expected/output-bem.html')), (await read(filename)));
133133
});
134134

135-
test('Transform html stdin options use witch modules', async t => {
135+
test('Transform html stdin options use with modules', async t => {
136136
t.plan(2);
137137
const filename = tempfile('.html');
138138
await execa(cli, [

0 commit comments

Comments
 (0)