Skip to content

Vite constants are not strings #351

@twhitbeck

Description

@twhitbeck

Thank you for this plugin! It is working really well for me, but I am running into one issue. Vite has some built in constants that live on import.meta.env that are not strings. import.meta.env.{DEV,PROD,SSR} are all booleans.
When setting process.env.DEV in node.js the value gets coerced to a string, which leads to a runtime discrepancy where in tests import.meta.env.DEV will be "true" | "false" instead of boolean.
I'm not 100% sure what the fix here is, but maybe there could be a configuration parameter to this plugin to set some properties somewhere other than process.env.

{
  "experimental": {
    "plugins": [
        [
            "swc-plugin-import-meta-env",
            {
                "globalThis.fakeProcessEnv": ["DEV", "PROD", "SSR"]
            }
        ]
    ]
},

which would transform

console.log(import.meta.env.FOO);
console.log(import.meta.env.PROD);

into

console.log(process.env.FOO);
console.log(globalThis.fakeProcessEnv.PROD);

wdyt?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions