Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom webpack loader to remove unused mimetypes from mime-db #5148

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions _scripts/mime-db-shrinking-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* electron-context-menu only needs mime-db for its save as feature.
* As we only activate save image and save as image features, we can remove all other mimetypes,
* as they will never get used.
* Which results in quite a significant reduction in file size.
* @param {string} source
*/
module.exports = function (source) {
const original = JSON.parse(source)

const reduced = {}

for (const mimeType of Object.keys(original)) {
if (mimeType.startsWith('image/') && original[mimeType].extensions &&
(!mimeType.startsWith('image/x-') || mimeType === 'image/x-icon' || mimeType === 'image/x-ms-bmp') &&
(!mimeType.startsWith('image/vnd.') || mimeType === 'image/vnd.microsoft.icon')) {

// Only the extensions field is needed, see: https://github.com/kevva/ext-list/blob/v2.2.2/index.js
reduced[mimeType] = {
extensions: original[mimeType].extensions
}
}
Comment on lines +14 to +22
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just make it easier to read
Coz the logic seems to be

  1. Add all image/ MIME types not matching specific prefixes with extensions
  2. Add specific MIME types
Suggested change
if (mimeType.startsWith('image/') && original[mimeType].extensions &&
(!mimeType.startsWith('image/x-') || mimeType === 'image/x-icon' || mimeType === 'image/x-ms-bmp') &&
(!mimeType.startsWith('image/vnd.') || mimeType === 'image/vnd.microsoft.icon')) {
// Only the extensions field is needed, see: https://github.com/kevva/ext-list/blob/v2.2.2/index.js
reduced[mimeType] = {
extensions: original[mimeType].extensions
}
}
if (!mimeType.startsWith('image/')) { continue }
if (original[mimeType].extensions == null) { continue }
let shouldBeAdded = false
if (!mimeType.startsWith('image/x-') && !mimeType.startsWith('image/vnd.')) {
shouldBeAdded = true
} else {
shouldBeAdded = mimeType === 'image/x-icon' ||
mimeType === 'image/x-ms-bmp' ||
mimeType === 'image/vnd.microsoft.icon'
}
if (!shouldBeAdded) { continue }
// Only the extensions field is needed, see: https://github.com/kevva/ext-list/blob/v2.2.2/index.js
reduced[mimeType] = {
extensions: original[mimeType].extensions
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current code is easier to read imo

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @efb4f5ff-1298-471a-8973-3d47447115dc I think the current code is more readable, also your code changes the logic, resulting in a different output.

I prefer the or/|| operator over writing lots of single line ifs that have the same outcome, as to me it's a lot more readable, especially when the variable names are short.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @efb4f5ff-1298-471a-8973-3d47447115dc I think the current code is more readable, also your code changes the logic, resulting in a different output.

I prefer the or/|| operator over writing lots of single line ifs that have the same outcome, as to me it's a lot more readable, especially when the variable names are short.

Also we really don't need to check for null here, as this code runs on a json file with a known structure so the only options are non-existent/undefined or an array, so checking for loose equality with null is rather confusing. Also the truthy check already covers both undefined and null.

}

return JSON.stringify(reduced)
}
4 changes: 4 additions & 0 deletions _scripts/webpack.main.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ const config = {
use: 'babel-loader',
exclude: /node_modules/,
},
{
resource: path.resolve(__dirname, '../node_modules/mime-db/db.json'),
use: path.join(__dirname, 'mime-db-shrinking-loader.js')
}
],
},
// webpack defaults to only optimising the production builds, so having this here is fine
Expand Down