mholt / PapaParse

Fast and powerful CSV (delimited text) parser that gracefully handles large files and malformed input
http://PapaParse.com
MIT License
12.43k stars 1.14k forks source link

Getting errors when used with es modules in karma -- "root" is undefined #821

Open donecarlo opened 4 years ago

donecarlo commented 4 years ago

Currently using web-components (LitElement, open-wc) and I have one component using Papaparse. When running tests via karma, it defaults to the "Browser globals" scenario, though in this case, "root" is undefined. Do you have any suggestions on what can be a possible workaround for this?

The karma config is Using @open-wc/testing-karma as the base config, which uses the @open-wc/karma-esm plugin.

MrMiracles commented 1 year ago

Same problem,

just importing it as a module will result in an error: Uncaught TypeError: root is undefined

Reproducible:

<!DOCTYPE html>
<head>
    <script type="module" src="./lib/papaparse.js"></script>
</head>
</html>

Or

<!DOCTYPE html>
<head>
    <script type="module">
        import * as papaparse  from "./lib/papaparse.js"
    </script>
</head>
</html>

EDIT: fixed the problem for now by adding export default and root=window to the first line:

/* @license
Papa Parse
v5.4.1
https://github.com/mholt/PapaParse
License: MIT
*/

export default (function(root=window, factory)
{
    /* globals define */
    if (typeof define === 'function' && define.amd)
    {
        // AMD. Register as an anonymous module.
        define([], factory);
    }
    else if (typeof module === 'object' && typeof exports !== 'undefined')
    {
        // Node. Does not work with strict CommonJS, but
        // only CommonJS-like environments that support module.exports,
        // like Node.
        module.exports = factory();
    }
    else
    {
        // Browser globals (root is window)
        root.Papa = factory();
    }
    // in strict mode we cannot access arguments.callee, so we need a named reference to
    // stringify the factory method for the blob worker
    // eslint-disable-next-line func-name
}
...