mholt / PapaParse

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

Bundling with rollup #797

Open maxwell8888 opened 4 years ago

maxwell8888 commented 4 years ago

I am trying to use PapaParse in the browser to parse a file using:

Papa.parse(fileInput.files[0], {
    complete: function(results) {
        console.log(results);
    }
});

I am bundling my javascript with the below rollup config which handles bare module specifiers:

import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";

export default {
  input: ["src/public/index.js", "src/private/index.js"],
  output: [
    // ES module version, for modern browsers
    {
      dir: "functions/client",
      format: "es",
      sourcemap: true,
      entryFileNames: "[name].js",
      chunkFileNames: "shared.js",
    },
  ],
  plugins: [resolve(), commonjs()],
};

and then importing PapaParse with import Papa from "papaparse";, however I get the following build error from rollup:

(!) Plugin node-resolve: preferring built-in module 'stream' over local alternative at 'stream', pass 'preferBuiltins: false' to disable this behavior or 'preferBuiltins: true' to disable this warning

and the following error in the browser:

Uncaught TypeError: Failed to resolve module specifier "stream". Relative references must start with either "/", "./", or "../".
pokoli commented 4 years ago

Have you trieed passsing 'preferBuiltins: true' to disable the warning?

maxwell8888 commented 4 years ago

I'm bundling for the browser, so don't want to include stream. Adding preferBuiltins: false option to @rollup/plugin-node-resolve, still fails with Error: Could not load stream (imported by node_modules/papaparse/papaparse.js): ENOENT: no such file or directory, open 'stream'.

maxwell8888 commented 4 years ago

Line 913 is causing the problem: var Duplex = require('stream').Duplex;.

maxwell8888 commented 4 years ago

Updating to var Duplex = false ? require("stream").Duplex : true;, where false should be a check for whether the platform is a web browser, solves the problem in my local version and allows rollup to bundle without adding the rogue import stream from "stream";. I tried adding var Duplex = global !== "browser" ? require("stream").Duplex : true; but this fails, so I'm not sure what to replace false with in order to get this working and passing tests so the fix can be merged.