PapaParse icon indicating copy to clipboard operation
PapaParse copied to clipboard

Bundling with rollup

Open maxwell8888 opened this issue 5 years ago • 4 comments

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 "../".

maxwell8888 avatar May 26 '20 18:05 maxwell8888

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

pokoli avatar May 27 '20 06:05 pokoli

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 avatar Sep 03 '20 11:09 maxwell8888

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

maxwell8888 avatar Sep 03 '20 12:09 maxwell8888

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.

maxwell8888 avatar Sep 03 '20 12:09 maxwell8888