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

Feature request: Read whole module if there is no default export #308

Closed
tianyingchun opened this issue Jun 5, 2023 · 8 comments
Closed

Comments

@tianyingchun
Copy link

#283 (comment)

const loadJs: LoaderAsync = async function loadJs(filepath) {
  try {
    const { href } = pathToFileURL(filepath);
    return (await import(href)).default;
  } catch (error) {
    return loadJsSync(filepath, null);
  }
};

return (await import(href)).default; always return undefined for project if we have an named exports at xxx.config.js

consider below example
valid.config.js

import { defineConfig } from 'vite';

// it is esm module causeof type:module
export const test = defineConfig({
cake: 'a lie',
});

it will aways return me undefined.

maybe it should be

const { href } = pathToFileURL(filepath);
const moduleData =  await import(href);
return moduleData.default|| moduleData;
@tianyingchun
Copy link
Author

tianyingchun commented Jun 5, 2023

@d-fischer
Copy link
Member

I don't see why it should load a non-default export from the file. Do you have any actual use case for this? I'm pretty sure that vite does not use cosmiconfig.

@tianyingchun
Copy link
Author

for convenient and fast, in some times we should defined xxxx.mjs

it use export const xx =xxx, instead use
export default { xx:``` }
cause of in type:module, resoltion:nodenext it not recommend use export default xxxx

@d-fischer
Copy link
Member

That's a feature request then, not a bug.

@d-fischer d-fischer changed the title Has a small bug on ESM support Feature request: Read whole module if there is no default export Jun 5, 2023
@tianyingchun
Copy link
Author

when this feature will be plan? :)

@prisis
Copy link

prisis commented Jun 11, 2023

Hey @tianyingchun :), every open source project accepts a PR with the needed feature/bug ;)

@tianyingchun
Copy link
Author

:) ok, i make time to make PR 💯

@jrandolf
Copy link
Collaborator

jrandolf commented Sep 2, 2023

We use default exports because it semantically shows there is one configuration. Named exports has the implication of many which is very much against the simplicity we strive for in this package. Unless there is a well-defined use-case with production code available, we will probably not support this.

@jrandolf jrandolf closed this as not planned Won't fix, can't repro, duplicate, stale Sep 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants