1+ 'use strict' ;
2+
13// Dependencies
2- var loaderUtils = require ( 'loader-utils' ) ;
3- var assign = require ( 'object-assign' ) ;
44var path = require ( 'path' ) ;
5+ var assign = require ( 'object-assign' ) ;
56var formatter = require ( 'stylelint/dist/formatters/stringFormatter' ) . default ;
6- var chalk = require ( 'chalk ' ) ;
7+ var arrify = require ( 'arrify ' ) ;
78
89// Modules
9- var linter = require ( './lib/linter ' ) ;
10+ var runCompilation = require ( './lib/run-compilation ' ) ;
1011
1112function apply ( options , compiler ) {
1213 var context = options . context || compiler . context ;
13- var errors = [ ] ;
1414
1515 options = Object . assign ( { } , options , {
1616 // TODO: make it work with arrays
17- files : options . files . map ( function ( file ) {
17+ files : options . files . map ( function ( file ) {
1818 return path . join ( context , '/' , file ) ;
1919 } )
2020 } ) ;
2121
22- function runCompilation ( compilation , done ) {
23- linter ( options ) . then ( function ( lint ) {
24- if ( lint . errored ) {
25- errors = lint . results . filter ( function ( f ) {
26- return f . errored ;
27- } ) . map ( function ( f ) {
28- return f . source ; // send error instead
29- } ) ;
30-
31- ( options . quiet !== true ) && console . log ( chalk . yellow ( options . formatter ( lint . results ) ) ) ;
32- }
33-
34- if ( options . failOnError && errors . length ) {
35- done ( new Error ( 'Failed because of a stylelint error.\n' ) ) ;
36- } else {
37- done ( ) ;
38- }
39- } ) . catch ( function ( e ) {
40- if ( options . failOnError && errors . length ) {
41- done ( new Error ( 'Failed because of a stylelint error.\n' ) ) ;
42- } else {
43- done ( ) ;
44- }
45- console . log ( chalk . red ( e ) ) ;
46- } ) ;
47-
48- compilation . plugin && compilation . plugin ( 'compilation' , function ( compilation ) {
49- errors . forEach ( function ( err ) {
50- compilation . errors . push ( err ) ;
51- } ) ;
52- } ) ;
53- }
54-
55- compiler . plugin ( 'run' , runCompilation ) ;
56- compiler . plugin ( 'watch-run' , runCompilation ) ;
22+ compiler . plugin ( 'run' , runCompilation . bind ( this , options ) ) ;
23+ compiler . plugin ( 'watch-run' , runCompilation . bind ( this , options ) ) ;
5724}
5825
5926// makes it easier to pass and check options to the plugin thank you webpack doc
6027// [https://webpack.github.io/docs/plugins.html#the-compiler-instance]
61- module . exports = function ( options ) {
62- options = options || { } ;
63- // Default Glob is any directory level of scss and/or sass file,
64- // under webpack's context and specificity changed via globbing patterns
65- options . files = options . files || '**/*.s?(c|a)ss' ;
66- ! Array . isArray ( options . files ) && ( options . files = [ options . files ] ) ;
67- options . configFile = options . configFile || '.stylelintrc' ;
68- options . formatter = options . formatter || formatter ;
69- options . quiet = options . quiet || false ;
70-
28+ module . exports = function ( options ) {
7129 return {
72- apply : apply . bind ( this , options )
30+ apply : apply . bind ( this , buildOptions ( options ) )
7331 } ;
74- } ;
32+ } ;
33+
34+ function buildOptions ( options ) {
35+ return assign ( {
36+ configFile : '.stylelintrc' ,
37+ formatter : formatter ,
38+ quiet : false
39+ } , options , {
40+ // Default Glob is any directory level of scss and/or sass file,
41+ // under webpack's context and specificity changed via globbing patterns
42+ files : arrify ( options . files || '**/*.s?(c|a)ss' )
43+ } ) ;
44+ }
0 commit comments