closest icon indicating copy to clipboard operation
closest copied to clipboard

webpack error: Unexpected token: name (element)

Open strarsis opened this issue 5 years ago • 3 comments

webpack production build (with minify) fails when element-closest is imported:

import 'element-closest';
Unexpected token: name (element) [scripts/main_a637191f.js:8161,10]

strarsis avatar Apr 09 '19 20:04 strarsis

I’m not sure that’s coming from me.

https://unpkg.com/[email protected]/index.js there is no name variable.

jonathantneal avatar Apr 09 '19 21:04 jonathantneal

this is likely due to using gulp-uglify (or another js minifier) which only supports es5 and this library uses es6, i'd recommend checking out

  • https://www.npmjs.com/package/gulp-uglify
  • https://www.npmjs.com/package/uglify-es
  • or using https://babeljs.io to convert your libraries to es5 prior to minification

troyxmccall avatar Apr 09 '19 22:04 troyxmccall

with version v3.0.0
try using a named import like this

import elementClosest from "element-closest";
elementClosest(window);

Also the elementClosest(window); is required to initialize the polyfill in Node (eg. with webpack, gulp etc)

diverent2 avatar Oct 21 '19 13:10 diverent2