Skip to content

Commit

Permalink
Replace WebpackRTLPlugin with a more-compatible bespoke solution.
Browse files Browse the repository at this point in the history
  • Loading branch information
sheabunge committed Aug 18, 2024
1 parent e54e7d2 commit 67b65b4
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1,592 deletions.
50 changes: 50 additions & 0 deletions config/RtlCssPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import path from 'path'
import rtlcss from 'rtlcss'
import type { Compiler } from 'webpack'
import type { ConfigureOptions } from 'rtlcss'

export interface WebpackRTLPluginOptions {
entries: Set<string>
rtlcssConfig: ConfigureOptions
transformFilename: (sourceFilename: string) => string
}

export class RtlCssPlugin {
private readonly options: WebpackRTLPluginOptions

private static readonly defaultOptions: WebpackRTLPluginOptions = {
entries: new Set(),
rtlcssConfig: {},
transformFilename: sourceFilename =>
`${path.parse(sourceFilename).name}-rtl.css`
}

constructor(options: Partial<WebpackRTLPluginOptions>) {
this.options = { ...RtlCssPlugin.defaultOptions, ...options }
}

apply(compiler: Compiler) {
const { Compilation, sources: { ConcatSource } } = compiler.webpack
const rtlcssProcessor = rtlcss.configure(this.options.rtlcssConfig)

compiler.hooks.compilation.tap(RtlCssPlugin.name, compilation => {
compilation.hooks.processAssets.tap({
name: RtlCssPlugin.name,
stage: Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL
}, assets => {
for (const chunk of compilation.chunks) {
if (chunk.name && !this.options.entries.has(chunk.name)) {
for (const sourceFilename of chunk.files) {
if ('.css' === path.extname(sourceFilename)) {
const rtlFilename = this.options.transformFilename(sourceFilename)
const ltrCss = assets[sourceFilename].source()
const rtlCss = rtlcssProcessor.process(ltrCss).css
compilation.emitAsset(rtlFilename, new ConcatSource(rtlCss), { sourceFilename })
}
}
}
}
})
})
}
}
21 changes: 0 additions & 21 deletions config/modules/webpack-rtl-plugin.d.ts

This file was deleted.

9 changes: 4 additions & 5 deletions config/webpack-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import autoprefixer from 'autoprefixer'
import hexrgba from 'postcss-hexrgba'
import MiniCssExtractPlugin from 'mini-css-extract-plugin'
import RemoveEmptyScriptsPlugin from 'webpack-remove-empty-scripts'
import WebpackRTLPlugin from 'webpack-rtl-plugin'
import { glob } from 'glob'
import type { Config as PostCssConfig } from 'postcss-load-config'
import { RtlCssPlugin } from './RtlCssPlugin'
import type { Configuration, EntryObject } from 'webpack'
import type { Config as PostCssConfig } from 'postcss-load-config'

const postcssOptions: PostCssConfig = {
plugins: [
Expand Down Expand Up @@ -99,9 +99,8 @@ export const cssWebpackConfig: Configuration = {
.replace(/-css\.css$/, '.css') :
'[name].css'
}),
new WebpackRTLPlugin({
test: /^(?<filename>edit|manage)\.css$/,
filename: [/(?<ext>\.css)/i, '-rtl$1']
new RtlCssPlugin({
entries: new Set(['manage-css', 'edit-css'])
})
]
}
Loading

0 comments on commit 67b65b4

Please sign in to comment.