@automattic/i18n-check-webpack-plugin 1.1.50 → 1.1.51

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [1.1.51] - 2026-11-23
9
+ ### Fixed
10
+ - Use the compilation's `webpack` instance rather than requiring our own copy. This reduces the risk of duplicate classes and such with pnpm-style peer dependencies. [#54066]
11
+
8
12
  ## [1.1.50] - 2026-11-16
9
13
  ### Changed
10
14
  - Update package dependencies. [#54703]
@@ -378,6 +382,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
378
382
  ### Added
379
383
  - Initial release.
380
384
 
385
+ [1.1.51]: https://github.com/Automattic/i18n-check-webpack-plugin/compare/v1.1.50...v1.1.51
381
386
  [1.1.50]: https://github.com/Automattic/i18n-check-webpack-plugin/compare/v1.1.49...v1.1.50
382
387
  [1.1.49]: https://github.com/Automattic/i18n-check-webpack-plugin/compare/v1.1.48...v1.1.49
383
388
  [1.1.48]: https://github.com/Automattic/i18n-check-webpack-plugin/compare/v1.1.47...v1.1.48
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@automattic/i18n-check-webpack-plugin",
3
- "version": "1.1.50",
3
+ "version": "1.1.51",
4
4
  "description": "A Webpack plugin to check that WordPress i18n hasn't been mangled by Webpack optimizations.",
5
5
  "keywords": [
6
6
  "webpack",
@@ -1,6 +1,6 @@
1
1
  const npath = require( 'path' );
2
2
  const babel = require( '@babel/core' );
3
- const webpack = require( 'webpack' );
3
+ const { validateSchema } = require( 'webpack' );
4
4
  const GettextEntries = require( './GettextEntries.js' );
5
5
  const GettextEntry = require( './GettextEntry.js' );
6
6
  const GettextExtractor = require( './GettextExtractor.js' );
@@ -96,7 +96,7 @@ class I18nCheckPlugin {
96
96
  #expectDomain;
97
97
 
98
98
  constructor( options = {} ) {
99
- webpack.validateSchema( schema, options, {
99
+ validateSchema( schema, options, {
100
100
  name: PLUGIN_NAME,
101
101
  baseDataPath: 'options',
102
102
  } );
@@ -126,9 +126,9 @@ class I18nCheckPlugin {
126
126
  /**
127
127
  * Record the resources for an asset.
128
128
  *
129
- * @param {webpack.Compilation} compilation - Compilation.
130
- * @param {string} filename - Asset filename.
131
- * @param {webpack.Module[]} modules - Modules in the asset.
129
+ * @param {import('webpack').Compilation} compilation - Compilation.
130
+ * @param {string} filename - Asset filename.
131
+ * @param {import('webpack').Module[]} modules - Modules in the asset.
132
132
  */
133
133
  #recordResourcesForAsset( compilation, filename, modules ) {
134
134
  const resources = new Set();
@@ -189,7 +189,7 @@ class I18nCheckPlugin {
189
189
  compilation.hooks.processAssets.tapPromise(
190
190
  {
191
191
  name: PLUGIN_NAME,
192
- stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ANALYSE,
192
+ stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ANALYSE,
193
193
  additionalAssets: true,
194
194
  },
195
195
  assets => {
@@ -209,9 +209,9 @@ class I18nCheckPlugin {
209
209
  /**
210
210
  * Process an asset.
211
211
  *
212
- * @param {webpack.Compilation} compilation - Compilation.
213
- * @param {string} filename - Asset filename.
214
- * @param {Map} moduleCache - Cache for processed modules.
212
+ * @param {import('webpack').Compilation} compilation - Compilation.
213
+ * @param {string} filename - Asset filename.
214
+ * @param {Map} moduleCache - Cache for processed modules.
215
215
  */
216
216
  async #processAsset( compilation, filename, moduleCache ) {
217
217
  // eslint-disable-next-line @wordpress/no-unused-vars-before-return -- Grabbing current timestamp for timing.
@@ -6,156 +6,154 @@
6
6
  * accepted by the constructor.
7
7
  */
8
8
 
9
- const {
10
- UsageState,
11
- Template: {
12
- numberToIdentifier,
13
- NUMBER_OF_IDENTIFIER_START_CHARS,
14
- NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS,
15
- },
16
- util: {
17
- comparators: { compareSelect, compareStringsNumeric },
18
- },
19
- } = require( 'webpack' );
20
9
  const { assignDeterministicIds } = require( 'webpack/lib/ids/IdHelpers' );
21
10
 
22
- /** @typedef {import("../Compiler")} Compiler */
23
- /** @typedef {import("../ExportsInfo")} ExportsInfo */
24
- /** @typedef {import("../ExportsInfo").ExportInfo} ExportInfo */
25
-
26
- /**
27
- * Determine if we can mangle.
28
- * @param {ExportsInfo} exportsInfo - exports info
29
- * @return {boolean} mangle is possible
30
- */
31
- const canMangle = exportsInfo => {
32
- if ( exportsInfo.otherExportsInfo.getUsed( undefined ) !== UsageState.Unused ) {
33
- return false;
34
- }
35
- let hasSomethingToMangle = false;
36
- for ( const exportInfo of exportsInfo.exports ) {
37
- if ( exportInfo.canMangle === true ) {
38
- hasSomethingToMangle = true;
39
- }
40
- }
41
- return hasSomethingToMangle;
42
- };
43
-
44
- // Sort by name
45
- const comparator = compareSelect( e => e.name, compareStringsNumeric );
46
- /**
47
- * Mangle exports.
48
- * @param {boolean} deterministic - use deterministic names
49
- * @param {ExportsInfo} exportsInfo - exports info
50
- * @param {boolean | undefined} isNamespace - is namespace object
51
- * @return {void}
52
- */
53
- const mangleExportsInfo = ( deterministic, exportsInfo, isNamespace ) => {
54
- if ( ! canMangle( exportsInfo ) ) {
55
- return;
56
- }
57
- const usedNames = new Set( [ '__', '_x', '_n', '_nx' ] );
58
- /** @type {ExportInfo[]} */
59
- const mangleableExports = [];
60
-
61
- // Avoid to renamed exports that are not provided when
62
- // 1. it's not a namespace export: non-provided exports can be found in prototype chain
63
- // 2. there are other provided exports and deterministic mode is chosen:
64
- // non-provided exports would break the determinism
65
- let avoidMangleNonProvided = ! isNamespace;
66
- if ( ! avoidMangleNonProvided && deterministic ) {
67
- for ( const exportInfo of exportsInfo.ownedExports ) {
68
- if ( exportInfo.provided !== false ) {
69
- avoidMangleNonProvided = true;
70
- break;
71
- }
72
- }
73
- }
74
- for ( const exportInfo of exportsInfo.ownedExports ) {
75
- const name = exportInfo.name;
76
- if ( ! exportInfo.hasUsedName() ) {
77
- if (
78
- // Can the export be mangled?
79
- exportInfo.canMangle !== true ||
80
- // Never rename 1 char exports
81
- ( name.length === 1 && /^[a-zA-Z0-9_$]/.test( name ) ) ||
82
- // Don't rename 2 char exports in deterministic mode
83
- ( deterministic &&
84
- name.length === 2 &&
85
- /^[a-zA-Z_$][a-zA-Z0-9_$]|^[1-9][0-9]/.test( name ) ) ||
86
- // Don't rename exports that are not provided
87
- ( avoidMangleNonProvided && exportInfo.provided !== true )
88
- ) {
89
- exportInfo.setUsedName( name );
90
- usedNames.add( name );
91
- } else {
92
- mangleableExports.push( exportInfo );
93
- }
94
- }
95
- if ( exportInfo.exportsInfoOwned ) {
96
- const used = exportInfo.getUsed( undefined );
97
- if ( used === UsageState.OnlyPropertiesUsed || used === UsageState.Unused ) {
98
- mangleExportsInfo( deterministic, exportInfo.exportsInfo, false );
99
- }
100
- }
101
- }
102
- if ( deterministic ) {
103
- assignDeterministicIds(
104
- mangleableExports,
105
- e => e.name,
106
- comparator,
107
- ( e, id ) => {
108
- const name = numberToIdentifier( id );
109
- const size = usedNames.size;
110
- usedNames.add( name );
111
- if ( size === usedNames.size ) {
112
- return false;
113
- }
114
- e.setUsedName( name );
115
- return true;
116
- },
117
- [
118
- NUMBER_OF_IDENTIFIER_START_CHARS,
119
- NUMBER_OF_IDENTIFIER_START_CHARS * NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS,
120
- ],
121
- NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS,
122
- usedNames.size
123
- );
124
- } else {
125
- const usedExports = [];
126
- const unusedExports = [];
127
- for ( const exportInfo of mangleableExports ) {
128
- if ( exportInfo.getUsed( undefined ) === UsageState.Unused ) {
129
- unusedExports.push( exportInfo );
130
- } else {
131
- usedExports.push( exportInfo );
132
- }
133
- }
134
- usedExports.sort( comparator );
135
- unusedExports.sort( comparator );
136
- let i = 0;
137
- for ( const list of [ usedExports, unusedExports ] ) {
138
- for ( const exportInfo of list ) {
139
- let name;
140
- do {
141
- name = numberToIdentifier( i++ );
142
- } while ( usedNames.has( name ) );
143
- exportInfo.setUsedName( name );
144
- }
145
- }
146
- }
147
- };
148
-
149
11
  class MangleExportsPlugin {
150
12
  constructor( options ) {
151
13
  this._deterministic = options?.deterministic ?? true;
152
14
  }
153
15
  /**
154
16
  * Apply the plugin
155
- * @param {Compiler} compiler - the compiler instance
17
+ * @param {import('webpack').Compiler} compiler - the compiler instance
156
18
  * @return {void}
157
19
  */
158
20
  apply( compiler ) {
21
+ const {
22
+ UsageState,
23
+ Template: {
24
+ numberToIdentifier,
25
+ NUMBER_OF_IDENTIFIER_START_CHARS,
26
+ NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS,
27
+ },
28
+ util: {
29
+ comparators: { compareSelect, compareStringsNumeric },
30
+ },
31
+ } = compiler.webpack;
32
+
33
+ /**
34
+ * Determine if we can mangle.
35
+ * @param {import('webpack').ExportsInfo} exportsInfo - exports info
36
+ * @return {boolean} mangle is possible
37
+ */
38
+ const canMangle = exportsInfo => {
39
+ if ( exportsInfo.otherExportsInfo.getUsed( undefined ) !== UsageState.Unused ) {
40
+ return false;
41
+ }
42
+ let hasSomethingToMangle = false;
43
+ for ( const exportInfo of exportsInfo.exports ) {
44
+ if ( exportInfo.canMangle === true ) {
45
+ hasSomethingToMangle = true;
46
+ }
47
+ }
48
+ return hasSomethingToMangle;
49
+ };
50
+
51
+ // Sort by name
52
+ const comparator = compareSelect( e => e.name, compareStringsNumeric );
53
+
54
+ /**
55
+ * Mangle exports.
56
+ * @param {boolean} deterministic - use deterministic names
57
+ * @param {import('webpack').ExportsInfo} exportsInfo - exports info
58
+ * @param {boolean | undefined} isNamespace - is namespace object
59
+ * @return {void}
60
+ */
61
+ const mangleExportsInfo = ( deterministic, exportsInfo, isNamespace ) => {
62
+ if ( ! canMangle( exportsInfo ) ) {
63
+ return;
64
+ }
65
+ const usedNames = new Set( [ '__', '_x', '_n', '_nx' ] );
66
+ /** @type {import('webpack').ExportInfo[]} */
67
+ const mangleableExports = [];
68
+
69
+ // Avoid to renamed exports that are not provided when
70
+ // 1. it's not a namespace export: non-provided exports can be found in prototype chain
71
+ // 2. there are other provided exports and deterministic mode is chosen:
72
+ // non-provided exports would break the determinism
73
+ let avoidMangleNonProvided = ! isNamespace;
74
+ if ( ! avoidMangleNonProvided && deterministic ) {
75
+ for ( const exportInfo of exportsInfo.ownedExports ) {
76
+ if ( exportInfo.provided !== false ) {
77
+ avoidMangleNonProvided = true;
78
+ break;
79
+ }
80
+ }
81
+ }
82
+ for ( const exportInfo of exportsInfo.ownedExports ) {
83
+ const name = exportInfo.name;
84
+ if ( ! exportInfo.hasUsedName() ) {
85
+ if (
86
+ // Can the export be mangled?
87
+ exportInfo.canMangle !== true ||
88
+ // Never rename 1 char exports
89
+ ( name.length === 1 && /^[a-zA-Z0-9_$]/.test( name ) ) ||
90
+ // Don't rename 2 char exports in deterministic mode
91
+ ( deterministic &&
92
+ name.length === 2 &&
93
+ /^[a-zA-Z_$][a-zA-Z0-9_$]|^[1-9][0-9]/.test( name ) ) ||
94
+ // Don't rename exports that are not provided
95
+ ( avoidMangleNonProvided && exportInfo.provided !== true )
96
+ ) {
97
+ exportInfo.setUsedName( name );
98
+ usedNames.add( name );
99
+ } else {
100
+ mangleableExports.push( exportInfo );
101
+ }
102
+ }
103
+ if ( exportInfo.exportsInfoOwned ) {
104
+ const used = exportInfo.getUsed( undefined );
105
+ if ( used === UsageState.OnlyPropertiesUsed || used === UsageState.Unused ) {
106
+ mangleExportsInfo( deterministic, exportInfo.exportsInfo, false );
107
+ }
108
+ }
109
+ }
110
+ if ( deterministic ) {
111
+ assignDeterministicIds(
112
+ mangleableExports,
113
+ e => e.name,
114
+ comparator,
115
+ ( e, id ) => {
116
+ const name = numberToIdentifier( id );
117
+ const size = usedNames.size;
118
+ usedNames.add( name );
119
+ if ( size === usedNames.size ) {
120
+ return false;
121
+ }
122
+ e.setUsedName( name );
123
+ return true;
124
+ },
125
+ [
126
+ NUMBER_OF_IDENTIFIER_START_CHARS,
127
+ NUMBER_OF_IDENTIFIER_START_CHARS * NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS,
128
+ ],
129
+ NUMBER_OF_IDENTIFIER_CONTINUATION_CHARS,
130
+ usedNames.size
131
+ );
132
+ } else {
133
+ const usedExports = [];
134
+ const unusedExports = [];
135
+ for ( const exportInfo of mangleableExports ) {
136
+ if ( exportInfo.getUsed( undefined ) === UsageState.Unused ) {
137
+ unusedExports.push( exportInfo );
138
+ } else {
139
+ usedExports.push( exportInfo );
140
+ }
141
+ }
142
+ usedExports.sort( comparator );
143
+ unusedExports.sort( comparator );
144
+ let i = 0;
145
+ for ( const list of [ usedExports, unusedExports ] ) {
146
+ for ( const exportInfo of list ) {
147
+ let name;
148
+ do {
149
+ name = numberToIdentifier( i++ );
150
+ } while ( usedNames.has( name ) );
151
+ exportInfo.setUsedName( name );
152
+ }
153
+ }
154
+ }
155
+ };
156
+
159
157
  const { _deterministic: deterministic } = this;
160
158
  compiler.hooks.compilation.tap( 'MangleExportsPlugin', compilation => {
161
159
  const moduleGraph = compilation.moduleGraph;