wp-webpack-script icon indicating copy to clipboard operation
wp-webpack-script copied to clipboard

SVGO Documentation outdated

Open derweili opened this issue 3 years ago • 13 comments

The svgo library got a major update on 19 February. The Docs https://github.com/swashata/wp-webpack-script/blob/master/site/docs/tutorials/using-various-svg-loader.md only work with

"svgo": "^1.3.2",
"svgo-loader": "^2.2.2"

When using the latest svgo version the build process returns following error Error: Plugin name should be specified.

I haven't figured out what needs to be changed in the config yet, downgrading to the version mentioned above does work as a hotfix.

derweili avatar Apr 26 '21 10:04 derweili

I do use svgo in my project. I will take a look and get back to you. Thank you for the info.

swashata avatar Apr 26 '21 13:04 swashata

Running into the same issue.

sethrubenstein avatar Apr 26 '21 16:04 sethrubenstein

Hello,

It turns out I use @svgr/webpack because my projects are react specific. But I took a look at the svg-loader documentation. It says the loader should be

loader: 'svgo-loader',
options: {
  configFile: './scripts/svgo.config.js'
}

So I am thinking, maybe try this and let me know how this works out

const {
	getFileLoaderOptions,
	issuerForNonStyleFiles,
	issuerForStyleFiles,
	babelLoader,
	fileLoader,
} = require('@wpackio/scripts');

module.exports = {
	// ...
	files: [
		{
			name: 'app',
			entry: {
				main: ['./src/app/index.js'],
				// stuff
			},
			// Extra webpack config to be dynamically created
			webpackConfig: (config, merge, appDir, isDev) => {
				const svgoLoader = {
					loader: 'svgo-loader',
-					options: {
-						plugins: [
-							{ removeTitle: true },
-							{ convertColors: { shorthex: false } },
-							{ convertPathData: false },
-						],
-					},
				};
				// create module rules
				const configWithSvg = {
					module: {
						rules: [
							// SVGO Loader
							// https://github.com/rpominov/svgo-loader
							// This rule handles SVG for javascript files
							{
								test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
								use: [
									{
										loader: fileLoader,
										options: getFileLoaderOptions(
											appDir,
											isDev,
											false
										),
									},
									svgoLoader,
								],
								issuer: issuerForNonStyleFiles,
							},
							// This rule handles SVG for style files
							{
								test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
								use: [
									{
										loader: fileLoader,
										options: getFileLoaderOptions(
											appDir,
											isDev,
											true
										),
									},
									svgoLoader,
								],
								issuer: issuerForStyleFiles,
							},
						],
					},
				};
				// merge the new module.rules with webpack-merge api
				return merge(config, configWithSvg);
			},
		},
	],
	// ...
};

and add a svgo.config.js to the root of your project.

swashata avatar Apr 27 '21 02:04 swashata

Actually, svgo.config.js isn't required. The configurations just need to be directly inside options property, instead of an array within plugin property.

The following sample works:

options: {
  { removeTitle: true },
  { convertColors: { shorthex: false } },
  { convertPathData: false },
},

robsonsobral avatar Apr 28 '21 23:04 robsonsobral

Thank you @robsonsobral, do you mind sending in a PR to update the docs?

swashata avatar Apr 29 '21 02:04 swashata

Sure! I'm glad to help!


By the way, I'm still learning on how to make WPack to fully work.

robsonsobral avatar Apr 29 '21 19:04 robsonsobral

Thanks all!

sethrubenstein avatar Apr 29 '21 22:04 sethrubenstein

@robsonsobral you can edit this file https://github.com/swashata/wp-webpack-script/blob/master/site/docs/tutorials/using-various-svg-loader.md and send a PR. Once I merge it, it will automatically update the site.

swashata avatar May 01 '21 09:05 swashata

Done, @swashata . However, I think you have confused the other issue I opened with this one. ;-)

robsonsobral avatar May 01 '21 20:05 robsonsobral

I just tried the configuration as proposed above, which didn't work for me. The configuration below however, does seem to work:

const svgoLoader = {
  loader: "svgo-loader",
  options: {
      removeTitle: true,
      convertColors: { shorthex: false },
      convertPathData: false ,
  },
};

robbertvancaem avatar May 11 '21 10:05 robbertvancaem

Thank you @robbertvancaem for the update. @robsonsobral could you please confirm this? We may need to update the docs.

swashata avatar May 12 '21 06:05 swashata

"Five years later", yes, I confirm!

robsonsobral avatar Sep 20 '21 16:09 robsonsobral

Haha, no worries. I will keep this open for now and will update the docs when I get a chance.

swashata avatar Oct 01 '21 05:10 swashata