bem-sdk icon indicating copy to clipboard operation
bem-sdk copied to clipboard

Add method to extract level configuration from path

Open Vittly opened this issue 7 years ago • 6 comments

In webpack-bem-plugin I want to get preset from path to specific file in project.

Firstly I thought that levelSync solves my problem. But It returns "nearest parent config".

Then I read Readme. This part:

"levels": [
    {
        "path": "path/to/level",
        "scheme": "nested" // < --- Look here. May be "preset" or "naming" should be there
    }
],

There is no method to get options for specific layer. My suggestions:

  • add method to return level declaration by path to file
  • add method to return level declaration by level (if naming.file.parse return level from path)
  • tell me that "preset" or "naming" is project level option and put it in the root of .bemrc + add fix to readme

Vittly avatar Feb 04 '18 17:02 Vittly

cc @tadatuta We need your expertise

qfox avatar Feb 05 '18 19:02 qfox

You are right, naming is project level option and can be in the root of .bemrc. Added it to example in readme: https://github.com/bem/bem-sdk/pull/286

I thought that levelSync solves my problem. But It returns "nearest parent config".

Actually it's not parent config but the merge of all the declarations for the level from all the configs (almost like Object.assign(levelDeclFromTopLevelConf, levelDeclFromNearestLevelConf)).

There's possibility to work with raw data with config method but for sure it's not convenient.

tadatuta avatar Feb 26 '18 10:02 tadatuta

Actually it's not parent config but the merge of all the declarations for the level from all the configs

Yes but except fields defined in levelConf. Lets write simple example:

// .bemrc
{
  prop1: 'foo',
  levels: [{ layer: 'bar', prop2: 'baz' }]
}

// code
const BemConf = require('@bem/sdk.config');
const config = new BemConf();

config.getLevelSync('bar');
// returns { prop1: 'foo' } <---- where is my prop2 ?

Issue is about two things:

  1. naming should be top level property in example. Please fix https://github.com/bem/bem-sdk/pull/286/files#r170572472
  2. somewhere should be a way to get properties from level declaration. Is it?

Vittly avatar Feb 26 '18 12:02 Vittly

the issue here is that level() and levelSync() still expect path to level as an argument not layer name (see: https://github.com/bem/bem-sdk/blob/master/packages/config/index.js#L265).

so it your example config.levelSync('bar'); is equivalent to config.levelSync('anythingElse'); and returns just common config. with path is works how expected (https://github.com/bem/bem-sdk/pull/286/files#r170595399).

what we need is to support both path (as fallback) and layer as arguments for level methods.

tadatuta avatar Feb 27 '18 06:02 tadatuta

Agree 👍 Here is noted that path is deprecated also

Vittly avatar Feb 27 '18 13:02 Vittly

what we need is to support both path (as fallback) and layer as arguments for level methods.

Wait, what you mean about layer as arguments? We still have levels with one or more layers. And they are still can have separate config.

qfox avatar Mar 01 '18 00:03 qfox