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
Why does the preserveModules
option generate node_modules
folder
#3684
Comments
The folder is created because If you want to change names, you might have some luck using the |
@lukastaegert Thanks for the reply. I just tried
In the error's stack trace it seems that the
It's a bit strange because the docs just says that this option supports both string and function 🤔 Am I doing something wrong?! |
Did the function return a string? 😉 |
And are you on latest Rollup? |
Yes it's returning some random string 😀 The important problem is that I put some logs inside the function and it's not called at all. The versions are: "@rollup/plugin-alias": "^3.1.1",
"@rollup/plugin-babel": "^5.0.4",
"@rollup/plugin-commonjs": "^13.0.0",
"@rollup/plugin-node-resolve": "^8.1.0",
"@rollup/plugin-replace": "^2.3.3",
"rollup": "^2.18.1",
"rollup-plugin-terser": "^6.1.0" |
As a first step I would update Rollup as that feature is a little newer than your minimum Rollup version: https://github.com/rollup/rollup/releases/tag/v2.20.0 |
The update fixed the function problem 👍 But now when I try to rebuild the full path (to replace the entryFileNames: ({ facadeModuleId }) => {
if (!facadeModuleId.includes('node_modules')) {
return '[name].js';
}
return facadeModuleId.replace('node_modules', 'dependencies');
}, I get this error:
It seems that I can only nest the result in some sub_directories but can't change the path that the file already has. For example if I write it like this: entryFileNames: ({ facadeModuleId }) => {
if (!facadeModuleId.includes('node_modules')) {
return '[name].js';
}
return 'dependencies/[name].js';
}, then it will nest all of the |
Ok, this should probably be improved. I would have thought that |
Ah, that will still not solve your problem, my bad |
Exactly, the problem is that whatever we return from this function, it would only be appended to the end of the path of the file. |
I fear we cannot fix this without a breaking change at the moment |
Well, currently the isPlainPathFragment function returns So instead of throwing, if we just add this functionality where these types of names would change the path as well, would it still be a breaking change? Also, another way is to add a new option besides |
I'm encountering this issue, although it's slightly different in that I'd also like to have control over the modules that get output from I think the underlying issue here is with I can try reproducing soon, but right now my work-around has been to use rollup-plugin-rename to restructure the output, and also to handle the relative imports that otherwise would have problems due to this restructuring. |
I was having the same issue and since I didn't know if it was a rollup bug or not I just created a plugin to fix this called rollup-plugin-rename-node-modules. I'm using it in my project and now it works. |
I'm encountered same issue.
To workaround it, I combined davidroeca's import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import babel from 'rollup-plugin-babel';
import rename from 'rollup-plugin-rename';
export default {
input: 'src/index.js',
output: {
dir: 'dist/',
format: 'cjs',
preserveModules: true,
},
plugins: [
commonjs(),
resolve({
resolveOnly: ['lodash-es'],
// modulesOnly: true,
}),
babel({
runtimeHelpers: true,
}),
rename({
include: ['**/*.js'],
map: (name) => name
.replace('src/', '')
.replace('node_modules/', 'external/')
.replace('../../external', '../external'),
})
],
}; And the output will look like this:
|
@shrpne Thank you so much, this was causing me so much pain trying to solve!! It's really worrying that this is the fix, assuming this is just a temporary workaround as this must be a bug that needs fixing? For me the two issues are that it creates an src folder and after fixing that, it's unable to properly find the node_modules folder |
Any progress on this? |
I'm experiencing this issue also. Adding this to my config has worked pretty well for me in the meantime: const pkg = require('./package.json');
const externalPackages = [
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {}),
];
// Creating regexes of the packages to make sure subpaths of the
// packages are also treated as external
const regexesOfPackages = externalPackages
.map(packageName => new RegExp(`^${packageName}(\/.*)?`));
export default {
external: regexesOfPackages,
// ...
}; |
I am using the following code to solve my issue
|
Fix at #4565 |
This issue has been resolved via #4565 as part of rollup@3.0.0-3. Note that this is a pre-release, so to test it, you need to install Rollup via |
This issue has been resolved via #4565 as part of rollup@3.0.0-4. Note that this is a pre-release, so to test it, you need to install Rollup via |
This issue has been resolved via #4565 as part of rollup@3.0.0-5. Note that this is a pre-release, so to test it, you need to install Rollup via |
This issue has been resolved via #4565 as part of rollup@3.0.0-6. Note that this is a pre-release, so to test it, you need to install Rollup via |
This issue has been resolved via #4565 as part of rollup@3.0.0-7. Note that this is a pre-release, so to test it, you need to install Rollup via |
This issue has been resolved via #4565 as part of rollup@3.0.0-8. Note that this is a pre-release, so to test it, you need to install Rollup via |
This issue has been resolved via #4565 as part of rollup@3.0.0. You can test it via |
@lukastaegert Hi, I tried updating to rollup v3, but we are unclear what exactly was resolved? From my testing it still outputs a |
Yes, but you can change the path now by supplying a function for entryFileNames. Citing the original issue
is now available. Depending on your actual problem, output.preserveModulesRoot might also be useful. |
Gotcha, |
for those who are still struggling with node_modules issue, this output object should fix it. thanks @lukastaegert for fixing it ✌🏻. {
dir: 'lib',
format: 'esm',
sourcemap: true,
preserveModules: true,
preserveModulesRoot: 'src',
entryFileNames: (chunkInfo) => {
if (chunkInfo.name.includes('node_modules')) {
return chunkInfo.name.replace('node_modules', 'external') + '.js';
}
return '[name].js';
}
} |
|
Documentation Is:
As described here in the docs, I'm using the
plugin-node-resolve
plugin to include thedependencies
of my library in the final bundle.I'm also using the
external
option as described here to exclude thepeerDependencies
. So the final rollup config looks something like this:The problem is that after the build, the
dependencies
of the package are generated in this location:dist/node_modules/PACKAGE_NAME
. And their import statements in other files are something like this:import SOMETHING from '../../../node_modules/PACKAGE_NAME/index.js';
And on the other hand, when I build the package on the consumer project (using
prepare
script), this nestednode_modules
directory doesn't exist (possibly becausenpm
andyarn
flatten thenode_modules
structure).So I was wondering is there any option to fix this problem? e.g. a way to change the name of these generated
node_modules
folders and their import statements to something else...The text was updated successfully, but these errors were encountered: