vite-plugins icon indicating copy to clipboard operation
vite-plugins copied to clipboard

'require' in string will be unexpectedly parsed. eg. console.log("var foo = require('foo')")

Open MecDog opened this issue 3 years ago • 2 comments

Versions

  • originjs: 1.0.2
  • node: 14.18.1

Reproduction

source code:

 console.error("You need to import react !\nExample: react=require('react')");

'vite-plugin-commonjs' output:

import * as __require_for_vite_iuMxJX from "react"
console.error("You need to import react !\nExample: react=__require_for_vite_iuMxJX");

MecDog avatar Feb 23 '22 03:02 MecDog

It's a little difficult to identify require in a string by RegExp. I've considered using a AST, but this will slow down transform.

flyfishzy avatar Feb 24 '22 13:02 flyfishzy

I have the same problem. Perhaps there could be an option to use an AST for those who need it? I'd rather have this plugin be slower than not work at all.

nwoltman avatar Mar 28 '22 20:03 nwoltman