Skip to content
Dashboard

How we optimized package imports in Next.js

Software Engineer

40% faster cold boots and 28% faster builds

Link to headingWhat is a barrel file?

index.js
export { default as module1 } from './module1';
export { default as module2 } from './module2';
export { default as module3 } from './module3';

import module1 from './utils/module1';
import module2 from './utils/module2';
import module3 from './utils/module3';

import { module1, module2, module3 } from './utils';

Link to headingWhat's the problem with barrel files?

Link to headingCan’t we tree-shake it?

Link to headingOur first attempt: modularizeImports

my-lib/index.js
export { default as module1 } from './module1';
export { default as module2 } from './module2';
export { default as module3 } from './module3';

Link to headingNew solution: optimizePackageImports

next.config.js
module.exports = {
experimental: {
optimizePackageImports: ["my-lib"]
}
}

Link to headingMeasuring performance improvements

Link to headingLocal development

Link to headingProduction builds

Link to headingFaster cold boots

Link to headingRecursive Barrel Files

Link to headingConclusion