Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • C create-react-app
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 1,547
    • Issues 1,547
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 417
    • Merge requests 417
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Meta
  • create-react-app
  • Issues
  • #6448
Closed
Open
Issue created Feb 17, 2019 by Administrator@rootContributor

Wrong InterpolateHtmlPlugin hook for env var replacement

Created by: 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.

Assignee
Assign to
Time tracking