preactjs/wmr

SyntaxError when loading script with process.env

andrewiggins opened this issue · 0 comments

Describe the bug
It appears there is a bug in one of the development transforms that incorrectly transforms the following code:

let FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM;
if (typeof process !== "undefined") {
  ({ FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM } = process.env);
}

to

var process={"browser":true,"env":{"NODE_ENV":"development"}};
let FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM;
if (typeof process !== "undefined") {
  ({ FORCE_COLOR, NODE_DISAtypeof undefined COLOR, TERM } = process.env);
}

Removing the typeof process !== 'undefined' check results in the bug going away. This code can be find in the wild in the kleur package: https://github.com/lukeed/kleur/blob/598f24cb7f5054b7c504e9ab89251305311131b1/colors.mjs#L1

To Reproduce
Steps to reproduce the behavior:

  1. Create the following files in a directory:
    <!-- index.html -->
    <html>
    <script type="module" src="./index.js"></script>
    </html>
    // index.js
    let FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM;
    if (typeof process !== "undefined") {
       ({ FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM } = process.env);
    }
  2. Run npx wmr
  3. Load http://localhost:8080/index.html
  4. See the following error in the console: Uncaught SyntaxError: Unexpected identifier

Also

  1. Run npx wmr build
  2. Notice error the console:
    Error: Unexpected token
    
     at Cs (D:\sandbox\wmr-parse-error\node_modules\wmr\wmr.cjs:2:138696)
     at va.error (D:\sandbox\wmr-parse-error\node_modules\wmr\wmr.cjs:2:177314)
     at va.tryParse (D:\sandbox\wmr-parse-error\node_modules\wmr\wmr.cjs:2:185502)
     at va.setSource (D:\sandbox\wmr-parse-error\node_modules\wmr\wmr.cjs:2:182924)
     at Gu.addModuleSource (D:\sandbox\wmr-parse-error\node_modules\wmr\wmr.cjs:2:353239)
     at async Gu.fetchModule (D:\sandbox\wmr-parse-error\node_modules\wmr\wmr.cjs:2:354416)
     at async Promise.all (index 0)

Expected behavior
WMR should properly transform the module.

Bug occurs with:

  • wmr or wmr start (development)
  • wmr build (production)
  • wmr serve

Desktop (please complete the following information):

  • OS: Windows 10
  • Browser: Edge 94
  • Node Version: 14.17.4
  • WMR Version: 3.6.0