Skip to content

Commit

Permalink
allow to load 'rtl-css'-chunks instead of original
Browse files Browse the repository at this point in the history
  • Loading branch information
c4off committed Jun 15, 2018
1 parent 3d9287b commit 96ec97f
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,24 @@ import cssnano from 'cssnano'
const WebpackRTLPlugin = function(options = {filename: false, options: {}, plugins: []}) {
this.options = options
this.chunkHashs = {};
this.pliginName = 'webpack-rtl-plugin';
}

WebpackRTLPlugin.prototype.apply = function(compiler) {
compiler.plugin('emit', (compilation, callback) => {
if (this.options.options.updateRuntimeChunk) {
const rtlFlag = this.options.rtlFlag || 'IS_RTL';
compiler.hooks.thisCompilation.tap(this.pliginName, compilation => {
compilation.mainTemplate.hooks.requireEnsure.tap(this.pliginName, (source, chunk, hash) => {
// already updated
if (source.indexOf('.rtl.css') !== -1){
return source;
}
return source.replace(/(var href.*)("\.css";)/i, '$1 (' + rtlFlag + ' ? ".rtl.css" : ".css");');
});
});
}

compiler.hooks.emit.tap(this.pliginName, (compilation) => {
const changedChunks = compilation.chunks.filter((chunk) => {
const name = chunk.name || chunk.id;
const prevHash = this.chunkHashs[name];
Expand Down Expand Up @@ -79,7 +93,7 @@ WebpackRTLPlugin.prototype.apply = function(compiler) {
chunk.files.push.apply(chunk.files, rtlFiles)
cb()
})
}, callback)
})
})
}

Expand Down

0 comments on commit 96ec97f

Please sign in to comment.