-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
55 lines (49 loc) · 1.67 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 async = require('async')
const postcss = require('postcss')
const RawSource = require('webpack-sources').RawSource
const merge = require('lodash.merge')
const Cache = require('fs-simple-cache')
class WebpackRTLWrapPlugin {
constructor (options = {}) {
this.options = merge({
testCss: /\.css?$/,
testIsRTL: /\.rtl\.css?$/
}, options)
}
apply (compiler) {
const cache = new Cache(this.options)
compiler.plugin('emit', (compilation, callback) => {
async.eachOfLimit(compilation.chunks, 5, (chunk, key, cb) => {
let promise = Promise.resolve()
chunk.files.forEach((file) => {
const asset = compilation.assets[file]
const isRTL = this.options.testIsRTL.test(file)
if (this.options.testCss.test(file)) {
const source = asset.source()
const key = source + isRTL
let content
if (content = cache.get(key).content) {
promise = promise.then(() => content)
}
else {
promise = promise.then(() => {
return postcss([
require('postcss-wrap')({ selector: '[dir=' + (isRTL ? 'rtl' : 'ltr') + ']', skip: /^html/ })
])
.process(source, {}).then(function (result) {
cache.put(key, { content: result.css })
return result.css
})
})
}
promise = promise.then(result => {
compilation.assets[file] = new RawSource(result)
})
}
})
promise.then(() => cb())
}, callback)
})
}
}
module.exports = WebpackRTLWrapPlugin