Skip to content

Commit

Permalink
webpack-config: Set output.uniqueName (#41315)
Browse files Browse the repository at this point in the history
* webpack-config: Set `output.uniqueName`

When we removed a bunch of otherwise-useless "name" properties from
`package.json` files, it turns out that caused Webpack to start using
the empty string for its `output.uniqueName`, which may result in
collisions if multiple bundles on a page do the same thing.

To avoid this, read the "name" from composer.json if package.json lacks
one.

Also, since we're now setting `output.uniqueName`, various webpack
configs using `output.library.name` should also set `output.uniqueName`
to maintain the previous behavior.

* Remove for entry.x.library.name

Hopefully Webpack does the right thing on its own for those.
  • Loading branch information
anomiex authored Jan 24, 2025
1 parent 8c33fac commit 18b4f12
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Set webpack `output.uniqueName` to match `output.library.name`, to account for a change in js-packages/webpack-config.


1 change: 1 addition & 0 deletions projects/js-packages/boost-score-api/webpack.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = {
...jetpackWebpackConfig.output,
path: path.resolve( __dirname, 'build' ),
filename: 'index.js',
uniqueName: 'BoostScoreApiLibrary',
library: {
name: 'BoostScoreApiLibrary',
type: 'umd',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Set webpack `output.uniqueName` to match `output.library.name`, to account for a change in js-packages/webpack-config.


Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
path: path.join( __dirname, '../build' ),
filename: 'bundle.js',
library: 'CriticalCSSGenerator',
uniqueName: 'CriticalCSSGenerator',
},
resolve: {
...jetpackWebpackConfig.resolve,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Set webpack `output.uniqueName` to match `output.library.name`, to account for a change in js-packages/webpack-config.


Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = {
...jetpackWebpackConfig.output,
path: path.resolve( __dirname, 'build' ),
filename: 'index.js',
uniqueName: 'ReactDataSyncClient',
library: {
name: 'ReactDataSyncClient',
type: 'umd',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Set webpack `output.uniqueName` to match `output.library.name`, to account for a change in js-packages/webpack-config.


Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = {
...jetpackWebpackConfig.output,
path: path.resolve( __dirname, 'build' ),
filename: 'index.js',
uniqueName: 'SvelteDataSyncClient',
library: {
name: 'SvelteDataSyncClient',
type: 'umd',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fixed
Comment: Set webpack `output.uniqueName` to match `output.library.name`, to account for a change in js-packages/webpack-config.


1 change: 1 addition & 0 deletions projects/js-packages/videopress-core/webpack.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module.exports = {
...jetpackWebpackConfig.output,
path: path.resolve( __dirname, 'build' ),
filename: 'index.js',
uniqueName: 'VideoPressCore',
library: {
name: 'VideoPressCore',
type: 'umd',
Expand Down
3 changes: 3 additions & 0 deletions projects/js-packages/webpack-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ This is an object suited for spreading some default values into Webpack's `outpu

- `filename`: `[name].js`.
- `chunkFilename`: `[name].js?minify=false&ver=[contenthash]`. The content hash serves as a cache buster, while `minify=false` avoids a broken minifier in the WordPress.com environment.
- `uniqueName`: If `package.json` has a name, that. Otherwise if `composer.json` has a name, that.

Note if you're setting `output.library.name`, you may want to also set `output.uniqueName` to the same string to match Webpack's default behavior.

#### `optimization`

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Set `output.uniqueName` by default. Note this may change output for things setting `output.library.name`.
30 changes: 30 additions & 0 deletions projects/js-packages/webpack-config/src/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,35 @@ const i18nFilterFunction = file => {
return i < 14 || file.startsWith( '@automattic/', i );
};

const getUniqueName = () => {
let dir = process.cwd(),
olddir;
do {
const file = path.join( dir, 'package.json' );
if ( fs.existsSync( file ) ) {
const cfg = JSON.parse( fs.readFileSync( file, { encoding: 'utf8' } ) );
if ( cfg.name ) {
return cfg.name;
}
}

const file2 = path.join( dir, 'composer.json' );
if ( fs.existsSync( file2 ) ) {
const cfg = JSON.parse( fs.readFileSync( file2, { encoding: 'utf8' } ) );
if ( cfg.name ) {
// Prepend an '@' to make it look more like a JS package name.
return '@' + cfg.name;
}
break;
}

olddir = dir;
dir = path.dirname( dir );
} while ( dir !== olddir );

throw new Error( 'Cannot determine unique name' );
};

/****** Options ******/

// See README.md for explanations of all these settings.
Expand All @@ -70,6 +99,7 @@ const devtool = isProduction ? false : 'source-map';
const output = {
filename: '[name].js',
chunkFilename: '[name].js?minify=false&ver=[contenthash]',
uniqueName: getUniqueName(),
};
const optimization = {
minimize: isProduction,
Expand Down

0 comments on commit 18b4f12

Please sign in to comment.