-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathindex.ts
More file actions
executable file
·94 lines (81 loc) · 2.78 KB
/
index.ts
File metadata and controls
executable file
·94 lines (81 loc) · 2.78 KB
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
import postCss from 'postcss';
import { loadDiagnostic } from './diagnostics';
import * as d from './declarations';
import * as util from './util';
export function postcss(
opts: ((ctx: d.RendererCtx) => d.PluginOptions) | d.PluginOptions = {}
) {
return {
name: 'postcss',
transform(sourceText: string, fileName: string, context: d.PluginCtx) {
if (typeof opts === 'function') {
opts = opts({
env: process.env.NODE_ENV,
file: fileName
});
}
if (!opts.hasOwnProperty('plugins') || opts.plugins.length < 1) {
return null;
}
if (!context || !util.usePlugin(fileName)) {
return null;
}
const renderOpts = util.getRenderOptions(opts, sourceText, context);
const results: d.PluginTransformResults = {
id: util.createResultsId(fileName)
};
if (sourceText.trim() === '') {
results.code = '';
return Promise.resolve(results);
}
return new Promise<d.PluginTransformResults>(resolve => {
postCss(renderOpts.plugins)
.process(renderOpts.data, {
from: fileName
})
.then(postCssResults => {
const warnings = postCssResults.warnings();
if (warnings.length > 0) {
// emit diagnostics for each warning
warnings.forEach((warn: any) => {
const err: any = {
reason: warn.text,
level: warn.type,
column: warn.column || -1,
line: warn.line || -1
};
loadDiagnostic(context, err, fileName);
});
const mappedWarnings = warnings
.map((warn: any) => {
return `${warn.type} ${
warn.plugin ? `(${warn.plugin})` : ''
}: ${warn.text}`;
})
.join(', ');
results.code = `/** postcss ${mappedWarnings} **/`;
resolve(results);
} else {
results.code = postCssResults.css.toString();
// write this css content to memory only so it can be referenced
// later by other plugins (autoprefixer)
// but no need to actually write to disk
context.fs
.writeFile(results.id, results.code, { inMemoryOnly: true })
.then(() => {
resolve(results);
});
}
return results;
})
.catch((err: any) => {
loadDiagnostic(context, err, fileName);
results.code = `/** postcss error${
err && err.message ? ': ' + err.message : ''
} **/`;
resolve(results);
});
});
}
};
}