Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webpack-rtl-plugin",
"version": "2.0.0",
"version": "3.0.0",
"description": "Webpack plugin to produce a rtl css bundle",
"main": "src/index.js",
"scripts": {
Expand All @@ -23,16 +23,15 @@
"license": "MIT",
"devDependencies": {
"chai": "4.2.0",
"css-loader": "2.1.1",
"mini-css-extract-plugin": "0.5.0",
"css-loader": "6.3.0",
"mini-css-extract-plugin": "2.3.0",
"mocha": "6.0.2",
"webpack": "4.29.6"
"webpack": "5.52.1"
},
"dependencies": {
"@romainberger/css-diff": "^1.0.3",
"async": "^2.0.0",
"cssnano": "4.1.10",
"rtlcss": "2.4.0",
"webpack-sources": "1.3.0"
"rtlcss": "2.4.0"
}
}
148 changes: 75 additions & 73 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const path = require('path')
const { createHash } = require('crypto')
const rtlcss = require('rtlcss')
const { ConcatSource } = require('webpack-sources')
const { ConcatSource } = require('webpack').sources
const cssDiff = require('@romainberger/css-diff')
const { forEachOfLimit } = require('async')
const cssnano = require('cssnano')
Expand All @@ -19,95 +19,97 @@ class WebpackRTLPlugin {
}

apply(compiler) {
compiler.hooks.emit.tapAsync(pluginName, (compilation, callback) => {
forEachOfLimit(compilation.chunks, 5, (chunk, key, cb) => {
const rtlFiles = []
let cssnanoPromise = Promise.resolve()
compiler.hooks.compilation.tap(pluginName, (compilation) => {
compilation.hooks.processAssets.tapAsync(pluginName, (assets, callback) => {
forEachOfLimit([...compilation.chunks], 5, (chunk, key, cb) => {
const rtlFiles = []
let cssnanoPromise = Promise.resolve()

chunk.files.forEach(asset => {
const match = this.options.test ? new RegExp(this.options.test).test(asset) : true
for (let asset of chunk.files) {
const match = this.options.test ? new RegExp(this.options.test).test(asset) : true

if (path.extname(asset) !== '.css') {
return
}
if (path.extname(asset) !== '.css') {
continue;
}

const baseSource = compilation.assets[asset].source()
let filename
let rtlSource
const baseSource = compilation.assets[asset].source()
let filename
let rtlSource

if (match) {
rtlSource = rtlcss.process(baseSource, this.options.options, this.options.plugins)
if (match) {
rtlSource = rtlcss.process(baseSource.toString(), this.options.options, this.options.plugins)

if (this.options.filename instanceof Array && this.options.filename.length === 2) {
filename = asset.replace(this.options.filename[0], this.options.filename[1])
}
else if (this.options.filename) {
filename = this.options.filename

if (/\[contenthash]/.test(this.options.filename)) {
const hash = createHash('md5').update(rtlSource).digest('hex').substr(0, 10)
filename = filename.replace('[contenthash]', hash)
}
if (/\[id]/.test(this.options.filename)) {
filename = filename.replace('[id]', chunk.id)
if (this.options.filename instanceof Array && this.options.filename.length === 2) {
filename = asset.replace(this.options.filename[0], this.options.filename[1])
}
if (/\[name]/.test(this.options.filename)) {
filename = filename.replace('[name]', chunk.name)
else if (this.options.filename) {
filename = this.options.filename

if (/\[contenthash]/.test(this.options.filename)) {
const hash = createHash('md5').update(rtlSource).digest('hex').substr(0, 10)
filename = filename.replace('[contenthash]', hash)
}
if (/\[id]/.test(this.options.filename)) {
filename = filename.replace('[id]', chunk.id)
}
if (/\[name]/.test(this.options.filename)) {
filename = filename.replace('[name]', chunk.name)
}
if (/\[file]/.test(this.options.filename)) {
filename = filename.replace('[file]', asset)
}
if (/\[filebase]/.test(this.options.filename)) {
filename = filename.replace('[filebase]', path.basename(asset))
}
if (/\[ext]/.test(this.options.filename)) {
filename = filename.replace('.[ext]', path.extname(asset))
}
}
if (/\[file]/.test(this.options.filename)) {
filename = filename.replace('[file]', asset)
else {
const newFilename = `${path.basename(asset, '.css')}.rtl`
filename = asset.replace(path.basename(asset, '.css'), newFilename)
}
if (/\[filebase]/.test(this.options.filename)) {
filename = filename.replace('[filebase]', path.basename(asset))
}
if (/\[ext]/.test(this.options.filename)) {
filename = filename.replace('.[ext]', path.extname(asset))
}
}
else {
const newFilename = `${path.basename(asset, '.css')}.rtl`
filename = asset.replace(path.basename(asset, '.css'), newFilename)
}

if (this.options.diffOnly) {
rtlSource = cssDiff(baseSource, rtlSource)
}
}

if (this.options.minify !== false) {
let nanoOptions = { from: undefined }
if (typeof this.options.minify === 'object') {
nanoOptions = this.options.minify
if (this.options.diffOnly) {
rtlSource = cssDiff(baseSource, rtlSource)
}
}

cssnanoPromise = cssnanoPromise.then(() => {
let minify = cssnano.process( baseSource, nanoOptions).then(output => {
compilation.assets[asset] = new ConcatSource(output.css)
});
if (this.options.minify !== false) {
let nanoOptions = { from: undefined }
if (typeof this.options.minify === 'object') {
nanoOptions = this.options.minify
}

if (match) {
const rtlMinify = cssnano.process(rtlSource, nanoOptions).then(output => {
compilation.assets[filename] = new ConcatSource(output.css)
rtlFiles.push(filename)
cssnanoPromise = cssnanoPromise.then(() => {
let minify = cssnano.process(baseSource, nanoOptions).then(output => {
compilation.assets[asset] = new ConcatSource(output.css)
});

minify = Promise.all([minify, rtlMinify]);
}
if (match) {
const rtlMinify = cssnano.process(rtlSource, nanoOptions).then(output => {
compilation.assets[filename] = new ConcatSource(output.css)
rtlFiles.push(filename)
});

return minify;
})
}
else if (match) {
compilation.assets[filename] = new ConcatSource(rtlSource)
rtlFiles.push(filename)
minify = Promise.all([minify, rtlMinify]);
}

return minify;
})
}
else if (match) {
compilation.assets[filename] = new ConcatSource(rtlSource)
rtlFiles.push(filename)
}
}
})

cssnanoPromise.then(() => {
chunk.files.push.apply(chunk.files, rtlFiles)
cb()
})
}, callback)
cssnanoPromise.then(() => {
rtlFiles.forEach(f => chunk.files.add(f));
cb()
});
}, callback)
})
})
}
}
Expand Down
29 changes: 15 additions & 14 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ const baseConfig = {
{
loader: "css-loader",
options: {
modules: true,
url: false,
importLoaders: 1,
localIdentName: "[local]",
modules: {
localIdentName: "[local]"
},
url: false,
importLoaders: 1
},
}
],
Expand All @@ -33,7 +34,7 @@ const baseConfig = {
},
plugins: [
new MiniCssExtractPlugin({
filename: 'style.css',
filename: 'style.css',
}),
new WebpackRTLPlugin({
minify: false,
Expand Down Expand Up @@ -98,7 +99,7 @@ describe('Webpack RTL Plugin', () => {
},
plugins: [
new MiniCssExtractPlugin({
filename: 'css/style.css',
filename: 'css/style.css',
}),
new WebpackRTLPlugin({
test: /css\//i,
Expand Down Expand Up @@ -146,7 +147,7 @@ describe('Webpack RTL Plugin', () => {
},
plugins: [
new MiniCssExtractPlugin({
filename: 'style.[contenthash].css',
filename: 'style.[contenthash].css',
}),
new WebpackRTLPlugin({
filename: 'style.[contenthash].rtl.css',
Expand Down Expand Up @@ -211,7 +212,7 @@ describe('Webpack RTL Plugin', () => {
},
plugins: [
new MiniCssExtractPlugin({
filename: 'style.[contenthash].css',
filename: 'style.[contenthash].css',
}),
new WebpackRTLPlugin({
filename: '[id]-[file]-[contenthash]-[name]-[filebase].rtl.[ext]',
Expand Down Expand Up @@ -276,7 +277,7 @@ describe('Webpack RTL Plugin', () => {
},
plugins: [
new MiniCssExtractPlugin({
filename: 'style.[contenthash].css',
filename: 'style.[contenthash].css',
}),
new WebpackRTLPlugin({
filename: [/(\.css)/, '-rtl$1'],
Expand Down Expand Up @@ -331,7 +332,7 @@ describe('Webpack RTL Plugin', () => {
},
plugins: [
new MiniCssExtractPlugin({
filename: path.join(cssPath, 'style.css'),
filename: path.join(cssPath, 'style.css'),
}),
new WebpackRTLPlugin(),
],
Expand Down Expand Up @@ -371,7 +372,7 @@ describe('Webpack RTL Plugin', () => {
},
plugins: [
new MiniCssExtractPlugin({
filename: 'style.css',
filename: 'style.css',
}),
new WebpackRTLPlugin({
options: {
Expand Down Expand Up @@ -422,7 +423,7 @@ describe('Webpack RTL Plugin', () => {
},
plugins: [
new MiniCssExtractPlugin({
filename: 'style.css',
filename: 'style.css',
}),
new WebpackRTLPlugin({
plugins: [
Expand Down Expand Up @@ -476,7 +477,7 @@ describe('Webpack RTL Plugin', () => {
},
plugins: [
new MiniCssExtractPlugin({
filename: 'style.css',
filename: 'style.css',
}),
new WebpackRTLPlugin({
diffOnly: true,
Expand Down Expand Up @@ -517,7 +518,7 @@ describe('Webpack RTL Plugin', () => {
},
plugins: [
new MiniCssExtractPlugin({
filename: 'style.css',
filename: 'style.css',
}),
new WebpackRTLPlugin(),
],
Expand Down
Loading