Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions packages/gulp-bem-src/lib/build-bem-graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,16 @@ const readDeps = deps.read();
const parseDeps = deps.parse();
const buildGraph = deps.buildGraph;

const getDepFiles = (introspection, levels) => {
return introspection.filter(file => file.tech === 'deps.js')
// Сортируем по уровням
.sort((f1, f2) => (levels.indexOf(f1.level) - levels.indexOf(f2.level)))
};
const loadDeps = async (introspection) => {
const depFiles = Array.from(introspection.techFiles('deps.js'));

const loadDeps = async (introspection, levels) => {
const depFiles = getDepFiles(introspection, levels);
const depFilesContents = await readDeps(depFiles);

return parseDeps(depFilesContents);
}
};

module.exports = async (introspection, levels) => {
const deps = await loadDeps(introspection, levels);
module.exports = async (introspection) => {
const deps = await loadDeps(introspection);

return buildGraph(deps);
};
20 changes: 14 additions & 6 deletions packages/gulp-bem-src/lib/gulp-bem-src.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const bemConfig = require('@bem/sdk.config');
const File = require('vinyl');
const read = require('gulp-read');

const introspectFs = require('./introspect-fs');
const introspectLevels = require('./introspect-levels');
const buildBemGraph = require('./build-bem-graph');
const resolveDeps = require('./resolve-deps');
const filesToStream = require('./files-to-stream');
Expand All @@ -34,11 +34,18 @@ async function _getBundleInfo(sources, decl, tech, options) {
const config = options.config || bemConfig();

// Получаем слепок файловой структуры с уровней
const introspection = await introspectFs(sources, config);
const graph = options.skipResolvingDependencies ? null : await buildBemGraph(introspection, sources)
const fulldecl = options.skipResolvingDependencies ? declToEntities(decl, tech) : resolveDeps(decl, graph, tech);
const introspection = await introspectLevels(sources, config);

return { introspection, graph, fulldecl };
if (options.skipResolvingDependencies) {
const fulldecl = declToEntities(decl, tech);

return { introspection, fulldecl };
}

const graph = await buildBemGraph(introspection);
const fulldecl = resolveDeps(decl, graph, tech);

return { introspection, fulldecl };
}

/**
Expand Down Expand Up @@ -140,7 +147,8 @@ function harvest(opts) {
}, {});

const res = [], depTechForFile = {};
for (const file of opts.introspection) {

for (const file of opts.introspection.files()) {
if (file.tech && !fileTechToDep[file.tech] && !techMap[file.tech]) {
techMap[file.tech] = [file.tech];
fileTechToDep[file.tech] = [file.tech];
Expand Down
22 changes: 0 additions & 22 deletions packages/gulp-bem-src/lib/introspect-fs.js

This file was deleted.

1 change: 1 addition & 0 deletions packages/gulp-bem-src/lib/introspect-levels/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./introspect-levels');
49 changes: 49 additions & 0 deletions packages/gulp-bem-src/lib/introspect-levels/introspect-level.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';

const fs = require('fs');
const { Writable } = require('stream');

const walk = require('@bem/sdk.walk');
const pify = require('pify');

/**
* @param {string} levelPath
* @param {*} bemConfig
*/
module.exports = async (levelPath, bemConfig) => {
const levelMap = await Promise.resolve(bemConfig.levelMap ? bemConfig.levelMap() : {});

return new Promise((resolve, reject) => {
const entityMap = new Map();

walk([levelPath], levelMap)
.on('error', reject)
.pipe(new Writable({
objectMode: true,
write(file, encoding, callback) {
tryCatch(async () => {
const id = file.entity.id;
const stats = await pify(fs.stat)(file.path);

file.stats = stats;

const entityFiles = entityMap.has(id) ? entityMap.get(id) : entityMap.set(id, new Set()).get(id);
entityFiles.add(file);

callback();
}, callback);
}
}))
.on('error', reject)
.on('finish', () => resolve(entityMap));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on → once?

});
};

// try-catch optimization
function tryCatch(tryFn, catchFn) {
try {
tryFn();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets test, I think it may not catch async functions

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wont' catch throws in callbacks, but will catch async functions afair.

} catch (err) {
catchFn(err);
}
}
30 changes: 30 additions & 0 deletions packages/gulp-bem-src/lib/introspect-levels/introspect-levels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const fs = require('fs');
const { Writable } = require('stream');

const introspectLevel = require('./introspect-level');
const Introspection = require('./introspection');

const levelCache = {};

/**
* @param {string[]} levelPaths
* @param {*} bemConfig
* @returns {Introspection}
*/
module.exports = async (levelPaths, bemConfig, { cache=false } = {}) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSDoc doesn't has options and cache description.

const levelIntrospections = await Promise.all(levelPaths.map(levelPath => {
if (cache && levelCache[levelPath]) {
return levelCache[levelPath];
}

const introspect = introspectLevel(levelPath, bemConfig);

levelCache[levelPath] = introspect;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets invert if.
Let save in cache with cache=true, and read always if exists


return introspect;
}));

return new Introspection(levelPaths, levelIntrospections);
};
59 changes: 59 additions & 0 deletions packages/gulp-bem-src/lib/introspect-levels/introspection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

/**
* Contains info about files in levels for bundle.
*/
module.exports = class Introspection {
constructor(levelPaths, introspections) {
this._levelPaths = levelPaths;
this._introspections = introspections;
}
/**
* Returns all level paths.
*
* @returns {String[]}
*/
levels() {
return this._levelPaths;
}
/**
* Returns all files.
*
* @returns {Iterator}
*/
*files() {
for (const introspection of this._introspections) {
for (const files of introspection.values()) {
yield* files;
}
}
}
/**
* Returns info about files of specified entity.
*
* @param {Object} entity
* @returns {Iterator}
*/
*entityFiles(entity) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filesByEntity

for (const introspection of this._introspections) {
yield* introspection.get(entity.id);
}
}
/**
* Returns info about files with specified tech.
*
* @param {string} tech
* @returns {Iterator}
*/
*techFiles(tech) {
for (const introspection of this._introspections) {
for (const files of introspection.values()) {
for (const file of files) {
if (file.tech === tech) {
yield file;
}
}
}
}
}
};
5 changes: 3 additions & 2 deletions packages/gulp-bem-src/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
"@bem/sdk.walk": "0.2.5",
"bubble-stream-error": "1.0.0",
"gulp-read": "0.0.1",
"stream-to-array": "2.3.0",
"pify": "^3.0.0",
"vinyl": "2.1.0"
},
"devDependencies": {
"@bem/sdk.naming.entity.parse": "^0.2.4",
"@bem/sdk.naming.presets": "^0.0.7"
"@bem/sdk.naming.presets": "^0.0.7",
"stream-to-array": "2.3.0"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

крышечки долой, гринкипер в помощь

}
}
13 changes: 12 additions & 1 deletion packages/gulp-bem-src/test/harvest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path');
const parseEntity = require('@bem/sdk.naming.entity.parse')(require('@bem/sdk.naming.presets/origin'));
const { assert } = require('chai');
const lib = require('..');
const Introspection = require('../lib/introspect-levels/introspection');

describe('harvest', () => {
it('should filter introspection by entity and tech', () => {
Expand Down Expand Up @@ -96,7 +97,17 @@ describe('harvest', () => {
// {entity: {block: 'button'}, tech: 'css'}

function checkHarvest(opts) {
opts.introspection = opts.files.map(makeFileEntity);
const files = opts.files.map(makeFileEntity);
const entityMap = new Map();

for (const file of files) {
const id = file.entity.id;
const entityFiles = entityMap.has(id) ? entityMap.get(id) : entityMap.set(id, new Set()).get(id);

entityFiles.add(file);
}

opts.introspection = new Introspection(['<level-path>'], [entityMap]);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is <level-path> ?

opts.result = opts.result.map(makeFileEntity);
opts.decl = opts.decl.map(makeEntity);

Expand Down