File size: 2,422 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const WebpackRTLPlugin = require( '@automattic/webpack-rtl-plugin' );
const MiniCssExtractPlugin = require( 'mini-css-extract-plugin' );
const MiniCSSWithRTLPlugin = require( './mini-css-with-rtl' );

/**
 * Return a webpack loader object containing our styling (Sass -> CSS) stack.
 * @param  {Object}    _                              Options
 * @param  {string[]}  _.includePaths                 Sass files lookup paths
 * @param  {string}    _.prelude                      String to prepend to each Sass file
 * @param  {Object}    _.postCssOptions               PostCSS options
 * @returns {Object}                                  webpack loader object
 */
module.exports.loader = ( { includePaths, prelude, postCssOptions } ) => ( {
	test: /\.(sc|sa|c)ss$/,
	use: [
		MiniCssExtractPlugin.loader,
		{
			loader: require.resolve( 'css-loader' ),
			options: {
				importLoaders: 2,
				// We do not want css-loader to resolve absolute paths. We
				// typically use `/` to indicate the start of the base URL,
				// but starting with css-loader v4, it started trying to handle
				// absolute paths itself.
				url: {
					filter: ( path ) => ! path.startsWith( '/' ),
				},
			},
		},
		{
			loader: require.resolve( 'postcss-loader' ),
			options: {
				postcssOptions: postCssOptions || {},
			},
		},
		{
			loader: require.resolve( 'sass-loader' ),
			options: {
				additionalData: prelude,
				sassOptions: {
					includePaths,
					quietDeps: true,
				},
				// The warnRuleAsWarning can be removed once sass-loader is updated to v14. It defaults to true in that version.
				// @see https://github.com/webpack-contrib/sass-loader/tree/v14.0.0?tab=readme-ov-file#warnruleaswarning
				warnRuleAsWarning: true,
			},
		},
	],
} );

/**
 * Return an array of styling relevant webpack plugin objects.
 * @param  {Object}   _                Options
 * @param  {string}   _.chunkFilename  filename pattern to use for CSS files
 * @param  {string}   _.filename       filename pattern to use for CSS chunk files
 * @returns {Object[]}                 styling relevant webpack plugin objects
 */
module.exports.plugins = ( { chunkFilename, filename } ) => [
	new MiniCssExtractPlugin( {
		chunkFilename,
		filename,
		ignoreOrder: true, // suppress conflicting order warnings from mini-css-extract-plugin
		attributes: {
			'data-webpack': true,
		},
	} ),
	new MiniCSSWithRTLPlugin(),
	new WebpackRTLPlugin(),
];