-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
154 lines (138 loc) · 6.29 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict';
const path = require('path'),
bn = require('@bem/naming'),
BemCell = require('@bem/cell'),
BemEntityName = require('@bem/entity-name'),
bemFs = require('@bem/fs-scheme'),
bemImport = require('@bem/import-notation'),
bemConfig = require('bem-config')(),
requiredPath = require('required-path'),
falafel = require('falafel'),
vow = require('vow'),
vowFs = require('vow-fs'),
loaderUtils = require('loader-utils'),
generators = require('./generators');
module.exports = function(source) {
this.cacheable && this.cacheable();
const callback = this.async(),
options = Object.assign({}, this.options.bemLoader, loaderUtils.getOptions(this)),
levelsMap = options.levels || bemConfig.levelMapSync(),
levels = Array.isArray(levelsMap) ? levelsMap : Object.keys(levelsMap),
techs = options.techs || ['js'],
langs = options.langs || ['en'],
techMap = techs.reduce((acc, tech) => {
acc[tech] || (acc[tech] = [tech]);
return acc;
}, options.techMap || {}),
extToTech = Object.keys(techMap).reduce((acc, tech) => {
techMap[tech].forEach(ext => {
acc[ext] = tech;
});
return acc;
}, {}),
defaultExts = Object.keys(extToTech),
allPromises = [],
namingOptions = options.naming || 'react',
bemNaming = bn(namingOptions);
generators.i18n = require('./generators/i18n').generate(langs);
const result = falafel(source, node => {
// match `require('b:button')`
if(!(
node.type === 'CallExpression' &&
node.callee.type === 'Identifier' &&
node.callee.name === 'require' &&
Object(node.arguments[0]).value
)) return;
const existingEntitiesPromises = bemImport.parse(
node.arguments[0].value,
// FIXME: we really need this context for parsing import?
// https://github.com/bem-sdk/bem-fs-scheme/issues/18
bemNaming.parse(path.basename(this.resourcePath).split('.')[0])
)
// expand entities by all provided levels
.reduce((acc, entity) => {
levels.forEach(layer => {
// if entity has tech get extensions for it or exactly it,
// otherwise expand entities by default extensions
(entity.tech? techMap[entity.tech] || [entity.tech] : defaultExts).forEach(tech => {
acc.push(BemCell.create({ entity, tech, layer }));
});
});
return acc;
}, [])
// find path for every entity and check it existance
.map(bemCell => {
const localNamingOpts = (levelsMap[bemCell.layer] && levelsMap[bemCell.layer].naming) || namingOptions;
const fsScheme = (levelsMap[bemCell.layer] && levelsMap[bemCell.layer].scheme) || 'nested';
const entityPath = path.resolve(bemFs(fsScheme).path(bemCell, localNamingOpts));
this.addDependency(entityPath);
return vowFs
.exists(entityPath)
.then(exist => {
// BemFile
return {
cell : bemCell,
exist,
// prepare path for require cause relative returns us string that we couldn't require
path : requiredPath(path.relative(path.dirname(this.resourcePath), entityPath))
};
});
});
existingEntitiesPromises.length && allPromises.push(
vow
.all(existingEntitiesPromises)
.then(bemFiles => {
/**
* techToFiles:
* js: [entity, entity]
* css: [entity, entity, entity]
* i18n: [entity]
*/
const techToFiles = {},
existsEntities = {},
errEntities = {};
bemFiles.forEach(file => {
const tech = file.cell.tech,
entity = file.cell.entity,
block = entity.block,
elem = entity.elem,
modName = entity.modName,
id = entity.id;
if(!file.exist) {
// there are no realizations found neither on levels not in techs
existsEntities[id] || (existsEntities[id] = false);
(errEntities[id] || (errEntities[id] = [])).push(file);
return;
}
(techToFiles[tech] || (techToFiles[tech] = [])).push(file);
existsEntities[id] = true;
// Add existence for `_mod` if `_mod_val` exists.
entity.isSimpleMod() === false &&
(existsEntities[BemEntityName.create({ block, elem, modName }).id] = true);
// Add existance for elem if __elem_mod exists.
entity.elem &&
(existsEntities[BemEntityName.create({ block, elem }).id] = true);
});
Object.keys(existsEntities).forEach(fileId => {
// check if entity has no tech to resolve
existsEntities[fileId] || errEntities[fileId].forEach(file => {
this.emitError(`BEM module not found: ${file.path}`);
});
});
node.update(`(${
// Each tech has own generator
Object.keys(techToFiles)
// js tech is always last
.sort(a => extToTech[a] === 'js')
.map(tech =>
(generators[extToTech[tech] || tech] || generators['*'])(techToFiles[tech])
)
.join(',\n')
})`);
})
);
});
vow.all(allPromises)
.then(() => callback(null, result.toString()))
.catch(callback);
};