Skip to content

Commit 67b65b4

Browse files
committed
Replace WebpackRTLPlugin with a more-compatible bespoke solution.
1 parent e54e7d2 commit 67b65b4

File tree

5 files changed

+73
-1592
lines changed

5 files changed

+73
-1592
lines changed

config/RtlCssPlugin.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import path from 'path'
2+
import rtlcss from 'rtlcss'
3+
import type { Compiler } from 'webpack'
4+
import type { ConfigureOptions } from 'rtlcss'
5+
6+
export interface WebpackRTLPluginOptions {
7+
entries: Set<string>
8+
rtlcssConfig: ConfigureOptions
9+
transformFilename: (sourceFilename: string) => string
10+
}
11+
12+
export class RtlCssPlugin {
13+
private readonly options: WebpackRTLPluginOptions
14+
15+
private static readonly defaultOptions: WebpackRTLPluginOptions = {
16+
entries: new Set(),
17+
rtlcssConfig: {},
18+
transformFilename: sourceFilename =>
19+
`${path.parse(sourceFilename).name}-rtl.css`
20+
}
21+
22+
constructor(options: Partial<WebpackRTLPluginOptions>) {
23+
this.options = { ...RtlCssPlugin.defaultOptions, ...options }
24+
}
25+
26+
apply(compiler: Compiler) {
27+
const { Compilation, sources: { ConcatSource } } = compiler.webpack
28+
const rtlcssProcessor = rtlcss.configure(this.options.rtlcssConfig)
29+
30+
compiler.hooks.compilation.tap(RtlCssPlugin.name, compilation => {
31+
compilation.hooks.processAssets.tap({
32+
name: RtlCssPlugin.name,
33+
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
34+
}, assets => {
35+
for (const chunk of compilation.chunks) {
36+
if (chunk.name && !this.options.entries.has(chunk.name)) {
37+
for (const sourceFilename of chunk.files) {
38+
if ('.css' === path.extname(sourceFilename)) {
39+
const rtlFilename = this.options.transformFilename(sourceFilename)
40+
const ltrCss = assets[sourceFilename].source()
41+
const rtlCss = rtlcssProcessor.process(ltrCss).css
42+
compilation.emitAsset(rtlFilename, new ConcatSource(rtlCss), { sourceFilename })
43+
}
44+
}
45+
}
46+
}
47+
})
48+
})
49+
}
50+
}

config/modules/webpack-rtl-plugin.d.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

config/webpack-css.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import autoprefixer from 'autoprefixer'
55
import hexrgba from 'postcss-hexrgba'
66
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
77
import RemoveEmptyScriptsPlugin from 'webpack-remove-empty-scripts'
8-
import WebpackRTLPlugin from 'webpack-rtl-plugin'
98
import { glob } from 'glob'
10-
import type { Config as PostCssConfig } from 'postcss-load-config'
9+
import { RtlCssPlugin } from './RtlCssPlugin'
1110
import type { Configuration, EntryObject } from 'webpack'
11+
import type { Config as PostCssConfig } from 'postcss-load-config'
1212

1313
const postcssOptions: PostCssConfig = {
1414
plugins: [
@@ -99,9 +99,8 @@ export const cssWebpackConfig: Configuration = {
9999
.replace(/-css\.css$/, '.css') :
100100
'[name].css'
101101
}),
102-
new WebpackRTLPlugin({
103-
test: /^(?<filename>edit|manage)\.css$/,
104-
filename: [/(?<ext>\.css)/i, '-rtl$1']
102+
new RtlCssPlugin({
103+
entries: new Set(['manage-css', 'edit-css'])
105104
})
106105
]
107106
}

0 commit comments

Comments
 (0)