-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
113 lines (94 loc) · 2.92 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
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const { green } = require('kleur');
const postcss = require('postcss');
const SubsequentPlugins = require('./subsequent-plugins');
const plugins = new SubsequentPlugins();
module.exports = (opts) => {
opts = _.merge(
{
output: {
path: path.join(__dirname, '..'),
name: '[name]-[query].[ext]',
},
queries: {},
extractAll: true,
stats: true,
entry: null,
},
opts
);
if (opts.config) {
plugins.updateConfig(opts.config);
}
const media = {};
function addMedia(key, css, query) {
if (!Array.isArray(media[key])) {
media[key] = [];
}
media[key].push({ css, query });
}
function getMedia(key) {
const css = media[key].map((data) => data.css).join('\n');
const query = media[key][0].query;
return { css, query };
}
return {
postcssPlugin: 'postcss-extract-media-query',
Once(root, { result }) {
let from = 'undefined.css';
if (opts.entry) {
from = opts.entry;
} else if (result.opts.from) {
from = result.opts.from;
}
const file = from.match(/([^/\\]+)\.(\w+)(?:\?.+)?$/);
const name = file[1];
const ext = file[2];
if (opts.output.path) {
root.walkAtRules('media', (atRule) => {
const query = atRule.params;
const queryname =
opts.queries[query] || (opts.extractAll && _.kebabCase(query));
if (queryname) {
const css = postcss.root().append(atRule).toString();
addMedia(queryname, css, query);
atRule.remove();
}
});
}
const promises = [];
// gather promises only if output.path specified because otherwise
// nothing has been extracted
if (opts.output.path) {
Object.keys(media).forEach((queryname) => {
promises.push(
new Promise((resolve) => {
let { css } = getMedia(queryname);
const newFile = opts.output.name
.replace(/\[name\]/g, name)
.replace(/\[query\]/g, queryname)
.replace(/\[ext\]/g, ext);
const newFilePath = path.join(opts.output.path, newFile);
const newFileDir = path.dirname(newFilePath);
plugins.applyPlugins(css, newFilePath).then((css) => {
if (!fs.existsSync(path.dirname(newFilePath))) {
// make sure we can write
fs.mkdirSync(newFileDir, { recursive: true });
}
fs.writeFileSync(newFilePath, css);
if (opts.stats === true) {
console.log(green('[extracted media query]'), newFile);
}
resolve();
});
})
);
});
}
return Promise.all(promises);
},
};
};
module.exports.postcss = true;