Consider replacing import.meta.env
rather than assigning it
#1889
Rich-Harris
started this conversation in
Ideas
Replies: 2 comments 2 replies
-
There's a nice middle ground here as well, where we do this this find-replace rewrite as a first-pass and then only include the // input: console.log(`SSR? ${import.meta.env.SSR}`);
import * as __SNOWPACK_ENV__ from '../__snowpack__/env.js';
console.log(`SSR? ${__SNOWPACK_ENV__.SSR}`);
// input: console.log(`SSR? ${import.meta.env}`);
import * as __SNOWPACK_ENV__ from '../__snowpack__/env.js';
console.log(`SSR? ${__SNOWPACK_ENV__}`);
// input: console.log(`SSR? ${import.meta}`);
import * as __SNOWPACK_ENV__ from '../__snowpack__/env.js';
import.meta.env = __SNOWPACK_ENV__;
console.log(`SSR? ${import.meta}`); |
Beta Was this translation helpful? Give feedback.
2 replies
-
I would like to not that adding the imports offsets the line numbers in errors. So i can no longer use the line number in the browser to look at the code line in my editor. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Currently, if a module references
import.meta.env
, the env value is imported and then assigned toimport.meta
. This......becomes this:
But
import.meta
doesn't translate easily to other tooling. If you manually bundle the code through Rollup, for example,import.meta
is treated as a special construct (which it must ifimport.meta.url
is to survive the bundling step, etc), andimport.meta.env
becomesundefined
.Even if we stay within Snowpack, using
import.meta
like this is suboptimal. Using the experimentaloptimize
option, we end up with this:With
@snowpack/plugin-webpack
, we get this:If Snowpack instead translated that code to the following...
...and the
env.js
file was written thusly (instead of default-exporting an object)......then we'd see these results with
optimize
and@snowpack/plugin-webpack
:This isn't just more compact and less brittle, the code will also execute faster, which could matter if you were checking
import.meta.env.blah
in a hot code path.There are edge case bugs here —
console.log(import.meta)
wouldn't work the same way, for example. But I think that's just a question of documentation, and that the current issues around portability are more severe in any case.Beta Was this translation helpful? Give feedback.
All reactions