-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
109 lines (93 loc) · 3.36 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
const { forEachOfLimit } = require('async');
const rtlcss = require('rtlcss');
const cssDiff = require('@romainberger/css-diff');
const cssnano = require('cssnano');
const { ConcatSource } = require('webpack-sources');
const path = require('path');
class RTLCSSPlugin {
constructor(options) {
this.options = {
filename: false,
options: {},
plugins: [],
...options
}
}
apply(compiler) {
compiler.hooks.emit.tapAsync('RTLCSSPlugin', (compilation, callback) => {
forEachOfLimit(compilation.chunks, 5, (chunk, key, cb) => {
const rtlFiles = [];
let cssnanoPromise = Promise.resolve();
chunk.files.forEach(asset => {
const match = this.options.test ? new RegExp(this.options.test).test(asset) : true;
if (path.extname(asset) !== '.css') {
return;
}
const baseSource = compilation.assets[asset].source();
let filename = '';
let rtlSource = '';
if (match) {
rtlSource = rtlcss.process(baseSource, this.options.options, this.options.plugins);
if (this.options.filename instanceof Array && this.options.filename.length === 2) {
filename = asset.replace(this.options.filename[0], this.options.filename[1]);
} else if (this.options.filename) {
filename = this.options.filename;
if (/\[contenthash]/.test(this.options.filename)) {
const hash = createHash('md5').update(rtlSource).digest('hex').substr(0, 10);
filename = filename.replace('[contenthash]', hash);
}
if (/\[id]/.test(this.options.filename)) {
filename = filename.replace('[id]', chunk.id);
}
if (/\[name]/.test(this.options.filename)) {
filename = path.dirname(asset) + '/' + filename.replace('[name]', path.basename(asset, '.css'));
}
if (/\[file]/.test(this.options.filename)) {
filename = filename.replace('[file]', asset);
}
if (/\[filebase]/.test(this.options.filename)) {
filename = filename.replace('[filebase]', path.basename(asset));
}
if (/\[ext]/.test(this.options.filename)) {
filename = filename.replace('.[ext]', path.extname(asset));
}
} else {
const newFilename = `${path.basename(asset, '.css')}.rtl`;
filename = asset.replace(path.basename(asset, '.css'), newFilename);
}
if (this.options.diffOnly) {
rtlSource = cssDiff(baseSource, rtlSource)
}
}
if (this.options.minify !== false) {
let nanoOptions = { from: undefined }
if (typeof this.options.minify === 'object') {
nanoOptions = this.options.minify;
}
cssnanoPromise = cssnanoPromise.then(() => {
let minify = cssnano.process( baseSource, nanoOptions).then(output => {
compilation.assets[asset] = new ConcatSource(output.css);
});
if (match) {
const rtlMinify = cssnano.process(rtlSource, nanoOptions).then(output => {
compilation.assets[filename] = new ConcatSource(output.css);
rtlFiles.push(filename);
});
minify = Promise.all([minify, rtlMinify]);
}
return minify;
});
} else if (match) {
compilation.assets[filename] = new ConcatSource(rtlSource);
rtlFiles.push(filename);
}
});
cssnanoPromise.then(() => {
chunk.files.push.apply(chunk.files, rtlFiles)
cb()
});
}, callback);
});
}
}
module.exports = RTLCSSPlugin;