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

cannot use kebab-case keys from .json file default exports #217

Closed
alexzhang1030 opened this issue Feb 15, 2023 · 2 comments
Closed

cannot use kebab-case keys from .json file default exports #217

alexzhang1030 opened this issue Feb 15, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@alexzhang1030
Copy link

alexzhang1030 commented Feb 15, 2023

Environment

stackblitz
unbuild: ^1.1.1
node: 16.14.2

Reproduction

https://stackblitz.com/edit/node-z61fts?file=index.js

Describe the bug

When import a kabab-case field from json file.

The result after the build is incorrect.

Please see reproduction and execute npm run build, and see dist/index.js.mjs.

I think this is an upstream error from rollup. But I cannot reproduce it by using rollup.

Additional context

No response

Logs

No response

@alexzhang1030 alexzhang1030 changed the title Build error when import kebab-case field from json file. Build result incorrect when import kebab-case field from json file. Feb 15, 2023
@zoeyzhao19
Copy link
Contributor

@alexzhang1030 , it is due to @rollup/plugin-json try to make identitier legal through

const makeLegalIdentifier = function makeLegalIdentifier(str) {
    let identifier = str
        .replace(/-(\w)/g, (_, letter) => letter.toUpperCase())
        .replace(/[^$_a-zA-Z0-9]/g, '_');
    if (/\d/.test(identifier[0]) || forbiddenIdentifiers.has(identifier)) {
        identifier = `_${identifier}`;
    }
    return identifier || '_';
};

So it replace something-idk to somethingIdk, that's why the named export code in the bundle file doesn't contain the kebab-case field. Related to here

@alexzhang1030
Copy link
Author

@alexzhang1030 , it is due to @rollup/plugin-json try to make identitier legal through

const makeLegalIdentifier = function makeLegalIdentifier(str) {
    let identifier = str
        .replace(/-(\w)/g, (_, letter) => letter.toUpperCase())
        .replace(/[^$_a-zA-Z0-9]/g, '_');
    if (/\d/.test(identifier[0]) || forbiddenIdentifiers.has(identifier)) {
        identifier = `_${identifier}`;
    }
    return identifier || '_';
};

So it replace something-idk to somethingIdk, that's why the named export code in the bundle file doesn't contain the kebab-case field. Related to here

I think so, let's track this issue.

@pi0 pi0 added the bug Something isn't working label Jul 18, 2023
@pi0 pi0 changed the title Build result incorrect when import kebab-case field from json file. cannot use kebab-case keys from .json file default exports Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants