1+ "use strict"
12// builtin tooling
2- var path = require ( "path" )
3+ const path = require ( "path" )
34
45// external tooling
5- var assign = require ( "object-assign" )
6- var postcss = require ( "postcss" )
6+ const postcss = require ( "postcss" )
77
88// internal tooling
9- var joinMedia = require ( "./lib/join-media" )
10- var resolveId = require ( "./lib/resolve-id" )
11- var loadContent = require ( "./lib/load-content" )
12- var processContent = require ( "./lib/process-content" )
13- var parseStatements = require ( "./lib/parse-statements" )
9+ const joinMedia = require ( "./lib/join-media" )
10+ const resolveId = require ( "./lib/resolve-id" )
11+ const loadContent = require ( "./lib/load-content" )
12+ const processContent = require ( "./lib/process-content" )
13+ const parseStatements = require ( "./lib/parse-statements" )
1414
1515function AtImport ( options ) {
16- options = assign (
16+ options = Object . assign (
1717 {
1818 root : process . cwd ( ) ,
1919 path : [ ] ,
@@ -33,12 +33,10 @@ function AtImport(options) {
3333
3434 if ( ! Array . isArray ( options . path ) ) options . path = [ ]
3535
36- options . path = options . path . map ( function ( p ) {
37- return path . resolve ( options . root , p )
38- } )
36+ options . path = options . path . map ( p => path . resolve ( options . root , p ) )
3937
4038 return function ( styles , result ) {
41- var state = {
39+ const state = {
4240 importedFiles : { } ,
4341 hashFiles : { } ,
4442 }
@@ -51,9 +49,7 @@ function AtImport(options) {
5149 throw new Error ( "plugins option must be an array" )
5250 }
5351
54- return parseStyles ( result , styles , options , state , [ ] ) . then ( function (
55- bundle
56- ) {
52+ return parseStyles ( result , styles , options , state , [ ] ) . then ( bundle => {
5753 applyRaws ( bundle )
5854 applyMedia ( bundle )
5955 applyStyles ( bundle , styles )
@@ -62,11 +58,11 @@ function AtImport(options) {
6258}
6359
6460function applyRaws ( bundle ) {
65- bundle . forEach ( function ( stmt , index ) {
61+ bundle . forEach ( ( stmt , index ) => {
6662 if ( index === 0 ) return
6763
6864 if ( stmt . parent ) {
69- var before = stmt . parent . node . raws . before
65+ const before = stmt . parent . node . raws . before
7066 if ( stmt . type === "nodes" ) stmt . nodes [ 0 ] . raws . before = before
7167 else stmt . node . raws . before = before
7268 } else if ( stmt . type === "nodes" ) {
@@ -76,15 +72,15 @@ function applyRaws(bundle) {
7672}
7773
7874function applyMedia ( bundle ) {
79- bundle . forEach ( function ( stmt ) {
75+ bundle . forEach ( stmt => {
8076 if ( ! stmt . media . length ) return
8177 if ( stmt . type === "import" ) {
82- stmt . node . params = stmt . fullUri + " " + stmt . media . join ( ", " )
78+ stmt . node . params = ` ${ stmt . fullUri } ${ stmt . media . join ( ", " ) } `
8379 } else if ( stmt . type === "media" ) stmt . node . params = stmt . media . join ( ", " )
8480 else {
85- var nodes = stmt . nodes
86- var parent = nodes [ 0 ] . parent
87- var mediaNode = postcss . atRule ( {
81+ const nodes = stmt . nodes
82+ const parent = nodes [ 0 ] . parent
83+ const mediaNode = postcss . atRule ( {
8884 name : "media" ,
8985 params : stmt . media . join ( ", " ) ,
9086 source : parent . source ,
@@ -93,7 +89,7 @@ function applyMedia(bundle) {
9389 parent . insertBefore ( nodes [ 0 ] , mediaNode )
9490
9591 // remove nodes
96- nodes . forEach ( function ( node ) {
92+ nodes . forEach ( node => {
9793 node . parent = undefined
9894 } )
9995
@@ -113,15 +109,15 @@ function applyMedia(bundle) {
113109function applyStyles ( bundle , styles ) {
114110 styles . nodes = [ ]
115111
116- bundle . forEach ( function ( stmt ) {
112+ bundle . forEach ( stmt => {
117113 if ( stmt . type === "import" ) {
118114 stmt . node . parent = undefined
119115 styles . append ( stmt . node )
120116 } else if ( stmt . type === "media" ) {
121117 stmt . node . parent = undefined
122118 styles . append ( stmt . node )
123119 } else if ( stmt . type === "nodes" ) {
124- stmt . nodes . forEach ( function ( node ) {
120+ stmt . nodes . forEach ( node => {
125121 node . parent = undefined
126122 styles . append ( node )
127123 } )
@@ -130,13 +126,13 @@ function applyStyles(bundle, styles) {
130126}
131127
132128function parseStyles ( result , styles , options , state , media ) {
133- var statements = parseStatements ( result , styles )
129+ const statements = parseStatements ( result , styles )
134130
135131 return Promise . resolve ( statements )
136- . then ( function ( stmts ) {
132+ . then ( stmts => {
137133 // process each statement in series
138- return stmts . reduce ( function ( promise , stmt ) {
139- return promise . then ( function ( ) {
134+ return stmts . reduce ( ( promise , stmt ) => {
135+ return promise . then ( ( ) => {
140136 stmt . media = joinMedia ( media , stmt . media || [ ] )
141137
142138 // skip protocol base uri (protocol://url) or protocol-relative
@@ -148,15 +144,15 @@ function parseStyles(result, styles, options, state, media) {
148144 } )
149145 } , Promise . resolve ( ) )
150146 } )
151- . then ( function ( ) {
152- var imports = [ ]
153- var bundle = [ ]
147+ . then ( ( ) => {
148+ const imports = [ ]
149+ const bundle = [ ]
154150
155151 // squash statements and their children
156- statements . forEach ( function ( stmt ) {
152+ statements . forEach ( stmt => {
157153 if ( stmt . type === "import" ) {
158154 if ( stmt . children ) {
159- stmt . children . forEach ( function ( child , index ) {
155+ stmt . children . forEach ( ( child , index ) => {
160156 if ( child . type === "import" ) imports . push ( child )
161157 else bundle . push ( child )
162158 // For better output
@@ -173,28 +169,28 @@ function parseStyles(result, styles, options, state, media) {
173169}
174170
175171function resolveImportId ( result , stmt , options , state ) {
176- var atRule = stmt . node
177- var sourceFile
172+ const atRule = stmt . node
173+ let sourceFile
178174 if ( atRule . source && atRule . source . input && atRule . source . input . file ) {
179175 sourceFile = atRule . source . input . file
180176 }
181- var base = sourceFile ? path . dirname ( atRule . source . input . file ) : options . root
177+ const base = sourceFile
178+ ? path . dirname ( atRule . source . input . file )
179+ : options . root
182180
183181 return Promise . resolve ( options . resolve ( stmt . uri , base , options ) )
184- . then ( function ( paths ) {
182+ . then ( paths => {
185183 if ( ! Array . isArray ( paths ) ) paths = [ paths ]
186-
184+ // Ensure that each path is absolute:
187185 return Promise . all (
188- paths . map ( function ( file ) {
189- // Ensure that each path is absolute:
190- if ( ! path . isAbsolute ( file ) ) return resolveId ( file , base , options )
191- return file
186+ paths . map ( file => {
187+ return ! path . isAbsolute ( file ) ? resolveId ( file , base , options ) : file
192188 } )
193189 )
194190 } )
195- . then ( function ( resolved ) {
191+ . then ( resolved => {
196192 // Add dependency messages:
197- resolved . forEach ( function ( file ) {
193+ resolved . forEach ( file => {
198194 result . messages . push ( {
199195 type : "dependency" ,
200196 file : file ,
@@ -203,27 +199,26 @@ function resolveImportId(result, stmt, options, state) {
203199 } )
204200
205201 return Promise . all (
206- resolved . map ( function ( file ) {
202+ resolved . map ( file => {
207203 return loadImportContent ( result , stmt , file , options , state )
208204 } )
209205 )
210206 } )
211- . then ( function ( result ) {
207+ . then ( result => {
212208 // Merge loaded statements
213- stmt . children = result . reduce ( function ( result , statements ) {
214- if ( statements ) result = result . concat ( statements )
215- return result
209+ stmt . children = result . reduce ( ( result , statements ) => {
210+ return statements ? result . concat ( statements ) : result
216211 } , [ ] )
217212 } )
218- . catch ( function ( err ) {
213+ . catch ( err => {
219214 if ( err . message . indexOf ( "Failed to find" ) !== - 1 ) throw err
220215 result . warn ( err . message , { node : atRule } )
221216 } )
222217}
223218
224219function loadImportContent ( result , stmt , filename , options , state ) {
225- var atRule = stmt . node
226- var media = stmt . media
220+ const atRule = stmt . node
221+ const media = stmt . media
227222 if ( options . skipDuplicates ) {
228223 // skip files already imported at the same scope
229224 if ( state . importedFiles [ filename ] && state . importedFiles [ filename ] [ media ] ) {
@@ -235,25 +230,26 @@ function loadImportContent(result, stmt, filename, options, state) {
235230 state . importedFiles [ filename ] [ media ] = true
236231 }
237232
238- return Promise . resolve ( options . load ( filename , options ) ) . then ( function (
239- content
240- ) {
233+ return Promise . resolve ( options . load ( filename , options ) ) . then ( content => {
241234 if ( content . trim ( ) === "" ) {
242- result . warn ( filename + " is empty" , { node : atRule } )
235+ result . warn ( ` ${ filename } is empty` , { node : atRule } )
243236 return
244237 }
245238
246239 // skip previous imported files not containing @import rules
247240 if ( state . hashFiles [ content ] && state . hashFiles [ content ] [ media ] ) return
248241
249- return processContent ( result , content , filename , options ) . then ( function (
250- importedResult
251- ) {
252- var styles = importedResult . root
242+ return processContent (
243+ result ,
244+ content ,
245+ filename ,
246+ options
247+ ) . then ( importedResult => {
248+ const styles = importedResult . root
253249 result . messages = result . messages . concat ( importedResult . messages )
254250
255251 if ( options . skipDuplicates ) {
256- var hasImport = styles . some ( function ( child ) {
252+ const hasImport = styles . some ( child => {
257253 return child . type === "atrule" && child . name === "import"
258254 } )
259255 if ( ! hasImport ) {
0 commit comments