-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtransform-import-atrule.js
94 lines (78 loc) · 2.98 KB
/
transform-import-atrule.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
// tooling
import postcss, { list } from 'postcss';
import getReplacedString from './get-replaced-string';
import path from 'path';
import transformNode from './transform-node';
import manageUnresolved from './manage-unresolved';
// transform @import at-rules
export default function transformImportAtrule(rule, opts) {
// if @import is supported
if (opts.transform.includes('@import')) {
// @import options
const { id, media, cwf, cwd } = getImportOpts(rule, opts);
// PostCSS options
const options = opts.result.opts;
const parser = options.parser || options.syntax && options.syntax.parse || null;
if (
opts.importFilter instanceof Function && opts.importFilter(id, media) ||
opts.importFilter instanceof RegExp && opts.importFilter.test(id)
) {
const cwds = [cwd].concat(opts.importPaths);
// promise the resolved file and its contents using the file resolver
const importPromise = cwds.reduce(
(promise, thiscwd) => promise.catch(
() => opts.importResolve(id, thiscwd, opts)
),
Promise.reject()
);
return importPromise.then(
// promise the processed file
({ file, contents }) => processor.process(contents, { from: file, parser: parser }).then(
({ root }) => {
// push a dependency message
opts.result.messages.push({ type: 'dependency', file: file, parent: cwf });
// imported nodes
const nodes = root.nodes.slice(0);
// if media params were detected
if (media) {
// create a new media rule
const mediaRule = postcss.atRule({
name: 'media',
params: media,
source: rule.source
});
// append with the imported nodes
mediaRule.append(nodes);
// replace the @import at-rule with the @media at-rule
rule.replaceWith(mediaRule);
} else {
// replace the @import at-rule with the imported nodes
rule.replaceWith(nodes);
}
// transform all nodes from the import
return transformNode({ nodes }, opts);
}
),
() => {
// otherwise, if the @import could not be found
manageUnresolved(rule, opts, '@import', `Could not resolve the @import for "${id}"`);
}
)
}
}
}
const processor = postcss();
// return the @import statement options (@import ID, @import ID MEDIA)
const getImportOpts = (node, opts) => {
const [ rawid, ...medias ] = list.space(node.params);
const id = getReplacedString(trimWrappingURL(rawid), node, opts);
const media = medias.join(' ');
// current working file and directory
const cwf = node.source && node.source.input && node.source.input.file || opts.result.from;
const cwd = cwf ? path.dirname(cwf) : opts.importRoot;
return { id, media, cwf, cwd };
};
// return a string with the wrapping url() and quotes trimmed
const trimWrappingURL = string => trimWrappingQuotes(string.replace(/^url\(([\W\w]*)\)$/, '$1'));
// return a string with the wrapping quotes trimmed
const trimWrappingQuotes = string => string.replace(/^("|')([\W\w]*)\1$/, '$2');