-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathpostcss-runner.js
73 lines (72 loc) · 3.15 KB
/
postcss-runner.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
const postcss = require("postcss");
const postcssrc = require("postcss-load-config");
const cssModules = require("postcss-modules");
// This script is essentially a PostCSS Runner
// https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md#postcss-runner-guidelines
module.exports = ({ src, filename, transformConfig }) => {
const ctx = {
// Not sure whether the map is useful or not.
// Disabled for now. We can always enable it once it becomes clear.
map: false,
// To ensure that PostCSS generates source maps and displays better syntax
// errors, runners must specify the from and to options. If your runner does
// not handle writing to disk (for example, a gulp transform), you should
// set both options to point to the same file"
// https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md#21-set-from-and-to-processing-options
from: filename,
to: filename
};
let tokens = {};
return postcssrc(ctx)
.then(
config => ({ ...config, plugins: config.plugins || [] }),
error => {
// Support running without postcss.config.js
// This is useful in case the webpack setup of the consumer does not
// use PostCSS at all and simply uses css-loader in modules mode.
if (error.message.startsWith("No PostCSS Config found in:")) {
return { plugins: [], options: { from: filename, to: filename } };
}
throw error;
}
)
.then(({ plugins, options }) => {
return postcss([
cssModules({
// Add 'generateScopedName' property to 'jesttransformcss.config.js' for custom name generation.
// List of available placeholder tokens: https://github.com/webpack/loader-utils#interpolatename
// *Note some placeholder tokens appear to not be working
generateScopedName:
transformConfig && transformConfig.generateScopedName ||
"[path][local]-[hash:base64:10]",
getJSON: (cssFileName, exportedTokens, outputFileName) => {
tokens = exportedTokens;
}
}),
...plugins
])
.process(src, options)
.then(
result => ({
css: result.css,
tokens,
// Display result.warnings()
// PostCSS runners must output warnings from result.warnings()
// https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md#32-display-resultwarnings
warnings: result.warnings().map(warn => warn.toString())
}),
// Don’t show JS stack for CssSyntaxError
// PostCSS runners must not show a stack trace for CSS syntax errors,
// as the runner can be used by developers who are not familiar with
// JavaScript. Instead, handle such errors gracefully:
// https://github.com/postcss/postcss/blob/master/docs/guidelines/runner.md#31-dont-show-js-stack-for-csssyntaxerror
error => {
if (error.name === "CssSyntaxError") {
process.stderr.write(error.message + error.showSourceCode());
} else {
throw error;
}
}
);
});
};