This repository was archived by the owner on Aug 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
55 lines (48 loc) · 1.52 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
const { ConcatSource } = require('webpack-sources');
const path = require('path');
const rtlcss = require('rtlcss');
const PluginName = 'WebpackRTLCSSPlugin';
/**
* Webpack Plugin to operate Right-to-Left operations over .css
*/
class WebpackRTLCSSPlugin {
/**
* @typedef {import('rtlcss').ConfigOptions} RTLCSSOptions
* @typedef {import('webpack').Compiler} WebPackCompiler
*/
/**
* Create an instance of the plugin
*
* @param {Object} [options] The `rtlcss` options.
* @param {RTLCSSOptions} [options.options] See https://rtlcss.com/learn/usage-guide/options/
* @param {Object|string[]} [options.plugins] See https://rtlcss.com/learn/extending-rtlcss/writing-a-plugin/
*/
constructor(options = {}) {
this.options = {
options: options.options,
plugins: options.plugins
};
}
/**
* Apply the plugin
*
* @param {WebPackCompiler} compiler The WebPack compiler
*/
apply(compiler) {
compiler.hooks.emit.tapAsync(PluginName, async (compilation, cb) => {
compilation.chunks.forEach((chunk) => {
chunk.files.forEach((asset) => {
// Ignore chunks that are not CSS
if (path.extname(asset) !== '.css') {
return;
}
const baseSource = compilation.assets[asset].source();
const rtlSource = rtlcss.process(baseSource, this.options.options, this.options.plugins);
compilation.assets[asset] = new ConcatSource(rtlSource);
});
});
cb();
});
}
}
module.exports = WebpackRTLCSSPlugin;