From bc086eacb4df2585b1596920d9c23300aced9f95 Mon Sep 17 00:00:00 2001 From: Paul Taylor Date: Tue, 29 Oct 2024 16:52:09 +0000 Subject: [PATCH 1/3] feat: change minification to use terser and allow translation strings --- .../src/components/DisplayComponent.tsx | 10 ++++++-- package-lock.json | 2 ++ package.json | 1 + src/commands/build/webpack.js | 25 ++++++++++++++++++- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/example-site/plugins/test-plugin/src/components/DisplayComponent.tsx b/example-site/plugins/test-plugin/src/components/DisplayComponent.tsx index 7c31513..05d0a1f 100644 --- a/example-site/plugins/test-plugin/src/components/DisplayComponent.tsx +++ b/example-site/plugins/test-plugin/src/components/DisplayComponent.tsx @@ -1,17 +1,22 @@ import SVGAsComponent from '@Static/logo.svg'; import SVGAsURL from '@Static/svg-file.svg?url'; import MailIconPNG from '@Static/mail-icon.png'; +import { __ } from '@wordpress/i18n'; import TestComponent from './TestComponent'; /** * Display Component for an example block. */ -export default (): JSX.Element => ( +export default (): JSX.Element => { + // translators: This is some basic alt-text. + const svgAlt = __("Reference and SVG as the url", 'test-translation'); + + return ( <>
- Reference and SVG as the url + {svgAlt}
Example Block
@@ -20,3 +25,4 @@ export default (): JSX.Element => ( ); +} diff --git a/package-lock.json b/package-lock.json index 203c462..3d2d6d9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -61,6 +61,7 @@ "stylelint-webpack-plugin": "^4.1.1", "svg-sprite-loader": "^6.0.9", "terminal-kit": "^2.1.8", + "terser-webpack-plugin": "^5.3.10", "ts-loader": "^9.4.2", "typescript": "^5.0.4", "url-loader": "^4.1.1", @@ -15152,6 +15153,7 @@ "version": "5.3.10", "resolved": "https://registry.npmjs.org/terser-webpack-plugin/-/terser-webpack-plugin-5.3.10.tgz", "integrity": "sha512-BKFPWlPDndPs+NGGCr1U59t0XScL5317Y0UReNrHaw9/FwhPENlq6bfgs+4yPfyP51vqC1bQ4rp1EfXW5ZSH9w==", + "license": "MIT", "dependencies": { "@jridgewell/trace-mapping": "^0.3.20", "jest-worker": "^27.4.5", diff --git a/package.json b/package.json index 62f4c31..33998e5 100644 --- a/package.json +++ b/package.json @@ -90,6 +90,7 @@ "stylelint-webpack-plugin": "^4.1.1", "svg-sprite-loader": "^6.0.9", "terminal-kit": "^2.1.8", + "terser-webpack-plugin": "^5.3.10", "ts-loader": "^9.4.2", "typescript": "^5.0.4", "url-loader": "^4.1.1", diff --git a/src/commands/build/webpack.js b/src/commands/build/webpack.js index a5eb5fd..28464fa 100644 --- a/src/commands/build/webpack.js +++ b/src/commands/build/webpack.js @@ -1,6 +1,7 @@ const fs = require('fs'); const path = require('path'); const webpack = require('webpack'); +const TerserPlugin = require('terser-webpack-plugin'); const Plugins = require('./plugins'); const Rules = require('./rules'); @@ -21,7 +22,9 @@ BROWSERSLIST_CONFIG = path.resolve(`${__dirname}/config`); */ module.exports = (__PROJECT_CONFIG__, mode) => { const customWebpackConfigFile = __PROJECT_CONFIG__.paths.project + '/webpack.config.js'; - const customConfig = fs.existsSync(customWebpackConfigFile) ? require(customWebpackConfigFile) : null; + const customConfig = fs.existsSync(customWebpackConfigFile) + ? require(customWebpackConfigFile) + : null; let webpackConfig = { mode, @@ -49,6 +52,26 @@ module.exports = (__PROJECT_CONFIG__, mode) => { maxAssetSize: 500000, // Set max size to 500kb. }, + optimization: { + minimizer: [ + new TerserPlugin({ + parallel: true, + terserOptions: { + output: { + comments: /translators:/i, + }, + compress: { + passes: 2, + }, + mangle: { + reserved: ['__', '_n', '_nx', '_x'], + }, + }, + extractComments: false, + }), + ], + }, + devtool: mode === 'production' ? 'source-map' : 'inline-cheap-module-source-map', externals: { From 9c41b1fadea84de165c002d7bda7c0eb4eb7de3f Mon Sep 17 00:00:00 2001 From: Paul Taylor Date: Tue, 29 Oct 2024 18:24:00 +0000 Subject: [PATCH 2/3] test: add number and sprintf usage examples --- .../src/components/DisplayComponent.tsx | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/example-site/plugins/test-plugin/src/components/DisplayComponent.tsx b/example-site/plugins/test-plugin/src/components/DisplayComponent.tsx index 05d0a1f..f043ffb 100644 --- a/example-site/plugins/test-plugin/src/components/DisplayComponent.tsx +++ b/example-site/plugins/test-plugin/src/components/DisplayComponent.tsx @@ -1,28 +1,32 @@ import SVGAsComponent from '@Static/logo.svg'; import SVGAsURL from '@Static/svg-file.svg?url'; import MailIconPNG from '@Static/mail-icon.png'; -import { __ } from '@wordpress/i18n'; +import { __, _n, sprintf } from '@wordpress/i18n'; import TestComponent from './TestComponent'; /** * Display Component for an example block. */ -export default (): JSX.Element => { +export default ({ attributes: { itemCount = 2 } }): JSX.Element => { + // General comment. // translators: This is some basic alt-text. const svgAlt = __("Reference and SVG as the url", 'test-translation'); + // translators: %d is the number of items chosen. + const translatedValue = sprintf(_n("%d item", "%d items", itemCount, 'test-translation'), itemCount); + return ( - <> -
- - {svgAlt} -
-
Example Block
- -
- PNG Mail Icon for testing usage. -
- -); + <> +
+ + {svgAlt} +
+
Example Block
+ +
+ PNG Mail Icon for testing usage. +
+ + ); } From 7e259f794c557546ab832f4b9233d3f1e2a071a9 Mon Sep 17 00:00:00 2001 From: Paul Taylor Date: Tue, 29 Oct 2024 18:24:50 +0000 Subject: [PATCH 3/3] 1.4.0 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3d2d6d9..5efebb2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@bigbite/build-tools", - "version": "1.3.3", + "version": "1.4.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@bigbite/build-tools", - "version": "1.3.3", + "version": "1.4.0", "license": "MIT", "dependencies": { "@babel/core": "^7.14.8", diff --git a/package.json b/package.json index 33998e5..a4ebe64 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@bigbite/build-tools", - "version": "1.3.3", + "version": "1.4.0", "description": "Provides configuration for the Big Bite Build Tools.", "author": "Paul Taylor (https://github.com/ampersarnie)", "engines": {