-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CSSTransformer.js
219 lines (192 loc) Β· 6.51 KB
/
CSSTransformer.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
// @flow
import type {FilePath} from '@parcel/types';
import type {Container, Node} from 'postcss';
import SourceMap from '@parcel/source-map';
import {Transformer} from '@parcel/plugin';
import {createDependencyLocation, isURL} from '@parcel/utils';
import postcss from 'postcss';
import valueParser from 'postcss-value-parser';
import semver from 'semver';
const URL_RE = /url\s*\("?(?![a-z]+:)/;
const IMPORT_RE = /@import/;
function canHaveDependencies(filePath: FilePath, code: string) {
return !/\.css$/.test(filePath) || IMPORT_RE.test(code) || URL_RE.test(code);
}
export default new Transformer({
canReuseAST({ast}) {
return ast.type === 'postcss' && semver.satisfies(ast.version, '^7.0.0');
},
async parse({asset}) {
// This is set by other transformers (e.g. Stylus) to indicate that it has already processed
// all dependencies, and that the CSS transformer can skip this asset completely. This is
// required because when stylus processes e.g. url() it replaces them with a dependency id
// to be filled in later. When the CSS transformer runs, it would pick that up and try to
// resolve a dependency for the id which obviously doesn't exist. Also, it's faster to do
// it this way since the resulting CSS doesn't need to be re-parsed.
if (asset.meta.hasDependencies === false) {
return null;
}
let code = await asset.getCode();
if (code != null && !canHaveDependencies(asset.filePath, code)) {
return null;
}
return {
type: 'postcss',
version: '7.0.0',
program: postcss.parse(code, {
from: asset.filePath,
}),
};
},
async transform({asset}) {
// Normalize the asset's environment so that properties that only affect JS don't cause CSS to be duplicated.
// For example, with ESModule and CommonJS targets, only a single shared CSS bundle should be produced.
asset.setEnvironment({
context: 'browser',
engines: {
browsers: asset.env.engines.browsers,
},
minify: asset.env.minify,
});
// When this asset is an bundle entry, allow that bundle to be split to load shared assets separately.
// Only set here if it is null to allow previous transformers to override this behavior.
if (asset.isSplittable == null) {
asset.isSplittable = true;
}
// Check for `hasDependencies` being false here as well, as it's possible
// another transformer (such as PostCSSTransformer) has already parsed an
// ast and CSSTransformer's parse was never called.
let ast = await asset.getAST();
if (!ast || asset.meta.hasDependencies === false) {
return [asset];
}
let isDirty = false;
ast.program.walkAtRules('import', rule => {
let params = valueParser(rule.params);
let [name, ...media] = params.nodes;
let moduleSpecifier;
if (
name.type === 'function' &&
name.value === 'url' &&
name.nodes.length
) {
name = name.nodes[0];
}
moduleSpecifier = name.value;
if (!moduleSpecifier) {
throw new Error('Could not find import name for ' + rule);
}
if (isURL(moduleSpecifier)) {
name.value = asset.addURLDependency(moduleSpecifier, {
loc: createDependencyLocation(
rule.source.start,
moduleSpecifier,
0,
8,
),
});
} else {
// If this came from an inline <style> tag, don't inline the imported file. Replace with the correct URL instead.
// TODO: run CSSPackager on inline style tags.
// let inlineHTML =
// this.options.rendition && this.options.rendition.inlineHTML;
// if (inlineHTML) {
// name.value = asset.addURLDependency(dep, {loc: rule.source.start});
// rule.params = params.toString();
// } else {
media = valueParser.stringify(media).trim();
let dep = {
moduleSpecifier,
// Offset by 8 as it does not include `@import `
loc: createDependencyLocation(
rule.source.start,
moduleSpecifier,
0,
8,
),
meta: {
media,
},
};
asset.addDependency(dep);
rule.remove();
// }
}
isDirty = true;
});
ast.program.walkDecls(decl => {
if (URL_RE.test(decl.value)) {
let parsed = valueParser(decl.value);
let isDeclDirty = false;
parsed.walk(node => {
if (
node.type === 'function' &&
node.value === 'url' &&
node.nodes.length > 0 &&
!node.nodes[0].value.startsWith('#') // IE's `behavior: url(#default#VML)`
) {
let url = asset.addURLDependency(node.nodes[0].value, {
loc: createDependencyLocation(
decl.source.start,
node.nodes[0].value,
),
});
isDeclDirty = node.nodes[0].value !== url;
node.nodes[0].value = url;
}
});
if (isDeclDirty) {
decl.value = parsed.toString();
isDirty = true;
}
}
});
if (isDirty) {
asset.setAST(ast);
}
return [asset];
},
async generate({asset, ast}) {
let root = ast.program;
// $FlowFixMe
if (Object.getPrototypeOf(ast.program) === Object.prototype) {
root = postcss.root(ast.program);
let convert = (parent: Container, node: Node, index: number) => {
let type = node.type === 'atrule' ? 'atRule' : node.type;
let result = postcss[type](node);
result.parent = parent;
if (parent) {
parent.nodes[index] = result;
}
if (result.walk) {
// $FlowFixMe
const container = (result: Container);
container.each((node, index) => {
convert(container, node, index);
});
}
};
root.each((node, index) => convert(root, node, index));
}
let result = await postcss().process(root, {
from: asset.filePath,
map: {
annotation: false,
inline: false,
},
// Pass postcss's own stringifier to it to silence its warning
// as we don't want to perform any transformations -- only generate
stringifier: postcss.stringify,
});
let map;
if (result.map != null) {
map = new SourceMap();
let {mappings, sources, names} = result.map.toJSON();
map.addRawMappings(mappings, sources, names);
}
return {
content: result.css,
map,
};
},
});