-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.js
167 lines (149 loc) · 7.19 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
155
156
157
158
159
160
161
162
163
164
165
166
167
'use strict';
const SHOW_MISSING = process.env.SHOW_MISSING;
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'),
loaderUtils = require('loader-utils'),
getGenerators = require('./generators');
module.exports = function(source, inputSourceMap) {
this.cacheable && this.cacheable();
const callback = this.async(),
optionsFromCompiler = this.options && this.options.bemLoader,
options = Object.assign({}, optionsFromCompiler, 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 = [],
unifyPath = path => path.replace(/\\/g, '/'),
namingOptions = options.naming || 'react',
bemNaming = bn(namingOptions),
// https://github.com/bem-sdk/bem-fs-scheme/issues/18
currentEntityName = path.basename(this.resourcePath),
currentEntity = bemNaming.parse(currentEntityName.split('.')[0]),
currentEntityTech = extToTech[currentEntityName.substr(currentEntityName.indexOf('.') + 1)],
generators = getGenerators(options.generators);
generators.i18n = require('./generators/i18n').generate(langs);
const result = falafel(source, { ecmaVersion : 8, sourceType : 'module' }, 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,
currentEntity
)
// expand entities by all provided levels/techs
.reduce((acc, entity) => {
// 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 => {
// don't push js block in context of block itself
// so we forewarn cycled requires
// example ``` block.react.js: import 'm:autoclosable=yes' ```
if(!(
currentEntity.isEqual(BemEntityName.create(entity)) &&
currentEntityTech === 'js' &&
extToTech[tech] === 'js'
)) {
levels.forEach(layer => {
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 new Promise(resolve => this.fs.stat(entityPath, err => {
// BemFile
resolve({
cell : bemCell,
exist : !err,
// prepare path for require cause relative returns us string that we couldn't require
path : unifyPath(requiredPath(path.relative(path.dirname(this.resourcePath), entityPath)))
});
}));
});
existingEntitiesPromises.length && allPromises.push(
Promise
.all(existingEntitiesPromises)
.then(bemFiles => {
/**
* extToFiles:
* js: [entity, entity]
* css: [entity, entity, entity]
* i18n: [entity]
*/
const extToFiles = {},
existsEntities = {},
errEntities = {};
bemFiles.forEach(file => {
const ext = 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;
}
(extToFiles[ext] || (extToFiles[ext] = [])).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);
});
SHOW_MISSING && Object.keys(existsEntities).forEach(fileId => {
// check if entity has no tech to resolve
existsEntities[fileId] || errEntities[fileId].forEach(file => {
this.emitWarning(`BEM module not found: ${file.path}`);
});
});
// Each tech has own generator
const res = Object.keys(extToFiles)
// use techs from config for order
// so the first one would be default, `js` in most cases
.sort((a, b) => techs.indexOf(extToTech[a]) - techs.indexOf(extToTech[b]))
.map((ext) => {
const tech = extToTech[ext] || ext;
return `${(generators[tech] || generators['*'])(extToFiles[ext])}`;
});
node.update(`[${res.join(',')}][0]`);
})
);
});
Promise.all(allPromises)
.then(() => callback(null, result.toString(), inputSourceMap))
.catch(callback);
};