parcel icon indicating copy to clipboard operation
parcel copied to clipboard

Parcel should auto-add process.env to posthtml-expression locals

Open yefim opened this issue 1 year ago • 2 comments

🙋 feature request

I'm trying to include an analytics script ONLY in production. I want to use a .posthtmlrc file and not a .posthtmlrc.js file to take advantage of all the caching. If parcel could inject process.env into the locals of posthtml-expression I would be very pleased :)

🤔 Expected Behavior

I want to be able to use process.env in my index.html file

😯 Current Behavior

My current setup is:

// .posthtmlrc.js
module.exports = {
  plugins: {
    'posthtml-include': {},
    'posthtml-expressions': {
      locals: {
        NODE_ENV: process.env.NODE_ENV,
      },
    },
  },
};
<!-- index.html -->
<if condition="NODE_ENV === 'production'">
  <script src="https://cdn.usefathom.com/script.js" defer></script>
</if>

💁 Possible Solution

Auto-inject process.env into index.html so I can do:

<if condition="process.env.NODE_ENV === 'production'">
  <script src="https://cdn.usefathom.com/script.js" defer></script>
</if>

Maybe I should take this up with posthtml. If so, don't mind me. Thanks! Love parcel and use it extensively for new sideprojects like valentineroulette.com

yefim avatar Jan 27 '24 07:01 yefim

Looking for the exact same use case! For now I can only think of the config as javascript route...

samtsai avatar May 07 '24 16:05 samtsai

Another workaround is to create a custom posthtml plugin that can modify your dom tree. That's actually working better for me in my use case.

samtsai avatar May 07 '24 16:05 samtsai