forked from postcss/postcss-import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·339 lines (302 loc) · 7.75 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
var path = require("path")
var assign = require("object-assign")
var postcss = require("postcss")
var joinMedia = require("./lib/join-media")
var resolveId = require("./lib/resolve-id")
var loadContent = require("./lib/load-content")
var parseStatements = require("./lib/parse-statements")
function AtImport(options) {
options = assign({
root: process.cwd(),
path: [],
skipDuplicates: true,
resolve: resolveId,
load: loadContent,
plugins: [],
}, options)
options.root = path.resolve(options.root)
// convert string to an array of a single element
if (typeof options.path === "string") {
options.path = [ options.path ]
}
if (!Array.isArray(options.path)) {
options.path = []
}
options.path = options.path.map(function(p) {
return path.resolve(options.root, p)
})
return function(styles, result) {
var state = {
importedFiles: {},
hashFiles: {},
}
if (styles.source && styles.source.input && styles.source.input.file) {
state.importedFiles[styles.source.input.file] = {}
}
if (options.plugins && !Array.isArray(options.plugins)) {
throw new Error("plugins option must be an array")
}
return parseStyles(
result,
styles,
options,
state,
[]
).then(function(bundle) {
applyRaws(bundle)
applyMedia(bundle)
applyStyles(bundle, styles)
if (
typeof options.addDependencyTo === "object" &&
typeof options.addDependencyTo.addDependency === "function"
) {
Object.keys(state.importedFiles)
.forEach(options.addDependencyTo.addDependency)
}
if (typeof options.onImport === "function") {
options.onImport(Object.keys(state.importedFiles))
}
})
}
}
function applyRaws(bundle) {
bundle.forEach(function(stmt, index) {
if (index === 0) {
return
}
if (stmt.parent) {
var before = stmt.parent.node.raws.before
if (stmt.type === "nodes") {
stmt.nodes[0].raws.before = before
}
else {
stmt.node.raws.before = before
}
}
else if (stmt.type === "nodes") {
stmt.nodes[0].raws.before = stmt.nodes[0].raws.before || "\n"
}
})
}
function applyMedia(bundle) {
bundle.forEach(function(stmt) {
if (!stmt.media.length) {
return
}
if (stmt.type === "import") {
stmt.node.params = stmt.fullUri + " " + stmt.media.join(", ")
}
else if (stmt.type ==="media") {
stmt.node.params = stmt.media.join(", ")
}
else {
var nodes = stmt.nodes
var parent = nodes[0].parent
var mediaNode = postcss.atRule({
name: "media",
params: stmt.media.join(", "),
source: parent.source,
})
parent.insertBefore(nodes[0], mediaNode)
// remove nodes
nodes.forEach(function(node) {
node.parent = undefined
})
// better output
nodes[0].raws.before = nodes[0].raws.before || "\n"
// wrap new rules with media query
mediaNode.append(nodes)
stmt.type = "media"
stmt.node = mediaNode
delete stmt.nodes
}
})
}
function applyStyles(bundle, styles) {
styles.nodes = []
bundle.forEach(function(stmt) {
if (stmt.type === "import") {
stmt.node.parent = undefined
styles.append(stmt.node)
}
else if (stmt.type === "media") {
stmt.node.parent = undefined
styles.append(stmt.node)
}
else if (stmt.type === "nodes") {
stmt.nodes.forEach(function(node) {
node.parent = undefined
styles.append(node)
})
}
})
}
function parseStyles(
result,
styles,
options,
state,
media
) {
var statements = parseStatements(result, styles)
return Promise.all(statements.map(function(stmt) {
stmt.media = joinMedia(media, stmt.media)
// skip protocol base uri (protocol://url) or protocol-relative
if (stmt.type !== "import" || /^(?:[a-z]+:)?\/\//i.test(stmt.uri)) {
return
}
return resolveImportId(
result,
stmt,
options,
state
)
})).then(function() {
var imports = []
var bundle = []
// squash statements and their children
statements.forEach(function(stmt) {
if (stmt.type === "import") {
if (stmt.children) {
stmt.children.forEach(function(child, index) {
if (child.type === "import") {
imports.push(child)
}
else {
bundle.push(child)
}
// For better output
if (index === 0) {
child.parent = stmt
}
})
}
else {
imports.push(stmt)
}
}
else if (stmt.type === "media" || stmt.type === "nodes") {
bundle.push(stmt)
}
})
return imports.concat(bundle)
})
}
function resolveImportId(
result,
stmt,
options,
state
) {
var atRule = stmt.node
var base = atRule.source && atRule.source.input && atRule.source.input.file
? path.dirname(atRule.source.input.file)
: options.root
return Promise.resolve(options.resolve(stmt.uri, base, options))
.then(function(resolved) {
if (!Array.isArray(resolved)) {
resolved = [ resolved ]
}
return Promise.all(resolved.map(function(file) {
return loadImportContent(
result,
stmt,
file,
options,
state
)
}))
})
.then(function(result) {
// Merge loaded statements
stmt.children = result.reduce(function(result, statements) {
if (statements) {
result = result.concat(statements)
}
return result
}, [])
})
.catch(function(err) {
result.warn(err.message, { node: atRule })
})
}
function loadImportContent(
result,
stmt,
filename,
options,
state
) {
var atRule = stmt.node
var media = stmt.media
if (options.skipDuplicates) {
// skip files already imported at the same scope
if (
state.importedFiles[filename] &&
state.importedFiles[filename][media]
) {
return
}
// save imported files to skip them next time
if (!state.importedFiles[filename]) {
state.importedFiles[filename] = {}
}
state.importedFiles[filename][media] = true
}
return Promise.resolve(options.load(filename, options))
.then(function(content) {
if (typeof options.transform !== "function") {
return content
}
return Promise.resolve(options.transform(content, filename, options))
.then(function(transformed) {
return typeof transformed === "string" ? transformed : content
})
})
.then(function(content) {
if (content.trim() === "") {
result.warn(filename + " is empty", { node: atRule })
return
}
// skip previous imported files not containing @import rules
if (
state.hashFiles[content] &&
state.hashFiles[content][media]
) {
return
}
return postcss(options.plugins).process(content, {
from: filename,
syntax: result.opts.syntax,
parser: result.opts.parser,
})
.then(function(importedResult) {
var styles = importedResult.root
result.messages = result.messages.concat(importedResult.messages)
if (options.skipDuplicates) {
var hasImport = styles.some(function(child) {
return child.type === "atrule" && child.name === "import"
})
if (!hasImport) {
// save hash files to skip them next time
if (!state.hashFiles[content]) {
state.hashFiles[content] = {}
}
state.hashFiles[content][media] = true
}
}
// recursion: import @import from imported file
return parseStyles(
result,
styles,
options,
state,
media
)
})
})
}
module.exports = postcss.plugin(
"postcss-import",
AtImport
)