InvokIT / js-untar

Library for reading tar files in the browser.
MIT License
75 stars 32 forks source link

Use an ESM-friendly UMD wrapper #30

Open fuweichin opened 4 years ago

fuweichin commented 4 years ago

Overview
The UMD wrapper js-untar currently in use may cause an error in certain circumstances.

Steps to Reproduce:

<script type="module">
import '/node_modules/jquery/dist/jquery.min.js';/* global jQuery */
import '/node_modules/pako/dist/pako_inflate.min.js';/* global pako */
import '/node_modules/js-untar/build/dist/untar.js';/* global untar */ 
//...
</script>

jquery and pako are okay with native ESM loader, while js-untar will throw an error

TypeError: Cannot set property 'untar' of undefined

Possible Solution:
Use a more versatile variant of UMD, e.g. returnExportsGlobal.js

guest271314 commented 2 years ago

In the browser I get the error

TypeError: Cannot set properties of undefined (setting 'untar')
    at untar.js:1:118
    at untar.js:1:123

using https://unpkg.com/js-untar@2.0.0/build/dist/untar.js.

Currently I handle the error and continue

try {
  await import('https://unpkg.com/js-untar@2.0.0/build/dist/untar.js');
} catch(e) {
   console.log(e);
} finally {
  // do stuff
}
guest271314 commented 2 years ago

The TypeError is thrown due to

https://github.com/InvokIT/js-untar/blob/49e639cf82e8d58dccb3458cbd08768afee8b41c/src/untar.js#L5

this is not defined in Ecmascript Modules. Substituting globalThis avoids the error.