@wordpress/dependency-extraction-webpack-plugin 3.5.0 → 4.0.1-next.d6164808d3.0

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
@@ -2,6 +2,18 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 4.0.0-next.0 (2022-08-23)
6
+
7
+ ### Breaking Change
8
+
9
+ - Increase the minimum Node.js version to 14 ([#43141](https://github.com/WordPress/gutenberg/pull/43141)).
10
+
11
+ ## 3.7.0 (2022-07-13)
12
+
13
+ ### New Features
14
+
15
+ - Output asset files for shared chunks, too ([#41002](https://github.com/WordPress/gutenberg/pull/41002)).
16
+
5
17
  ## 3.5.0 (2022-05-18)
6
18
 
7
19
  ### Bug Fix
package/README.md CHANGED
@@ -17,7 +17,7 @@ Install the module
17
17
  npm install @wordpress/dependency-extraction-webpack-plugin --save-dev
18
18
  ```
19
19
 
20
- **Note**: This package requires Node.js 12.0.0 or later. It also requires webpack 4.8.3 and newer. It is not compatible with older versions.
20
+ **Note**: This package requires Node.js 14.0.0 or later. It also requires webpack 4.8.3 and newer. It is not compatible with older versions.
21
21
 
22
22
  ## Usage
23
23
 
package/lib/index.js CHANGED
@@ -117,7 +117,7 @@ class DependencyExtractionWebpackPlugin {
117
117
 
118
118
  if ( isWebpack4 ) {
119
119
  compiler.hooks.emit.tap( this.constructor.name, ( compilation ) =>
120
- this.addAssets( compilation, compiler )
120
+ this.addAssets( compilation )
121
121
  );
122
122
  } else {
123
123
  compiler.hooks.thisCompilation.tap(
@@ -126,18 +126,17 @@ class DependencyExtractionWebpackPlugin {
126
126
  compilation.hooks.processAssets.tap(
127
127
  {
128
128
  name: this.constructor.name,
129
- stage:
130
- compiler.webpack.Compilation
131
- .PROCESS_ASSETS_STAGE_ANALYSE,
129
+ stage: compiler.webpack.Compilation
130
+ .PROCESS_ASSETS_STAGE_ANALYSE,
132
131
  },
133
- () => this.addAssets( compilation, compiler )
132
+ () => this.addAssets( compilation )
134
133
  );
135
134
  }
136
135
  );
137
136
  }
138
137
  }
139
138
 
140
- addAssets( compilation, compiler ) {
139
+ addAssets( compilation ) {
141
140
  const {
142
141
  combineAssets,
143
142
  combinedOutputFile,
@@ -163,127 +162,102 @@ class DependencyExtractionWebpackPlugin {
163
162
 
164
163
  const combinedAssetsData = {};
165
164
 
166
- // Process each entry point independently.
167
- for ( const [
168
- entrypointName,
169
- entrypoint,
170
- ] of compilation.entrypoints.entries() ) {
171
- const entrypointExternalizedWpDeps = new Set();
165
+ // Accumulate all entrypoint chunks, some of them shared
166
+ const entrypointChunks = new Set();
167
+ for ( const entrypoint of compilation.entrypoints.values() ) {
168
+ for ( const chunk of entrypoint.chunks ) {
169
+ entrypointChunks.add( chunk );
170
+ }
171
+ }
172
+
173
+ // Process each entrypoint chunk independently
174
+ for ( const chunk of entrypointChunks ) {
175
+ const chunkFiles = Array.from( chunk.files );
176
+
177
+ const chunkJSFile = chunkFiles.find( ( f ) => /\.js$/i.test( f ) );
178
+ if ( ! chunkJSFile ) {
179
+ // There's no JS file in this chunk, no work for us. Typically a `style.css` from cache group.
180
+ continue;
181
+ }
182
+
183
+ const chunkDeps = new Set();
172
184
  if ( injectPolyfill ) {
173
- entrypointExternalizedWpDeps.add( 'wp-polyfill' );
185
+ chunkDeps.add( 'wp-polyfill' );
174
186
  }
175
187
 
176
188
  const processModule = ( { userRequest } ) => {
177
189
  if ( this.externalizedDeps.has( userRequest ) ) {
178
- const scriptDependency = this.mapRequestToDependency(
179
- userRequest
180
- );
181
- entrypointExternalizedWpDeps.add( scriptDependency );
190
+ chunkDeps.add( this.mapRequestToDependency( userRequest ) );
182
191
  }
183
192
  };
184
193
 
185
194
  // Search for externalized modules in all chunks.
186
- for ( const chunk of entrypoint.chunks ) {
187
- const modulesIterable = isWebpack4
188
- ? chunk.modulesIterable
189
- : compilation.chunkGraph.getChunkModules( chunk );
190
- for ( const chunkModule of modulesIterable ) {
191
- processModule( chunkModule );
192
- // Loop through submodules of ConcatenatedModule.
193
- if ( chunkModule.modules ) {
194
- for ( const concatModule of chunkModule.modules ) {
195
- processModule( concatModule );
196
- }
195
+ const modulesIterable = isWebpack4
196
+ ? chunk.modulesIterable
197
+ : compilation.chunkGraph.getChunkModules( chunk );
198
+ for ( const chunkModule of modulesIterable ) {
199
+ processModule( chunkModule );
200
+ // Loop through submodules of ConcatenatedModule.
201
+ if ( chunkModule.modules ) {
202
+ for ( const concatModule of chunkModule.modules ) {
203
+ processModule( concatModule );
197
204
  }
198
205
  }
199
206
  }
200
207
 
201
- const {
202
- hashFunction,
203
- hashDigest,
204
- hashDigestLength,
205
- } = compilation.outputOptions;
206
-
207
208
  // Go through the assets and hash the sources. We can't just use
208
- // `entrypointChunk.contentHash` because that's not updated when
209
+ // `chunk.contentHash` because that's not updated when
209
210
  // assets are minified. In practice the hash is updated by
210
211
  // `RealContentHashPlugin` after minification, but it only modifies
211
212
  // already-produced asset filenames and the updated hash is not
212
213
  // available to plugins.
213
- const hash = createHash( hashFunction );
214
- for ( const filename of entrypoint.getFiles().sort() ) {
215
- const asset = compilation.getAsset( filename );
216
- hash.update( asset.source.buffer() );
217
- }
218
- const version = hash
214
+ const { hashFunction, hashDigest, hashDigestLength } =
215
+ compilation.outputOptions;
216
+
217
+ const contentHash = chunkFiles
218
+ .sort()
219
+ .reduce( ( hash, filename ) => {
220
+ const asset = compilation.getAsset( filename );
221
+ return hash.update( asset.source.buffer() );
222
+ }, createHash( hashFunction ) )
219
223
  .digest( hashDigest )
220
224
  .slice( 0, hashDigestLength );
221
225
 
222
- const entrypointChunk = isWebpack4
223
- ? entrypoint.chunks.find( ( c ) => c.name === entrypointName )
224
- : entrypoint.getEntrypointChunk();
225
-
226
226
  const assetData = {
227
227
  // Get a sorted array so we can produce a stable, stringified representation.
228
- dependencies: Array.from( entrypointExternalizedWpDeps ).sort(),
229
- version,
228
+ dependencies: Array.from( chunkDeps ).sort(),
229
+ version: contentHash,
230
230
  };
231
231
 
232
- const assetString = this.stringify( assetData );
233
- const contentHash = createHash( hashFunction )
234
- .update( assetString )
235
- .digest( hashDigest )
236
- .slice( 0, hashDigestLength );
237
-
238
- // Determine a filename for the asset file.
239
- const [ filename, query ] = entrypointName.split( '?', 2 );
240
- const buildFilename = compilation.getPath(
241
- compiler.options.output.filename,
242
- {
243
- chunk: entrypointChunk,
244
- filename,
245
- query,
246
- basename: basename( filename ),
247
- contentHash,
248
- }
249
- );
250
-
251
232
  if ( combineAssets ) {
252
- combinedAssetsData[ buildFilename ] = assetData;
233
+ combinedAssetsData[ chunkJSFile ] = assetData;
253
234
  continue;
254
235
  }
255
236
 
256
237
  let assetFilename;
257
-
258
238
  if ( outputFilename ) {
259
239
  assetFilename = compilation.getPath( outputFilename, {
260
- chunk: entrypointChunk,
261
- filename,
262
- query,
263
- basename: basename( filename ),
240
+ chunk,
241
+ filename: chunkJSFile,
264
242
  contentHash,
265
243
  } );
266
244
  } else {
267
- assetFilename = buildFilename.replace(
268
- /\.js$/i,
269
- '.asset.' + ( outputFormat === 'php' ? 'php' : 'json' )
270
- );
245
+ const suffix =
246
+ '.asset.' + ( outputFormat === 'php' ? 'php' : 'json' );
247
+ assetFilename = compilation
248
+ .getPath( '[file]', { filename: chunkJSFile } )
249
+ .replace( /\.js$/i, suffix );
271
250
  }
272
251
 
273
252
  // Add source and file into compilation for webpack to output.
274
- compilation.assets[ assetFilename ] = new RawSource( assetString );
275
- entrypointChunk.files[ isWebpack4 ? 'push' : 'add' ](
276
- assetFilename
253
+ compilation.assets[ assetFilename ] = new RawSource(
254
+ this.stringify( assetData )
277
255
  );
256
+ chunk.files[ isWebpack4 ? 'push' : 'add' ]( assetFilename );
278
257
  }
279
258
 
280
259
  if ( combineAssets ) {
281
- // Assert the `string` type for output path.
282
- // The type indicates the option may be `undefined`.
283
- // However, at this point in compilation, webpack has filled the options in if
284
- // they were not provided.
285
- const outputFolder = /** @type {{path:string}} */ ( compiler.options
286
- .output ).path;
260
+ const outputFolder = compilation.outputOptions.path;
287
261
 
288
262
  const assetsFilePath = path.resolve(
289
263
  outputFolder,
@@ -303,11 +277,4 @@ class DependencyExtractionWebpackPlugin {
303
277
  }
304
278
  }
305
279
 
306
- function basename( name ) {
307
- if ( ! name.includes( '/' ) ) {
308
- return name;
309
- }
310
- return name.substr( name.lastIndexOf( '/' ) + 1 );
311
- }
312
-
313
280
  module.exports = DependencyExtractionWebpackPlugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/dependency-extraction-webpack-plugin",
3
- "version": "3.5.0",
3
+ "version": "4.0.1-next.d6164808d3.0",
4
4
  "description": "Extract WordPress script dependencies from webpack bundles.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -20,7 +20,7 @@
20
20
  "url": "https://github.com/WordPress/gutenberg/issues"
21
21
  },
22
22
  "engines": {
23
- "node": ">=12"
23
+ "node": ">=14"
24
24
  },
25
25
  "files": [
26
26
  "lib",
@@ -38,5 +38,5 @@
38
38
  "publishConfig": {
39
39
  "access": "public"
40
40
  },
41
- "gitHead": "198fa129cf1af8dc615918987ea6795cd40ab7df"
41
+ "gitHead": "ba8a396d2f418e53a6c4c50575582f3f3eb11ff7"
42
42
  }