-
Notifications
You must be signed in to change notification settings - Fork 3
/
rtl-css.js
66 lines (57 loc) · 2.46 KB
/
rtl-css.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
const path = require('path')
const rtlcss = require('rtlcss')
const webpack = require('webpack')
const checkIsCss = filename => path.extname(filename) === '.css'
class RtlCssPlugin {
constructor(options) {
this.options = {
filename: '[name].rtl.css',
options: {},
plugins: [],
hooks: {},
...options
}
}
processAssets = (compilation, callback) => {
compilation.chunks.forEach(chunk => {
Array.from(chunk.files)
.filter(checkIsCss)
.forEach(filename => {
// Get the asset source for each file generated by the chunk:
const assetSource = compilation.assets[filename].source()
const processedAssetSource = rtlcss.process(assetSource, this.options.options, this.options.plugins, this.options.hooks)
let interpolatedFilePath
if (Array.isArray(this.options.filename)) {
if (this.options.filename.length !== 2) {
throw Error('You should pass two elements and array')
}
// also allow save current file destination
interpolatedFilePath = filename.replace(this.options.filename[0], this.options.filename[1])
} else {
interpolatedFilePath = compilation.getPath(this.options.filename, {
chunk,
cssFileName: filename,
})
}
compilation.assets[interpolatedFilePath] = new webpack.sources.RawSource(processedAssetSource)
if (this.options.addToChunkFiles) {
/* for example: LoadablePlugin will add this chunks to loadable config, but it's not always required */
chunk.files.add(interpolatedFilePath)
}
})
})
callback()
}
apply(compiler) {
compiler.hooks.compilation.tap('RtlCssPlugin', compilation => {
compilation.hooks.processAssets.tapAsync(
{
name: 'RtlCssPlugin-processing',
stage: compilation.PROCESS_ASSETS_STAGE_OPTIMIZE,
},
(chunks, callback) => this.processAssets(compilation, callback),
)
})
}
}
module.exports = RtlCssPlugin