Skip to content

Wrong InterpolateHtmlPlugin hook for env var replacement #6448

Closed
@GuiHash

Description

@GuiHash

Is this a bug report?

Yes

Steps to Reproduce

  1. create a new CRA project
  2. add this script in your index.html :
<script>
    var isProduction = '%NODE_ENV%' === 'production'
</script>
  1. run yarn build

Expected Behavior

Expect index.html output :

<!-- content before -->
<script>var isProduction = !0</script>
<!-- content after -->

Actual Behavior

Actual index.html output :

<!-- content before -->
<script>var isProduction = !1</script>
<!-- content after -->

Why it happens ?

The bug is due to wrong event/hook selection in InterpolateHtmlPlugin.

With beforeEmit event, The uglification of javascript in the index.html is done before the replacement of environment variables, so step by step we have :

  1. raw js
var env = '%NODE_ENV%'
var isProduction = '%NODE_ENV%' === 'production' 
  1. uglify (+ compress)
var env='%NODE_ENV%'
var isProduction=!1
  1. replace env var
var env='production'
var isProduction=!1

How to fix ?

In html-webpack-plugin, html minification (and js uglify) is done via html-minifier just after the afterTemplateExecution.

So we need to use the afterTemplateExecution event to replace env vars before uglify.
It's the first time where we have the html output, so It seams to be more relevant to replace env vars in the html with this hook.

Activity

stale

stale commented on Mar 19, 2019

@stale

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

olessavluk

olessavluk commented on Mar 20, 2019

@olessavluk

Same issue was reported earlier - #5473

self-assigned this
on Mar 21, 2019
olessavluk

olessavluk commented on Mar 24, 2019

@olessavluk

What about the following example:

<% if ("%NODE_ENV%" === "production") { %>
<script>
  console.log('it works!');
</script>
<% } %>

Since InterpolateHtmlPlugin applied afterTemplateExecution this placeholder would not be replaced.

Is it possible to run this plugin before any processing so it will work? Or maybe a better solution would be to pass ENV variables as options to HtmlWebpackPlugin, so they can be used during template execution?

GuiHash

GuiHash commented on Mar 27, 2019

@GuiHash
ContributorAuthor

@olessavluk,

This placeholder is not officially support by documentation.
Moreover, look at this issue : #4202

olessavluk

olessavluk commented on Mar 27, 2019

@olessavluk

@GuiHash yes, this is what I am looking for. Thanks for pointing this issue!

However, I think it can be done without breaking changes (no need to change the file extension, and syntax right now). I will create separate issue & probably PR with proper description

locked and limited conversation to collaborators on Oct 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Participants

    @iansu@heyimalex@olessavluk@GuiHash

    Issue actions

      Wrong InterpolateHtmlPlugin hook for env var replacement · Issue #6448 · facebook/create-react-app