File size: 1,352 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
const webpack = require( 'webpack' );
const PLUGIN_NAME = 'MiniCSSWithRTL';

class MiniCSSWithRTLModule extends webpack.RuntimeModule {
	constructor( { globalVar = null } = {} ) {
		super( 'get mini-css chunk filename with rtl' );
		this.globalVar = globalVar;
	}

	generate() {
		const { compilation } = this;
		const { runtimeTemplate } = compilation;
		const namespace = webpack.RuntimeGlobals.require;
		const template = webpack.Template;
		const globalBar = this.globalVar
			? `window[${ JSON.stringify( this.globalVar ) }]`
			: 'document.dir';

		return `${ namespace }.miniCssF = (
			${ runtimeTemplate.returningFunction(
				runtimeTemplate.basicFunction(
					'chunkId',
					template.indent( [
						'var isCssRtlEnabled = ' + globalBar + " === 'rtl';",
						'var originalUrl = originalFn(chunkId);',
						'return isCssRtlEnabled ? originalUrl.replace(".css",".rtl.css") : originalUrl;',
					] )
				),
				'originalFn'
			) }
		)(${ namespace }.miniCssF)`;
	}
}

module.exports = class MiniCSSWithRTLPlugin {
	apply( compiler ) {
		compiler.hooks.thisCompilation.tap( PLUGIN_NAME, ( compilation ) => {
			compilation.hooks.runtimeRequirementInTree
				.for( webpack.RuntimeGlobals.ensureChunkHandlers )
				.tap( PLUGIN_NAME, ( chunk ) => {
					compilation.addRuntimeModule( chunk, new MiniCSSWithRTLModule() );
				} );
		} );
	}
};