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

preserveModules, but one bundle per node_module #5626

Open
fregante opened this issue Aug 25, 2024 · 1 comment
Open

preserveModules, but one bundle per node_module #5626

fregante opened this issue Aug 25, 2024 · 1 comment

Comments

@fregante
Copy link

Feature Use Case

preserveModule appears to be all or nothing. This means that each and every file, including multiple files under node_modules are bundled separately, often appearing as several [package]/index.js files.

I really liked the idea behind Snowpack, bundling each package in node_modules as its own bundle. I'm not asking for that kind of optimization here, but it's just to give an idea of the expected output.

Feature Proposal

I'd love for a way to pick what's preserved and what not. The kind of granularity is up for debate. I think the most flexible API would look like groupBy, where the callback returns an ID and rollup just uses the ID to create a chunk.

In my scenario, the callback would be:

// node_modules/[name].js
preserveModules: chunkInfo => chunkInfo.name.includes('node_modules')
	? '/node_modules/'   chunkInfo.name.split('/')[1] // The package name
	: chunkInfo.name

or:

// single vendor.js
preserveModules: chunkInfo => chunkInfo.name.includes('node_modules') 
	? 'vendor'
	: chunkInfo.name
@fregante
Copy link
Author

FWIW, I cleared up the current output via a custom entryFileNames callback, but packages with multiple files still create multiple files:

const noise = new Set(['index', 'dist', 'src', 'source', 'distribution', 'node_modules', 'main', 'esm', 'cjs', 'build', 'built']);
// ...
	entryFileNames(chunkInfo) {
		if (chunkInfo.name.includes('node_modules')) {
			const cleanName = chunkInfo.name
				.split('/')
				.filter(part => !noise.has(part))
				.join('-');
			return `node_modules/${cleanName}.js`;
		}
	
		return '[name].js'
	},

Before

node_modules
 / react
   / index.js
 / react-dom
   / index.js
   / utils.js

After

node_modules
 / react.js
 / react-dom.js
 / react-dom-utils.js

I found it on a similar (but very different) issue: #3684 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant