Skip to content

Commit

Permalink
fix: replace unknown import.meta.env lookups with full env object
Browse files Browse the repository at this point in the history
Fixes #1
  • Loading branch information
mpeyper committed Apr 9, 2021
1 parent 3f23671 commit 4b5fb76
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ const x = import.meta.env.OTHER
↓ ↓ ↓ ↓ ↓ ↓
const x = undefined
const x = {
...Object.fromEntries(Object.entries(process.env).filter(([k]) => /^VITE_/.test(k))),
NODE_ENV: process.env.NODE_ENV || 'test',
MODE: process.env.NODE_ENV || 'test',
BASE_URL: '/',
DEV: process.env.NODE_ENV !== 'production',
PROD: process.env.NODE_ENV === 'production'
}.OTHER
`;
Expand Down Expand Up @@ -86,4 +93,59 @@ const x = import.meta.env.VITE_VAR
const x = process.env.VITE_VAR
`;

exports[`vite-meta-env replace env object: replace env object 1`] = `
const env = import.meta.env
↓ ↓ ↓ ↓ ↓ ↓
const env = {
...Object.fromEntries(Object.entries(process.env).filter(([k]) => /^VITE_/.test(k))),
NODE_ENV: process.env.NODE_ENV || 'test',
MODE: process.env.NODE_ENV || 'test',
BASE_URL: '/',
DEV: process.env.NODE_ENV !== 'production',
PROD: process.env.NODE_ENV === 'production'
}
`;

exports[`vite-meta-env replace key access: replace key access 1`] = `
const key = "VITE_VAR"; const x = import.meta.env[key]
↓ ↓ ↓ ↓ ↓ ↓
const key = 'VITE_VAR'
const x = {
...Object.fromEntries(Object.entries(process.env).filter(([k]) => /^VITE_/.test(k))),
NODE_ENV: process.env.NODE_ENV || 'test',
MODE: process.env.NODE_ENV || 'test',
BASE_URL: '/',
DEV: process.env.NODE_ENV !== 'production',
PROD: process.env.NODE_ENV === 'production'
}[key]
`;

exports[`vite-meta-env replace string access: replace string access 1`] = `
const x = import.meta.env["VITE_VAR"]
↓ ↓ ↓ ↓ ↓ ↓
const x = {
...Object.fromEntries(Object.entries(process.env).filter(([k]) => /^VITE_/.test(k))),
NODE_ENV: process.env.NODE_ENV || 'test',
MODE: process.env.NODE_ENV || 'test',
BASE_URL: '/',
DEV: process.env.NODE_ENV !== 'production',
PROD: process.env.NODE_ENV === 'production'
}['VITE_VAR']
`;
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pluginTester({
'replace DEV': 'const x = import.meta.env.DEV',
'replace PROD': 'const x = import.meta.env.PROD',
'replace VITE_* variables': 'const x = import.meta.env.VITE_VAR',
'replace string access': 'const x = import.meta.env["VITE_VAR"]',
'replace key access': 'const key = "VITE_VAR"; const x = import.meta.env[key]',
'replace env object': 'const env = import.meta.env',
'not replaceable': 'const x = import.meta.env.OTHER',
'not import.meta.env': 'const x = process.env.MODE'
}
Expand Down
31 changes: 21 additions & 10 deletions packages/babel-plugin-transform-vite-meta-env/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type babelCore from '@babel/core'

const REPLACE_VARS = [
const replaceVars = [
{
regex: /^VITE_/,
replacement: (template: typeof babelCore.template, variableName: string) =>
Expand All @@ -24,20 +24,26 @@ const REPLACE_VARS = [
regex: /^PROD$/,
replacement: (template: typeof babelCore.template) =>
template.expression.ast("process.env.NODE_ENV === 'production'")
},
{
regex: /.*/,
replacement: (template: typeof babelCore.template) => template.expression.ast('undefined')
}
]

const replaceEnv = (template: typeof babelCore.template) =>
template.expression.ast(`{
...Object.fromEntries(Object.entries(process.env).filter(([k]) => /^VITE_/.test(k))),
NODE_ENV: process.env.NODE_ENV || 'test',
MODE: process.env.NODE_ENV || 'test',
BASE_URL: '/',
DEV: process.env.NODE_ENV !== 'production',
PROD: process.env.NODE_ENV === 'production'
}`)

function getReplacement(
variableName: string,
template: typeof babelCore.template
): babelCore.types.Expression {
return REPLACE_VARS.filter(({ regex }) => regex.test(variableName)).map(({ replacement }) =>
replacement(template, variableName)
)[0]
): babelCore.types.Expression | undefined {
return replaceVars
.filter(({ regex }) => regex.test(variableName))
.map(({ replacement }) => replacement(template, variableName))[0]
}

export default function viteMetaEnvBabelPlugin({
Expand All @@ -64,7 +70,12 @@ export default function viteMetaEnvBabelPlugin({

const replacement = getReplacement(variableName, template)

path.replaceWith(replacement)
if (replacement) {
path.replaceWith(replacement)
}
},
MetaProperty(path) {
path.parentPath.replaceWith(replaceEnv(template))
}
}
}
Expand Down

0 comments on commit 4b5fb76

Please sign in to comment.