Skip to content

Instantly share code, notes, and snippets.

@g3tr1ght
g3tr1ght / images-optimization-with-webpack.md
Created November 13, 2025 00:19
Images optimization with webpack

Images optimization with webpack

To prebuild images for various breakpoints using webpack and implement them with responsive design, you should use build-time tools to generate the different image sizes and then use HTML's srcset and picture elements for implementation, as using CSS media queries to change image sources is less efficient. [1, 2, 3, 4]

1. Prebuilding Images with Webpack

Use a webpack plugin and image processing library to automatically generate multiple image sizes during the build process. A common combination is the image-minimizer-webpack-plugin with sharp. [5, 6]

Webpack Configuration Example (webpack.config.js):

const ImageMinimizerPlugin = require('image-minimizer-webpack-plugin');
@g3tr1ght
g3tr1ght / esm-package.md
Created October 12, 2025 00:55 — forked from sindresorhus/esm-package.md
Pure ESM package

Pure ESM package

The package that linked you here is now pure ESM. It cannot be require()'d from CommonJS.

This means you have the following choices:

  1. Use ESM yourself. (preferred)
    Use import foo from 'foo' instead of const foo = require('foo') to import the package. You also need to put "type": "module" in your package.json and more. Follow the below guide.
  2. If the package is used in an async context, you could use await import(…) from CommonJS instead of require(…).
  3. Stay on the existing version of the package until you can move to ESM.
@g3tr1ght
g3tr1ght / difference.js
Created December 4, 2022 20:52 — forked from Yimiprod/difference.js
Deep diff between two object, using lodash
/**
* Deep diff between two object, using lodash
* @param {Object} object Object compared
* @param {Object} base Object to compare with
* @return {Object} Return a new object who represent the diff
*/
function difference(object, base) {
function changes(object, base) {
return _.transform(object, function(result, value, key) {
if (!_.isEqual(value, base[key])) {
@g3tr1ght
g3tr1ght / cancelPromise.md
Created November 6, 2021 05:06 — forked from pygy/cancelPromise.md
You can already cancel ES6 Promises

The gist: by having a Promise adopt the state of a forever pending one, you can suspend its then handlers chain.

Promise.pending = Promise.race.bind(Promise, [])

let cancel

new Promise(function(fulfill, reject) {
  cancel = function() {fulfill(Promise.pending())}
  setTimeout(fulfill, 1000, 5)
@g3tr1ght
g3tr1ght / protips.js
Created November 4, 2021 18:30 — forked from nolanlawson/protips.js
Promise protips - stuff I wish I had known when I started with Promises
// Promise.all is good for executing many promises at once
Promise.all([
promise1,
promise2
]);
// Promise.resolve is good for wrapping synchronous code
Promise.resolve().then(function () {
if (somethingIsNotRight()) {
throw new Error("I will be rejected asynchronously!");
@g3tr1ght
g3tr1ght / package.json
Created October 11, 2021 03:22 — forked from jayphelps/package.json
TypeScript output es2015, esm (ES Modules), CJS, UMD, UMD + Min + Gzip. Assumes you install typescript (tsc), rollup, uglifyjs either globally or included as devDependencies
{
"scripts": {
"build": "npm run build:es2015 && npm run build:esm && npm run build:cjs && npm run build:umd && npm run build:umd:min",
"build:es2015": "tsc --module es2015 --target es2015 --outDir dist/es2015",
"build:esm": "tsc --module es2015 --target es5 --outDir dist/esm",
"build:cjs": "tsc --module commonjs --target es5 --outDir dist/cjs",
"build:umd": "rollup dist/esm/index.js --format umd --name YourLibrary --sourceMap --output dist/umd/yourlibrary.js",
"build:umd:min": "cd dist/umd && uglifyjs --compress --mangle --source-map --screw-ie8 --comments --o yourlibrary.min.js -- yourlibrary.js && gzip yourlibrary.min.js -c > yourlibrary.min.js.gz",
}
}
@g3tr1ght
g3tr1ght / list-changed-files-against-master.js
Last active October 9, 2021 02:51
Git. List files changed since diverging from specific/remote/parent branch. Use it to pipe the result to jest or eslint.
const execFileSync = require('child_process').execFileSync;
const exec = (command, args) => {
console.log('> ' + [command].concat(args).join(' '));
const options = {
cwd: process.cwd(),
env: process.env,
stdio: 'pipe',
encoding: 'utf-8',
};
@g3tr1ght
g3tr1ght / webpack-dynamic-polyfills.js
Last active September 21, 2021 17:46
Dynamic whole-sale polyfilling example
// polyfills.js
require('whatwg-fetch');
require('intl');
require('url-polyfill');
require('core-js/web/dom-collections');
require('core-js/es6/promise');
require('core-js/es6/map');
require('core-js/es6/string');
require('core-js/es6/array');
require('core-js/es6/object');
@g3tr1ght
g3tr1ght / webpack-bundle-splitting.md
Last active September 21, 2021 17:01
Webpack Bundle splitting

The idea behind bundle splitting is pretty simple. If you have one giant file, and change one line of code, the user must download that entire file again. But if you’d split it into two files, then the user would only need to download the one that changed, and the browser would serve the other file from cache. It’s worth noting that since bundle splitting is all about caching, it makes no difference to first time visitors.

  • Webpack has some clever defaults which we override here, like a maximum of 3 files when splitting the output files, and a minimum file size of 30 KB (all smaller files would be joined together).
  • cacheGroups is where we define rules for how Webpack should group chunks into output files. We have one here called ‘vendor’ that will be used for any module being loaded from node_modules. Normally, you would just define a name for the output file as a string. But we're defining name as a function (which will be called for every parsed file) then returning the name of the package from the pa
@g3tr1ght
g3tr1ght / webpack-esmodules-lib.js
Created September 21, 2021 15:50
Sample webpack config which produces ES6 modules. See https://webpack.js.org/configuration/output/#outputlibrarytarget for more details
{
entry: './src/module.js',
output: {
filename: 'module.js',
path: path.resolve(__dirname, 'dist'),
libraryTarget: 'module'
},
experiments: {
outputModule: true,
},