-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (59 loc) · 2.39 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
/*
* Copyright © ProgramCMS. All rights reserved.
* See COPYING.txt for license details.
*
* Developed by Mohamed EL QUCHIRI <[email protected]>
*/
const { sources } = require('webpack');
const postcss = require('postcss');
const rtlcss = require('rtlcss');
const path = require('path');
const autoprefixer = require('autoprefixer');
class RtlCssPlugin {
constructor(options) {
// Options for the plugin
this.options = options || {};
}
apply(compiler) {
compiler.hooks.thisCompilation.tap('RtlCssPlugin', (compilation) => {
compilation.hooks.processAssets.tapAsync({
name: 'RtlCssPlugin',
stage: compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE, // Here we access compilation
}, (assets, callback) => {
// Loop through emitted assets
for (const filename in assets) {
// Check if the file matches the pattern
if (this.isCSSEntry(filename)) {
const asset = assets[filename];
const source = asset.source();
// Transform CSS to RTL
let result = postcss()
.use(rtlcss([
this.options.config || {} ,
this.options.plugins || {},
this.options.hooks || {}
]))
.use(autoprefixer())
.process(source);
const rtlCSS = result.css;
// Generate RTL filename
const rtlFilename = this.options.filename || this.getRTLFileName(filename);
// Add the generated RTL CSS file to the assets
assets[rtlFilename] = new sources.RawSource(rtlCSS);
}
}
callback();
});
});
}
isCSSEntry(filename) {
// Add your file matching logic here
return /\.css$/.test(filename);
}
getRTLFileName(filename) {
// Generate RTL filename
const parsedPath = path.parse(filename);
return path.join(parsedPath.dir, parsedPath.name + '-rtl' + parsedPath.ext);
}
}
module.exports = RtlCssPlugin;