javiertury / babel-plugin-transform-import-meta

Other
41 stars 9 forks source link

import.meta.env is undefined using Vite #2

Closed pooledge closed 3 years ago

pooledge commented 3 years ago

Hi, I am trying to get @testing-library/svelte running using Sveltekit and Vite 2. Not sure whether it's a jest issue. Transpiling ESM works, but the import.meta seems to contain only url object, as per error:

TypeError: Cannot read property 'VITE_API_URL' of undefined
const url = import.meta.env.VITE_API_URL;

.babelrc

{
    "presets": [
        [
            "@babel/preset-env",
            {
                "targets": {
                    "node": "current"
                }
            }
        ]
    ],
    "plugins": ["babel-plugin-transform-import-meta"]
}

package.json

{
...
"devDependencies": {
"@babel/core": "^7.13.15",
"@babel/plugin-transform-modules-commonjs": "^7.13.8",
"@babel/preset-env": "^7.13.15",
"@sveltejs/adapter-node": "next",
"@sveltejs/kit": "next",
"@testing-library/jest-dom": "^5.11.10",
"@testing-library/svelte": "^3.0.3",
"@typescript-eslint/eslint-plugin": "^4.19.0",
"@typescript-eslint/parser": "^4.19.0",
"babel-jest": "^26.6.3",
"babel-plugin-transform-import-meta": "^1.0.1",
"eslint": "^7.22.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-svelte3": "^3.1.0",
"jest": "^26.6.3",
"prettier": "~2.2.1",
"prettier-plugin-svelte": "^2.2.0",
"sass": "^1.0.0",
"svelte": "^3.29.0",
"svelte-jester": "^1.4.0",
"svelte-preprocess": "^4.0.0",
"tslib": "^2.0.0",
"typescript": "^4.0.0",
"vite": "^2.1.0"
},
"type": "module",
"engines": {
"node": ">= 12.17.0"
},
"jest": {
"transform": {
    "^.+\\.js$": "babel-jest",
    "^.+\\.svelte$": "svelte-jester"
},
"moduleNameMapper": {
    "^\\$api/(.*)": "<rootDir>/src/api/$1",
    "^\\$app/(.*)": [
        "<rootDir>/.svelte/dev/runtime/app/$1",
        "<rootDir>/.svelte/build/runtime/app/$1"
    ],
    "^\\$components/(.*)": "<rootDir>/src/components/$1",
    "^\\$lib/(.*)": "<rootDir>/src/lib/$1",
    "^\\$service-worker": "<rootDir>/.svelte/build/runtime/service-worker",
    "^\\$stores/(.*)": "<rootDir>/src/stores/$1"
},
"moduleFileExtensions": [
    "js",
    "svelte"
],
"setupFilesAfterEnv": [
    "@testing-library/jest-dom/extend-expect"
]
}
javiertury commented 3 years ago

I've just released a major version.

Now this plugin transpiles only known import.meta properties, that is, only import.meta.url. The remaining properties are untouched and it's the responsibility of other plugins to substitute them. I believe this is what Vite does.

gajus commented 2 years ago

You should be using https://github.com/OpenSourceRaidGuild/babel-vite/tree/main/packages/babel-preset-vite for this.