Skip to content
Dashboard

How we optimized package imports in Next.js

Software Engineer

40% faster cold boots and 28% faster builds

Copy 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';

Copy link to headingWhat's the problem with barrel files?

Copy link to headingCan’t we tree-shake it?

Copy 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';

Copy link to headingNew solution: optimizePackageImports

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

Copy link to headingMeasuring performance improvements

Copy link to headingLocal development

Copy link to headingProduction builds

Copy link to headingFaster cold boots

Copy link to headingRecursive Barrel Files

Copy link to headingConclusion

Ready to deploy?