Skip to content

Commit

Permalink
Merge pull request #2 from OpenSourceRaidGuild/#1
Browse files Browse the repository at this point in the history
fix: replace unknown import.meta.env lookups with full env object
  • Loading branch information
JacobMGEvans committed Apr 9, 2021
2 parents 3f23671 + 9a96ff5 commit a1d0441
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`vite-meta-env not import.meta lookup: not import.meta lookup 1`] = `
const x = import.meta()
↓ ↓ ↓ ↓ ↓ ↓
const x = import.meta()
`;

exports[`vite-meta-env not import.meta.env: not import.meta.env 1`] = `
const x = import.meta.other
↓ ↓ ↓ ↓ ↓ ↓
const x = import.meta.other
`;

exports[`vite-meta-env not import.meta: not import.meta 1`] = `
const x = process.env.MODE
↓ ↓ ↓ ↓ ↓ ↓
Expand All @@ -17,7 +39,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 +115,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,7 +12,12 @@ 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'
'not import.meta.env': 'const x = import.meta.other',
'not import.meta': 'const x = process.env.MODE',
'not import.meta lookup': 'const x = import.meta()'
}
})
43 changes: 33 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,24 @@ export default function viteMetaEnvBabelPlugin({

const replacement = getReplacement(variableName, template)

path.replaceWith(replacement)
if (replacement) {
path.replaceWith(replacement)
}
},
MetaProperty(path) {
const envNode = t.isMemberExpression(path.parentPath.node) && path.parentPath.node

if (!envNode) {
return
}

const isEnvVar = t.isIdentifier(envNode.property) && envNode.property.name === 'env'

if (!isEnvVar) {
return
}

path.parentPath.replaceWith(replaceEnv(template))
}
}
}
Expand Down

0 comments on commit a1d0441

Please sign in to comment.