@wordpress/dependency-extraction-webpack-plugin 3.6.0 → 4.0.1-next.957ca95e4c.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 (2022-08-24)
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(
@@ -129,14 +129,14 @@ class DependencyExtractionWebpackPlugin {
129
129
  stage: compiler.webpack.Compilation
130
130
  .PROCESS_ASSETS_STAGE_ANALYSE,
131
131
  },
132
- () => this.addAssets( compilation, compiler )
132
+ () => this.addAssets( compilation )
133
133
  );
134
134
  }
135
135
  );
136
136
  }
137
137
  }
138
138
 
139
- addAssets( compilation, compiler ) {
139
+ addAssets( compilation ) {
140
140
  const {
141
141
  combineAssets,
142
142
  combinedOutputFile,
@@ -162,124 +162,102 @@ class DependencyExtractionWebpackPlugin {
162
162
 
163
163
  const combinedAssetsData = {};
164
164
 
165
- // Process each entry point independently.
166
- for ( const [
167
- entrypointName,
168
- entrypoint,
169
- ] of compilation.entrypoints.entries() ) {
170
- 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();
171
184
  if ( injectPolyfill ) {
172
- entrypointExternalizedWpDeps.add( 'wp-polyfill' );
185
+ chunkDeps.add( 'wp-polyfill' );
173
186
  }
174
187
 
175
188
  const processModule = ( { userRequest } ) => {
176
189
  if ( this.externalizedDeps.has( userRequest ) ) {
177
- const scriptDependency =
178
- this.mapRequestToDependency( userRequest );
179
- entrypointExternalizedWpDeps.add( scriptDependency );
190
+ chunkDeps.add( this.mapRequestToDependency( userRequest ) );
180
191
  }
181
192
  };
182
193
 
183
194
  // Search for externalized modules in all chunks.
184
- for ( const chunk of entrypoint.chunks ) {
185
- const modulesIterable = isWebpack4
186
- ? chunk.modulesIterable
187
- : compilation.chunkGraph.getChunkModules( chunk );
188
- for ( const chunkModule of modulesIterable ) {
189
- processModule( chunkModule );
190
- // Loop through submodules of ConcatenatedModule.
191
- if ( chunkModule.modules ) {
192
- for ( const concatModule of chunkModule.modules ) {
193
- processModule( concatModule );
194
- }
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 );
195
204
  }
196
205
  }
197
206
  }
198
207
 
199
- const { hashFunction, hashDigest, hashDigestLength } =
200
- compilation.outputOptions;
201
-
202
208
  // Go through the assets and hash the sources. We can't just use
203
- // `entrypointChunk.contentHash` because that's not updated when
209
+ // `chunk.contentHash` because that's not updated when
204
210
  // assets are minified. In practice the hash is updated by
205
211
  // `RealContentHashPlugin` after minification, but it only modifies
206
212
  // already-produced asset filenames and the updated hash is not
207
213
  // available to plugins.
208
- const hash = createHash( hashFunction );
209
- for ( const filename of entrypoint.getFiles().sort() ) {
210
- const asset = compilation.getAsset( filename );
211
- hash.update( asset.source.buffer() );
212
- }
213
- 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 ) )
214
223
  .digest( hashDigest )
215
224
  .slice( 0, hashDigestLength );
216
225
 
217
- const entrypointChunk = isWebpack4
218
- ? entrypoint.chunks.find( ( c ) => c.name === entrypointName )
219
- : entrypoint.getEntrypointChunk();
220
-
221
226
  const assetData = {
222
227
  // Get a sorted array so we can produce a stable, stringified representation.
223
- dependencies: Array.from( entrypointExternalizedWpDeps ).sort(),
224
- version,
228
+ dependencies: Array.from( chunkDeps ).sort(),
229
+ version: contentHash,
225
230
  };
226
231
 
227
- const assetString = this.stringify( assetData );
228
- const contentHash = createHash( hashFunction )
229
- .update( assetString )
230
- .digest( hashDigest )
231
- .slice( 0, hashDigestLength );
232
-
233
- // Determine a filename for the asset file.
234
- const [ filename, query ] = entrypointName.split( '?', 2 );
235
- const buildFilename = compilation.getPath(
236
- compiler.options.output.filename,
237
- {
238
- chunk: entrypointChunk,
239
- filename,
240
- query,
241
- basename: basename( filename ),
242
- contentHash,
243
- }
244
- );
245
-
246
232
  if ( combineAssets ) {
247
- combinedAssetsData[ buildFilename ] = assetData;
233
+ combinedAssetsData[ chunkJSFile ] = assetData;
248
234
  continue;
249
235
  }
250
236
 
251
237
  let assetFilename;
252
-
253
238
  if ( outputFilename ) {
254
239
  assetFilename = compilation.getPath( outputFilename, {
255
- chunk: entrypointChunk,
256
- filename,
257
- query,
258
- basename: basename( filename ),
240
+ chunk,
241
+ filename: chunkJSFile,
259
242
  contentHash,
260
243
  } );
261
244
  } else {
262
- assetFilename = buildFilename.replace(
263
- /\.js$/i,
264
- '.asset.' + ( outputFormat === 'php' ? 'php' : 'json' )
265
- );
245
+ const suffix =
246
+ '.asset.' + ( outputFormat === 'php' ? 'php' : 'json' );
247
+ assetFilename = compilation
248
+ .getPath( '[file]', { filename: chunkJSFile } )
249
+ .replace( /\.js$/i, suffix );
266
250
  }
267
251
 
268
252
  // Add source and file into compilation for webpack to output.
269
- compilation.assets[ assetFilename ] = new RawSource( assetString );
270
- entrypointChunk.files[ isWebpack4 ? 'push' : 'add' ](
271
- assetFilename
253
+ compilation.assets[ assetFilename ] = new RawSource(
254
+ this.stringify( assetData )
272
255
  );
256
+ chunk.files[ isWebpack4 ? 'push' : 'add' ]( assetFilename );
273
257
  }
274
258
 
275
259
  if ( combineAssets ) {
276
- // Assert the `string` type for output path.
277
- // The type indicates the option may be `undefined`.
278
- // However, at this point in compilation, webpack has filled the options in if
279
- // they were not provided.
280
- const outputFolder = /** @type {{path:string}} */ (
281
- compiler.options.output
282
- ).path;
260
+ const outputFolder = compilation.outputOptions.path;
283
261
 
284
262
  const assetsFilePath = path.resolve(
285
263
  outputFolder,
@@ -299,11 +277,4 @@ class DependencyExtractionWebpackPlugin {
299
277
  }
300
278
  }
301
279
 
302
- function basename( name ) {
303
- if ( ! name.includes( '/' ) ) {
304
- return name;
305
- }
306
- return name.substr( name.lastIndexOf( '/' ) + 1 );
307
- }
308
-
309
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.6.0",
3
+ "version": "4.0.1-next.957ca95e4c.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": "48d5f37dfb52d2e77c8eeb662f9874cf141b8c6b"
41
+ "gitHead": "272a74bbbaab10ee24424eafe9578e705fbfbbb4"
42
42
  }