Skip to content

Commit

Permalink
refactor(build): split chunks to simplify tree shaking
Browse files Browse the repository at this point in the history
  • Loading branch information
gcornut committed Jan 29, 2025
1 parent 98781b9 commit ac8e169
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions packages/lumx-react/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import path from 'path';
import glob from 'glob';

import commonjs from '@rollup/plugin-commonjs';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import analyze from 'rollup-plugin-analyzer';
Expand All @@ -16,18 +18,34 @@ const importUrl = new URL(import.meta.url);
const __dirname = path.dirname(importUrl.pathname);

const ROOT_PATH = path.resolve(__dirname, '..', '..');
const SRC_PATH = path.resolve(__dirname, 'src');
const DIST_PATH = path.resolve(__dirname, pkg.publishConfig.directory);
export const extensions = ['.js', '.jsx', '.ts', '.tsx'];

const INTERNAL = '_internal';

/**
* List of entry modules that will get compiled and exported to NPM.
* All other modules are considered as internal and won't be exposed.
*/
const input = Object.fromEntries([
// Externals
['index', 'src/index.ts'], // => @lumx/react
['utils/index', 'src/utils/index.ts'], // => @lumx/react/utils
// Internals (split to help bundler tree-shake)
...glob.sync('src/components/*/index.ts').map((componentPath) => {
const relativePath = path.relative(SRC_PATH, componentPath);
return [`${INTERNAL}/${relativePath}`, componentPath];
}),
]);

const formatFileNames = (ext) => (info) => {
if (info.name.startsWith(INTERNAL)) {
return `[name]-[hash].${ext}`;
}
return `[name].${ext}`;
};

// Bundle JS code
const bundleJS = {
input,
Expand All @@ -37,7 +55,8 @@ const bundleJS = {
hoistTransitiveImports: false,
dir: DIST_PATH,
// Unnamed chunk moved to `_internal` folder
chunkFileNames: '_internal/[name].js',
chunkFileNames: `_internal/[hash].js`,
entryFileNames: formatFileNames('js'),
},
plugins: [
/** Clean dist dir */
Expand Down Expand Up @@ -86,7 +105,8 @@ const bundleType = {
format: 'esm',
dir: DIST_PATH,
// Unnamed chunk moved to `_internal` folder
chunkFileNames: '_internal/[name].d.ts',
chunkFileNames: `_internal/[hash].d.ts`,
entryFileNames: formatFileNames('d.ts'),
},
plugins: [
/** Externalize peer dependencies. */
Expand Down

0 comments on commit ac8e169

Please sign in to comment.