From 3d9287badb1ad57295a3c23086609436fa75ea1c Mon Sep 17 00:00:00 2001 From: Konstantin Lopyrev Date: Fri, 6 Apr 2018 18:29:22 +0300 Subject: [PATCH 1/3] Filter chunks and process only changed --- src/index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 03bcfdd..9139425 100644 --- a/src/index.js +++ b/src/index.js @@ -8,11 +8,20 @@ import cssnano from 'cssnano' const WebpackRTLPlugin = function(options = {filename: false, options: {}, plugins: []}) { this.options = options + this.chunkHashs = {}; } WebpackRTLPlugin.prototype.apply = function(compiler) { compiler.plugin('emit', (compilation, callback) => { - forEachOfLimit(compilation.chunks, 5, (chunk, key, cb) => { + const changedChunks = compilation.chunks.filter((chunk) => { + const name = chunk.name || chunk.id; + const prevHash = this.chunkHashs[name]; + const currHash = chunk.hash; + this.chunkHashs[name] = currHash; + return !prevHash || (currHash !== prevHash); + }); + + forEachOfLimit(changedChunks, 5, (chunk, key, cb) => { var rtlFiles = [], cssnanoPromise = Promise.resolve() From 96ec97f768dbee4ccc2a7fc10dc7de75cdaaff31 Mon Sep 17 00:00:00 2001 From: asychyov Date: Fri, 15 Jun 2018 11:42:28 +0300 Subject: [PATCH 2/3] allow to load 'rtl-css'-chunks instead of original --- src/index.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 9139425..cb1e18c 100644 --- a/src/index.js +++ b/src/index.js @@ -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]; @@ -79,7 +93,7 @@ WebpackRTLPlugin.prototype.apply = function(compiler) { chunk.files.push.apply(chunk.files, rtlFiles) cb() }) - }, callback) + }) }) } From 886c2d023112f8a5ff8c47e1ee77d6180068c80d Mon Sep 17 00:00:00 2001 From: asychyov Date: Fri, 15 Jun 2018 12:24:06 +0300 Subject: [PATCH 3/3] updated package.json and readme.md --- README.md | 4 ++++ package.json | 4 ++-- src/index.js | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 72216ba..2890d36 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,8 @@ This will create the normal `style.css` and an additionnal `style.rtl.css`. ``` new WebpackRTLPlugin({ filename: 'style.[contenthash].rtl.css', + updateRuntimeChunk: true, + rtlFlag: 'IS_RTL', options: {}, plugins: [], diffOnly: false, @@ -56,6 +58,8 @@ new WebpackRTLPlugin({ * `filename` the filename of the result file. May contain `[contenthash]`. Default to `style.css`. * `[contenthash]` a hash of the content of the extracted file +* `updateRuntimeChunk` updates webpack runtime to look for `.rtl.css` async chunks instead of `.css`. Works along with `mini-css-extract-plugin`. +* `rtlFlag`. If `updateRuntimeChunk` is set to `true` will look for global var (IS_RTL by default) passed as a string value. * `options` Options given to `rtlcss`. See the [rtlcss documentation for available options](http://rtlcss.com/learn/usage-guide/options/). * `plugins` RTLCSS plugins given to `rtlcss`. See the [rtlcss documentation for writing plugins](http://rtlcss.com/learn/extending-rtlcss/writing-a-plugin/). Default to `[]`. * `diffOnly` If set to `true`, the stylesheet created will only contain the css that differs from the source stylesheet. Default to `false`. diff --git a/package.json b/package.json index 03161d6..d1f7428 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "webpack-rtl-plugin", - "version": "1.6.0", + "version": "1.7.0", "description": "Webpack plugin to produce a rtl css bundle", "main": "lib/index.js", "scripts": { @@ -35,7 +35,7 @@ "extract-text-webpack-plugin": "^1.0.1", "mocha": "^2.4.5", "style-loader": "^0.13.1", - "webpack": "^1.13.0" + "webpack": "^4.0.0" }, "dependencies": { "@romainberger/css-diff": "^1.0.3", diff --git a/src/index.js b/src/index.js index cb1e18c..b10cc29 100644 --- a/src/index.js +++ b/src/index.js @@ -13,7 +13,7 @@ const WebpackRTLPlugin = function(options = {filename: false, options: {}, plugi } WebpackRTLPlugin.prototype.apply = function(compiler) { - if (this.options.options.updateRuntimeChunk) { + if (this.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) => {