This repository has been archived by the owner on Feb 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
130 lines (114 loc) · 3.5 KB
/
index.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
'use strict';
const sysPath = require('path');
const postcss = require('postcss');
const postcssModules = require('postcss-modules');
const progeny = require('progeny');
const logger = require('loggy');
const anymatch = require('anymatch');
const notify = (warnings) => {
if (!warnings.length) return;
const str = warnings.map(warn => {
const line = warn.line ? `line ${warn.line}` : ''
const col = warn.col ? ` col ${warn.col}` : ''
const node = warn.node ? ' ' + warn.node.toString() : '';
return `\t[${warn.plugin}]:${node}\t${line}${col}: ${warn.text}\n`;
}).join('\n');
logger.warn(`postcss-brunch: ${str}`);
};
const cssModulify = (path, data, map, options) => {
let json = {};
const getJSON = (...args) => {
json = typeof options.getJSON === 'function' && options.getJSON(...args) || args[1]
};
return postcss([postcssModules(Object.assign({getJSON}, options))])
.process(data, {from: path, map}).then(x => {
const exports = 'module.exports = ' + JSON.stringify(json) + ';';
return { data: x.css, map: x.map, exports };
});
};
class PostCSSCompiler {
constructor(config) {
const rootPath = config.paths.root;
this.config = config.plugins.postcss || {};
this.pattern = this.config.pattern || /\.p?css$/i;
this.map = Object.assign({
inline: false,
annotation: false,
sourcesContent: false,
}, this.config.map);
const progenyOpts = Object.assign({rootPath, reverseArgs: true}, this.config.progeny);
this.getDependencies = progeny(progenyOpts);
this.isIgnored = anymatch(this.config.ignore || []);
const procs = this.config.processors || [];
const compilers = [].concat(procs.compilers || procs);
this._compiler = postcss(compilers);
const optimizers = [].concat(procs.optimizers || []);
if (!optimizers.length) return;
this._optimizer = postcss(optimizers);
this.optimize = file => {
const path = file.path;
const opts = Object.assign({
from: path,
to: sysPath.basename(path),
map: this.map,
}, this.config.options);
if (file.data === undefined) {
file.data = '';
}
if (file.map) {
opts.map.prev = JSON.stringify(file.map);
}
return this._optimizer.process(file.data, opts).then(result => {
notify(result.warnings());
return {
path,
data: result.css,
map: JSON.stringify(result.map),
};
}).catch(error => {
if (error.name === 'CssSyntaxError') {
throw new Error('postcss-brunch syntax error: ' + error.message + error.showSourceCode());
}
throw error;
});
};
}
compile(file) {
const path = file.path;
if (this.isIgnored(path)) {
return Promise.resolve(file);
}
const opts = Object.assign({
from: path,
to: sysPath.basename(path),
map: this.map,
}, this.config.options);
if (file.data === undefined) {
file.data = '';
}
if (file.map) {
opts.map.prev = JSON.stringify(file.map);
}
return this._compiler.process(file.data, opts).then(result => {
notify(result.warnings());
const data = result.css;
const map = JSON.stringify(result.map);
if (this.config.modules) {
const opts = this.config.modules === true ? {} : this.config.modules;
return cssModulify(path, data, map, opts);
} else {
return {path, data, map};
}
}).catch(error => {
if (error.name === 'CssSyntaxError') {
throw new Error('postcss-brunch syntax error: ' + error.message + error.showSourceCode());
}
throw error;
});
}
}
Object.assign(PostCSSCompiler.prototype, {
brunchPlugin: true,
type: 'stylesheet',
});
module.exports = PostCSSCompiler;