bem-sdk-archive / bem-deps

🎯 Manage BEM dependencies. DEPRECATED β†’
https://github.com/bem/bem-sdk/tree/master/packages/deps
Other
10 stars 5 forks source link

Resolve Deps #2

Closed blond closed 8 years ago

blond commented 9 years ago

API

The resolve method to supplement the declaration missing entities.

Important: It takes into account the order dependencies of BEM-entities.

resolve(decl, deps) -> decl

Object[] decl β€” list of BEM-entities. Each item is object. See, bem-naming. Object[] deps β€” list of dependencies between BEM-entities.

Example:

var deps = require('bem-deps'),
    decl = [
        { block: 'A' },
        { block: 'B' }
    ],
    myDeps = [
        { 
            entity: { block: 'A' },
            dependOn: [
                {
                    entity: { block: 'B' },
                    order: 'dependenceBeforeDependants'
                },
                {
                    entity: { block: 'C' }
                }
            ]
        }
    ];

var res = deps.resolve(decl, myDeps);

console.log(res); 
/*
{ 
    entities: [{ block: 'B' }, { block: 'A' }, { block: 'C' }],
    dependOn: []
}
*/

Terms

Each item of deps list is Object.

Object entity β€” BEM-entity that is dependent on other Object[] dependOn β€” list of BEM-entities of which depends on entity

Example:

{
    entity: { block: 'A' },
    dependOn: [
        { entity: { block: 'B' } },
        { entity: { block: 'C' } }
    ]
}

This means that the block A depends on blocks B and C.

Techs

Constraints can be not only between the BEM-entities but also between technologies.

resolve(decl, deps, { tech: 'css' })

Example:

Tech -> Block

{
    entity: { block: 'A' },
    tech: 'css',
    dependOn: [
        { entity: { block: 'B' } }
    ]
}

This means that the css tech of block A depends on all techs of block B.

Example:

Tech -> Tech

{
    entity: { block: 'A' },
    tech: 'js',
    dependOn: [
        { 
            entity: { block: 'B' },
            tech: 'js'
        }
    ]
}

This means that the js tech of block A depends on js tech of block B.

Deps By Techs

For cases when the technology is dependent on other technology in result will be add dependOn field. Each item of dependOn is object with tech and entities filed.

Example:

var myDeps = {
    entity: { block: 'A' },
    tech: 'js',
    dependOn: [
        { 
            entity: [{ block: 'B' }],
            tech: 'bemhtml'
        }
    ]
};

var res = resolve(decl, deps, { tech: 'js' });

console.log(res);

/*
{
    entities: [{ block: 'A' }],
    dependOn: [
        {
            tech: bemhtml,
            entities: [{ block: 'B' }]
        }
    ]
}
*/

Order

Depending provides information about the order:

Example:

{
    entity: { block: 'A' },
    tech: 'js',
    dependOn: [
        { 
            entity: { block: 'B' },
            order: 'dependenceBeforeDependants'
        }
    ]
}

This means that the block A depends on of block B and require that B will be before A.

Priority

Rule 1: Try to keep the order of declaration.

Example:

Declaration: A, B, C Dependency graph: B <- E (with order: dependenceBeforeDependants) Expected result: A, E, B, C

Rule 2: Try to keep the natural order for BEM-entities with same block scope (block and its mods, elems and their mods):

arikon commented 9 years ago
/*
{
    entities: [{ block: 'A' }],
    dependOn: [
        {
            tech: bemhtml,
            entities: [{ block: 'B' }]
        }
    ]
}
*/

I don't like that in the resulting deps info about order of tech-dependencies is lost, and these deps are grouped by techs.

Proposing another resulting format (it could be optional):

/*
{
    entities: [{ block: 'A' }, { block: 'B', tech: 'bemhtml' }]
}
*/
blond commented 9 years ago

I don't like that in the resulting deps info about order of tech-dependencies is lost, and these deps are grouped by techs.

No, this information is not lost. It is important to understand why we need result.

If we need to build JavaScript then dependencies with tech: js will get in entities and order will not be lost.

var myDeps = {
    entity: { block: 'A' },
    tech: 'js',
    dependOn: [
        { 
            entity: [{ block: 'B' }],
            order: 'dependenceBeforeDependants',
            tech: 'js'
        }
    ]
};

var res = resolve(decl, deps, { tech: 'js' });

console.log(res);

/*
{
    entities: [{ block: 'B' }, { block: 'A' }],
    dependOn: []
}
*/

If we need to build JavaScript with BEMHTML then dependencies with tech: 'bemhtml' will get in dependOn.

var myDeps = [
    {
        entity: { block: 'A' },
        tech: 'js',
        dependOn: [
            { 
                entity: [{ block: 'B' }],
                tech: 'bemhtml'
            },
            { 
                entity: [{ block: 'C' }],
                tech: 'bemhtml'
            }
        ]
    },
    {
        entity: { block: 'C' },
        tech: 'bemhtml',
        dependOn: [
            { 
                entity: [{ block: 'D' }],
                tech: 'bemhtml'
            }
        ]
    },
];

var res = resolve(decl, deps, { tech: 'js' });

console.log(res);

/*
{
    entities: [{ block: 'A' }],
    dependOn: [
        {
            tech: bemhtml,
            entities: [{ block: 'B' }, { block: 'C' }]
        }
    ]
}
*/

res.entities // we can concat JS files

var bemhtmlForJsDecl = res.dependOn[0].entities;
var bemhtmlForJsRes = resolve(decl, deps, { tech: 'bemhtml' });

console.log(bemhtmlForJsRes);

/*
{
    entities: [{ block: 'B' }, { block: 'C' }, { block: 'D' }],
    dependOn: []
}
*/

bemhtmlForJsRes.entities // we can compile BEMHTML files
arikon commented 9 years ago

What if I need to mix files of different techs in a single flow?

blond commented 9 years ago

@arikon You're talking about different technologies or different extensions?

Can you give real-life examples?

arikon commented 9 years ago

@blond Different techs and exts

Can you give real-life examples?

No =(

blond commented 9 years ago

Different techs and exts

You can resolve different exts at the time of parsing.

For different techs I think that everything should remain as it is. Real-life example β€” js+bemhtml.

blond commented 8 years ago

Fixed in #59