-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyling.js
54 lines (46 loc) · 1.52 KB
/
styling.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
import { dirname, join } from 'path';
import { readFileSync } from 'fs';
import css from 'css';
import { encapsulate } from './encapsulate';
export function addStylingProperty(path, t, style) {
const { ast, id } = encapsulate(css.parse(style));
const text = css.stringify(ast);
path.node.key.name = 'style';
path.node.value.value = text;
const styling = `function(e) { e.attr('_ng-${id}', ''); }`;
const styleScope = t.ObjectProperty(t.Identifier('styling'),
t.Identifier(styling))
path.insertAfter(styleScope);
}
export function isStyleUrl(path) {
return 'styleUrl' == path.node.key.name;
}
export function isStylized(path) {
const name = path.node.key.name;
return ('styleUrl' == name || 'style' == name)
&& !path.parent.properties.some(p => 'styling' == p.key.name);
}
export function readStyle(path, state) {
const filename = path.node.value.value;
const style = readStyleFile(filename, state);
return style;
}
function readStyleFile(filename, state) {
const basePath = state.opts.basePath || dirname(state.file.opts.filename);
const relativeFilename = join(basePath, filename);
try {
return readFileSync(relativeFilename).toString();
} catch(error) {
if ('ENOENT' == error.code) {
console.error(`StyleURL Error: open file '${filename}'
- base path: ${basePath}
- error path: ${error.path}
- relative: ${relativeFilename}
`);
} else {
console.error('Error: Unkown inlineStyle error');
throw error;
}
process.exit();
}
}