Skip to content

Commit 96ec97f

Browse files
committed
allow to load 'rtl-css'-chunks instead of original
1 parent 3d9287b commit 96ec97f

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

src/index.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,24 @@ import cssnano from 'cssnano'
99
const WebpackRTLPlugin = function(options = {filename: false, options: {}, plugins: []}) {
1010
this.options = options
1111
this.chunkHashs = {};
12+
this.pliginName = 'webpack-rtl-plugin';
1213
}
1314

1415
WebpackRTLPlugin.prototype.apply = function(compiler) {
15-
compiler.plugin('emit', (compilation, callback) => {
16+
if (this.options.options.updateRuntimeChunk) {
17+
const rtlFlag = this.options.rtlFlag || 'IS_RTL';
18+
compiler.hooks.thisCompilation.tap(this.pliginName, compilation => {
19+
compilation.mainTemplate.hooks.requireEnsure.tap(this.pliginName, (source, chunk, hash) => {
20+
// already updated
21+
if (source.indexOf('.rtl.css') !== -1){
22+
return source;
23+
}
24+
return source.replace(/(var href.*)("\.css";)/i, '$1 (' + rtlFlag + ' ? ".rtl.css" : ".css");');
25+
});
26+
});
27+
}
28+
29+
compiler.hooks.emit.tap(this.pliginName, (compilation) => {
1630
const changedChunks = compilation.chunks.filter((chunk) => {
1731
const name = chunk.name || chunk.id;
1832
const prevHash = this.chunkHashs[name];
@@ -79,7 +93,7 @@ WebpackRTLPlugin.prototype.apply = function(compiler) {
7993
chunk.files.push.apply(chunk.files, rtlFiles)
8094
cb()
8195
})
82-
}, callback)
96+
})
8397
})
8498
}
8599

0 commit comments

Comments
 (0)