-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Dynamic import glob support #2097
Comments
I agree that some kind of globbing would be certainly very powerful. But there are some basic questions that I struggle to answer:
At first glance, this looks to me like a feature that wants to add some convenience to something that is already and very reliably possible today. I.e. I could use the "named chunks" feature to create a number of possible chunks relative to my importing chunk and then dynamically import those. Maybe some full examples would be helpful to get a better idea of how such a feature might work.
This, on the other hand, sounds very much doable and useful. |
@lukastaegert these are some great points. So if you have What do you think of something like that? Also auto-collecting globs by default is also a big question. An alternative would be ensuring users define the entry point paths explicitly: In the object form we could possibly do something like: input: {
'main': 'src/index',
'dynamic-*': src/dynamics/*.js',
} Where we support only a single non-deep wildcard that can act as its own substitution. This stuff gets complex fast... but that's why I thought it worth putting out the wild ideas now - not exactly close to implementation here don't worry. |
So we basically add a "mini-runtime" just for those files? I guess that would be acceptable. We would need to put some thought into how a file is handled that is imported both globbed-dynamically as well as synchronously by its explicit name. For a first attempt, I would suggest to ALWAYS use the dynamic map to have the general case covered and then see what the most useful and easy optimizations are.
As people keep asking about this, I think this is something we should definitely do! Ideally, we would also support |
It's just a dynamic map for unknown input values - so far as we could predict inputs into the dynamic import we can make the rewrite more well-defined. Definitely starting with the map we could extend the optimization cases from there.
I'd be against ** globs if we can - if users have arbitrary depth to their entry points, they can still list those depths explicitly. Note patterns like
We already support these replacements in the |
Maybe we should also handle import( |
I made a plugin for this: https://github.com/LarsDenBakker/rollup-plugin-dynamic-import-variables |
@LarsDenBakker any interest in having that part of this repo? totally cool if not, no worries! |
Yes that would be great! |
+1 from me as well, having something like this has been missing for quite some time! |
@LarsDenBakker wonderful. We don't have a formal process for this in place (because it doesn't happen often enough to have one). I've sent you an invite to the plugin maintainers team and created the I'd humbly suggest shortening the name a bit to |
a more generic solution are compile-time expressions in my case, i also need the filenames of the globbed imports // codegen-files.js
import glob from "tiny-glob"
export default async function () {
const prefix = "../src";
const files = await glob(prefix + "/**/*.{ts,js}", {
cwd: __dirname,
filesOnly: true,
});
const object = "filesystemFiles";
const str = JSON.stringify;
const importCode = files.map(path => {
// remove prefix + extension
const name = path.slice(prefix.length).replace(/\.[a-z0-9]+$/i, "");
return `${object}[${str(name)}] = () => import(${str(path)});`;
}).join("\n");
return {
//data: files,
code: importCode,
};
} // index.js
/** @type {Record<string, () => any>} */
const filesystemFiles = {};
import.meta.compileTime("./codegen-files.js");
for (const [file, importFile] in Object.entries(filesystemFiles)) {
if (needFile(file)) {
const fileModule = await importFile();
useFile(fileModule);
}
} edit: vite != rollup ... |
Out of my view globing in any parts like input would hurt essential at maximum we can go for generateBuild hook for assets globs and buildStart hook to emit the glob results we should never no where in the core try to put globs in that breaks ecmascript. and yes vite is rollup the first 3 steps of a single file and maybe even multiple are the same. @milahu can be seen as offtopic as he used import meta helpers no one knows where that come from maybe that's something vite added. |
Cases like
import(`asdf/${}`)
,import('base/' + var)
etc should all be turned into globbed dynamic import entry points for support in chunking.We could possibly look at adding glob support to code splitting inputs in general here too, although we may have a problem in that
input: ['asdf/*']
can work, while there isn't necessarily a way of doing that in an object -input: { '_': ['asdf/*'], named: 'entry' }
...?The text was updated successfully, but these errors were encountered: