forked from rsuite/rsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRTLCSSPlugin.js
81 lines (75 loc) · 2.36 KB
/
RTLCSSPlugin.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
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path');
const rtlcss = require('rtlcss');
const postcss = require('postcss');
const { NODE_ENV, STYLE_DEBUG } = process.env;
class RTLCSSPlugin {
constructor(options) {
this.options = options || {};
}
apply(compiler) {
const taskQueue = [];
compiler.hooks.emit.tapAsync('RTLCSSPlugin', (compilation, callback) => {
const postCssProcess = (content, assetName) =>
postcss(
STYLE_DEBUG === 'STYLE' || NODE_ENV === 'production'
? [
require('autoprefixer'),
require('cssnano')({
preset: [
'default',
{
discardComnments: {
removeAll: false
}
}
]
})
]
: []
)
.process(content)
.then(result => {
compilation.assets[assetName] = {
source: function() {
return result.css;
},
size: function() {
return result.css.length;
}
};
})
.catch(error => Promise.reject(error));
try {
// Explore each chunk (build output):
compilation.chunks.forEach(chunk => {
// Explore each asset filename generated by the chunk:
chunk.files.forEach(filename => {
// Get the asset source for each file generated by the chunk:
const directory = path.dirname(filename),
cssPath = this.options.path;
if ((!cssPath || cssPath === directory) && path.extname(filename) === '.css') {
const cssContent = compilation.assets[filename].source();
taskQueue.push(postCssProcess(cssContent, filename));
taskQueue.push(
postCssProcess(
rtlcss.process(cssContent),
`${directory}/${path.basename(filename, '.css')}.rtl.css`
)
);
}
});
});
} catch (e) {
console.error(e);
}
Promise.all(taskQueue)
.catch(error => {
console.log(error);
callback();
})
.then(() => callback());
});
}
}
module.exports = RTLCSSPlugin;