This repository has been archived by the owner on Dec 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add import.meta.env support to test runner
- Loading branch information
1 parent
1207a1c
commit c84d51b
Showing
4 changed files
with
66 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const template = require("@babel/template").default; | ||
|
||
const PUBLIC_ENV_REGEX = /^SNOWPACK_PUBLIC_/; | ||
function generateEnvObject(mode) { | ||
const envObject = { ...process.env }; | ||
for (const env of Object.keys(envObject)) { | ||
if (!PUBLIC_ENV_REGEX.test(env)) { | ||
delete envObject[env]; | ||
} | ||
} | ||
envObject.MODE = mode; | ||
envObject.NODE_ENV = mode; | ||
return envObject; | ||
} | ||
|
||
/** | ||
* Add import.meta.env support | ||
* Note: import.meta.url is not supported at this time | ||
*/ | ||
module.exports = function () { | ||
const ast = template.ast(` | ||
({env: ${JSON.stringify(generateEnvObject("test"))}}) | ||
`); | ||
return { | ||
visitor: { | ||
MetaProperty(path, state) { | ||
path.replaceWith(ast); | ||
}, | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const template = require("@babel/template").default; | ||
|
||
const PUBLIC_ENV_REGEX = /^SNOWPACK_PUBLIC_/; | ||
function generateEnvObject(mode) { | ||
const envObject = { ...process.env }; | ||
for (const env of Object.keys(envObject)) { | ||
if (!PUBLIC_ENV_REGEX.test(env)) { | ||
delete envObject[env]; | ||
} | ||
} | ||
envObject.MODE = mode; | ||
envObject.NODE_ENV = mode; | ||
return envObject; | ||
} | ||
|
||
/** | ||
* Add import.meta.env support | ||
* Note: import.meta.url is not supported at this time | ||
*/ | ||
module.exports = function () { | ||
const ast = template.ast(` | ||
({env: ${JSON.stringify(generateEnvObject("test"))}}) | ||
`); | ||
return { | ||
visitor: { | ||
MetaProperty(path, state) { | ||
path.replaceWith(ast); | ||
}, | ||
}, | ||
}; | ||
}; |