Skip to content

Commit

Permalink
support import.meta.env
Browse files Browse the repository at this point in the history
  • Loading branch information
vankop committed May 19, 2022
1 parent 2738eeb commit 0756d91
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/EnvironmentPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ class EnvironmentPlugin {
});
}

definitions[`process.env.${key}`] =
value === undefined ? "undefined" : JSON.stringify(value);
definitions[`process.env.${key}`] = definitions[
`import.meta.env.${key}`
] = value === undefined ? "undefined" : JSON.stringify(value);
}

new DefinePlugin(definitions).apply(compiler);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
it("should support import.meta.env", () => {
expect(process.env.MY_ENV).toBe("env1");
expect(import.meta.env.MY_ENV).toBe("env1");
});
15 changes: 15 additions & 0 deletions test/configCases/plugins/environment-plugin/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,20 @@ module.exports = [
unknownContextCritical: false
},
plugins: [new EnvironmentPlugin("III")]
},
{
name: "import-meta-env",
entry: "./support-import-meta-env",
plugins: [
new EnvironmentPlugin("MY_ENV"),
compiler => {
compiler.hooks.beforeRun.tap("my-plugin", () => {
process.env.MY_ENV = "env1";
});
compiler.hooks.done.tap("my-plugin", () => {
process.env.MY_ENV = undefined;
});
}
]
}
];

0 comments on commit 0756d91

Please sign in to comment.