-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
50 lines (35 loc) · 1.26 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
const loaderUtils = require('loader-utils');
const flatten = require('lodash.flatten');
const exec = require('webpack-loader-api-exec');
const utils = require('./libs');
module.exports = function( source ) {
if ( this.cacheable ) {
this.cacheable();
}
const options = loaderUtils.getOptions( this );
const content = ( typeof source === 'string' )
? exec( source, this.resourcePath )
: source;
const next = this.async();
const modules = utils.normalize( content );
Promise.all( modules.map( ( block ) => utils.search( this, block, options.bem ) ) )
.then( ( pathes ) => {
const result = [];
for ( let path of flatten( pathes ) ) {
if ( !path ) {
continue;
}
const req = loaderUtils.stringifyRequest( this, path );
result.push( `require(${req});` );
}
next( null, result.join('\n') );
} )
.catch( ( err ) => {
if ( typeof err === 'string' ) {
const emitMethod = options.strict ? 'emitError' : 'emitWarning';
this[emitMethod]( err );
return void next( null );
}
next( err );
} );
};