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 18, 2022
1 parent 2738eeb commit 5148728
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/EnvironmentPlugin.js
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
@@ -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");
});
12 changes: 12 additions & 0 deletions test/configCases/plugins/environment-plugin/webpack.config.js
Expand Up @@ -9,6 +9,7 @@ process.env.EEE = "eee";
process.env.FFF = "fff";
process.env.GGG = "ggg";
process.env.III = "";
process.env.MY_ENV = "env1";

/** @type {import("../../../../").Configuration[]} */
module.exports = [
Expand Down Expand Up @@ -64,5 +65,16 @@ 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.done.tap("my-plugin", () => {
process.env.MY_ENV = undefined;
})
]
}
];

0 comments on commit 5148728

Please sign in to comment.