react-virtualized icon indicating copy to clipboard operation
react-virtualized copied to clipboard

Bogus import in generated dist ESM file

Open yyx990803 opened this issue 4 years ago • 33 comments

Bug Report

https://unpkg.com/browse/[email protected]/dist/es/WindowScroller/utils/onScroll.js

What is the current behavior?

Last line of the linked file has the following bogus import pointing to a non-existent export:

import { bpfrpt_proptype_WindowScroller } from "../WindowScroller.js";

What is the expected behavior?

This import shouldn't exist.

Which versions of React and react-virtualized, and which browser / OS are affected by this issue? Did this work in previous versions of react-virtualized?

Not sure which version this started shipping - but this import breaks when imported in native browser ESM and also breaks in strict bundlers like esbuild / rollup.

yyx990803 avatar Jan 22 '21 23:01 yyx990803

This has been reported more than 2 years ago: https://github.com/bvaughn/react-virtualized/issues/1212

I guess webpack ignores bogus imports but some modern tools like snowpack or esbuild don't.

tajo avatar Feb 04 '21 05:02 tajo

Having the same issue. Since the there is no flow needed to be converted to prop types in the onScroll file then perhaps the babel-plugin-flow-react-proptypes plugin that seems to be causing this issue should simply be disabled for this file by adding 'no babel-plugin-flow-react-proptypes'; to the top of the file? As described in the "Supression" section of https://github.com/brigand/babel-plugin-flow-react-proptypes.

mewhhaha avatar Feb 19 '21 09:02 mewhhaha

I have published a version of the package with the fix, you can use it via resolutions field in package.json

{
    "resolutions": {
        "react-virtualized": "git+https://[email protected]/remorses/react-virtualized-fixed-import.git#9.22.3"
    }
}

remorses avatar Mar 19 '21 15:03 remorses

I have published a version of the package with the fix, you can use it via resolutions field in package.json

{
    "resolutions": {
        "react-virtualized": "git+https://[email protected]/remorses/react-virtualized-fixed-import.git#9.22.3""
    }
}

This fixed the problem for me.

ty-lerscott avatar Mar 26 '21 23:03 ty-lerscott

I have published a version of the package with the fix, you can use it via resolutions field in package.json

{
    "resolutions": {
        "react-virtualized": "git+https://[email protected]/remorses/react-virtualized-fixed-import.git#9.22.3""
    }
}

Did not work for me. Any other way to get a hotfix?

hsblhsn avatar Apr 22 '21 17:04 hsblhsn

@bvaughn can we remove that bogus import please?

0rvar avatar Apr 29 '21 06:04 0rvar

For folks using Yarn v2 you can use this patch:

package.json

{
  "resolutions": {
    "react-virtualized": "patch:[email protected]#./path/to/react-virtualized-9.22.3.patch"
  }
}

react-virtualized-9.22.3.patch

diff --git a/dist/es/WindowScroller/utils/onScroll.js b/dist/es/WindowScroller/utils/onScroll.js
index d00f0f18c6596e4e57f4f762f91fed4282610c91..42456dc5c3b9b5ea4a83ce976df542bf2e55e8a4 100644
--- a/dist/es/WindowScroller/utils/onScroll.js
+++ b/dist/es/WindowScroller/utils/onScroll.js
@@ -71,4 +71,3 @@ export function unregisterScrollListener(component, element) {
     }
   }
 }
-import { bpfrpt_proptype_WindowScroller } from "../WindowScroller.js";
\ No newline at end of file

rtsao avatar May 20 '21 22:05 rtsao

I have published a version of the package with the fix, you can use it via resolutions field in package.json

{
    "resolutions": {
        "react-virtualized": "git+https://[email protected]/remorses/react-virtualized-fixed-import.git#9.22.3""
    }
}

Did not work for me. Any other way to get a hotfix?

if you are using yarn workspaces, put this in the base package.json

zombieleet avatar Jul 15 '21 22:07 zombieleet

Using pnpm, need a patch for resoving the importing issue

zheeeng avatar Sep 07 '21 03:09 zheeeng

I think this PR fixes this issue: https://github.com/bvaughn/react-virtualized/pull/1635

If so, there needs to be a new release of react-virtualized with this patch

0rvar avatar Sep 07 '21 11:09 0rvar

I have published a version of the package with the fix, you can use it via resolutions field in package.json

{
    "resolutions": {
        "react-virtualized": "git+https://[email protected]/remorses/react-virtualized-fixed-import.git#9.22.3""
    }
}

This fixed the problem for me.

Error: The following dependencies are imported but could not be resolved: react-virtualized/styles.css

chelseachen007 avatar Dec 06 '21 08:12 chelseachen007

So this looks all ready to go, but it seems like @bvaughn doesn't want to release it? https://github.com/bvaughn/react-virtualized/commit/2e962d8f8aebc22cb1168a8d147bcaa1d27aa326 is the changelog being updated but he's asking for "someone else" to publish it.

jasonwilliams avatar Mar 04 '22 11:03 jasonwilliams

Hi,

Anyone knows what's going on with the solution for this isssue? I'm using it on vite project with npm and I'm getting this error to.

the resolutiondidn't work for those using npm.

What can I do for now?

Thanks Friends!

lironezra avatar May 08 '22 11:05 lironezra

Hi @lironezra, Did you manage to deal with this problem? I have the same one and have no idea about it.

gsiwiec01 avatar Jun 03 '22 18:06 gsiwiec01

Hi @gsiwiec01

Still doesn't work :( Waiting for someone knows how to deal with that.

lironezra avatar Jun 14 '22 13:06 lironezra

Hi @lironezra, I deleted the line manually in order to continue the work.

RedRosh avatar Jul 08 '22 07:07 RedRosh

Deleting the line is enough to get it working once, but it'll get overwritten, won't work across machines, etc. The patching approach seems like a more resilient way to go, but I'm not using Yarn v2.

I fixed it with patch-package:

npm i patch-package   # or: yarn add patch-package postinstall-postinstall

# Edit the file and delete the line with bpfrpt_proptype_WindowScroller
vim node_modules/react-virtualized/dist/es/WindowScroller/utils/onScroll.js

# Generate the patch file (it puts it in /patches)
npx patch-package react-virtualized

# Commit the patch
git add patches/
git commit -m "temporary workaround for vite error with react-virtualized"

Also, added a postinstall script in package.json to apply the patch after npm install / yarn.

 "scripts": {
+  "postinstall": "patch-package"
 }

If the patch eventually fails to apply, it should be simple enough to update it (repeating some steps above) or remove it.

dceddia avatar Aug 04 '22 19:08 dceddia

vite.config.ts

import type { Plugin } from "vite";
import fs from "fs";
import path from "path";


export default defineConfig({
  plugins: [ reactVirtualized() ], // add
}

const WRONG_CODE = `import { bpfrpt_proptype_WindowScroller } from "../WindowScroller.js";`;
export function reactVirtualized(): Plugin {
    return {
        name: "flat:react-virtualized",
        // Note: we cannot use the `transform` hook here
        //       because libraries are pre-bundled in vite directly,
        //       plugins aren't able to hack that step currently.
        //       so instead we manually edit the file in node_modules.
        //       all we need is to find the timing before pre-bundling.
        configResolved() {
            const file = require
                .resolve("react-virtualized")
                .replace(
                    path.join("dist", "commonjs", "index.js"),
                    path.join("dist", "es", "WindowScroller", "utils", "onScroll.js"),
                );
            const code = fs.readFileSync(file, "utf-8");
            const modified = code.replace(WRONG_CODE, "");
            fs.writeFileSync(file, modified);
        },
    };
}

shunyue1320 avatar Aug 08 '22 14:08 shunyue1320

vite.config.ts

export default defineConfig({
  plugins: [ reactVirtualized() ], // add
}


function reactVirtualized() {
  return {
    name: "my:react-virtualized",
    configResolved() {
      const file = require
        .resolve("react-virtualized")
        .replace(
          path.join("dist", "commonjs", "index.js"),
          path.join("dist", "es", "WindowScroller", "utils", "onScroll.js"),
        );
      const code = fs.readFileSync(file, "utf-8");
      const modified = code.replace(WRONG_CODE, "");
      fs.writeFileSync(file, modified);
    },
  }
}

can you share all file please? from whete to import path and fs? thanks :)

gilad1987 avatar Sep 05 '22 14:09 gilad1987

vite.config.ts

can you share all file please? from whete to import path and fs? thanks :)

@gilad1987 I used this following and it worked

import * as fs from 'node:fs';
import path from 'path';

SrMouraSilva avatar Sep 21 '22 20:09 SrMouraSilva

vite.config.ts

import type { Plugin } from "vite";
import fs from "fs";
import path from "path";


export default defineConfig({
  plugins: [ reactVirtualized() ], // add
}

const WRONG_CODE = `import { bpfrpt_proptype_WindowScroller } from "../WindowScroller.js";`;
export function reactVirtualized(): Plugin {
    return {
        name: "flat:react-virtualized",
        // Note: we cannot use the `transform` hook here
        //       because libraries are pre-bundled in vite directly,
        //       plugins aren't able to hack that step currently.
        //       so instead we manually edit the file in node_modules.
        //       all we need is to find the timing before pre-bundling.
        configResolved() {
            const file = require
                .resolve("react-virtualized")
                .replace(
                    path.join("dist", "commonjs", "index.js"),
                    path.join("dist", "es", "WindowScroller", "utils", "onScroll.js"),
                );
            const code = fs.readFileSync(file, "utf-8");
            const modified = code.replace(WRONG_CODE, "");
            fs.writeFileSync(file, modified);
        },
    };
}

not working for me, follow the error?

error when starting dev server:
TypeError: __require.resolve is not a function
    at Object.configResolved 

Could you help me?

pauloasantos avatar Oct 04 '22 15:10 pauloasantos

vite.config.ts

import type { Plugin } from "vite";
import fs from "fs";
import path from "path";


export default defineConfig({
  plugins: [ reactVirtualized() ], // add
}

const WRONG_CODE = `import { bpfrpt_proptype_WindowScroller } from "../WindowScroller.js";`;
export function reactVirtualized(): Plugin {
    return {
        name: "flat:react-virtualized",
        // Note: we cannot use the `transform` hook here
        //       because libraries are pre-bundled in vite directly,
        //       plugins aren't able to hack that step currently.
        //       so instead we manually edit the file in node_modules.
        //       all we need is to find the timing before pre-bundling.
        configResolved() {
            const file = require
                .resolve("react-virtualized")
                .replace(
                    path.join("dist", "commonjs", "index.js"),
                    path.join("dist", "es", "WindowScroller", "utils", "onScroll.js"),
                );
            const code = fs.readFileSync(file, "utf-8");
            const modified = code.replace(WRONG_CODE, "");
            fs.writeFileSync(file, modified);
        },
    };
}

not working for me, follow the error?

error when starting dev server:
TypeError: __require.resolve is not a function
    at Object.configResolved 

Could you help me?

You can insert const require = module.createRequire(import.meta.url); in start of configResolved function with module import like that: import module from "module";

Rejke avatar Oct 07 '22 13:10 Rejke

@bvaughn if you are not going to maintain this code anymore than give it to someone who will instead of leaving issues out to hang. Especially when its downloaded more than a million times a week. I mean come on, Evan You created this issue!

BigBallard avatar Nov 15 '22 17:11 BigBallard

@Rejke To fix this:

error when starting dev server:
TypeError: __require.resolve is not a function
    at Object.configResolved 

You can use the below:

vite.config.ts

import fs from 'node:fs/promises'
import path from 'node:path'
import url from 'node:url'
import { createRequire } from 'node:module'

// same usage inside defineConfig

const WRONG_CODE = `import { bpfrpt_proptype_WindowScroller } from "../WindowScroller.js";`

function reactVirtualized(): PluginOption {
  return {
    name: 'flat:react-virtualized',
    // Note: we cannot use the `transform` hook here
    //       because libraries are pre-bundled in vite directly,
    //       plugins aren't able to hack that step currently.
    //       so instead we manually edit the file in node_modules.
    //       all we need is to find the timing before pre-bundling.
    configResolved: async () => {
      const require = createRequire(import.meta.url)
      const reactVirtualizedPath = require.resolve('react-virtualized')
      const { pathname: reactVirtualizedFilePath } = new url.URL(reactVirtualizedPath, import.meta.url)
      const file = reactVirtualizedFilePath
        .replace(
          path.join('dist', 'commonjs', 'index.js'),
          path.join('dist', 'es', 'WindowScroller', 'utils', 'onScroll.js'),
        )
      const code = await fs.readFile(file, 'utf-8')
      const modified = code.replace(WRONG_CODE, '')
      await fs.writeFile(file, modified)
    },
  }
}

petersamokhin avatar Mar 26 '23 01:03 petersamokhin

Thank you @petersamokhin you a life saver bru

Roeefl avatar Aug 07 '23 07:08 Roeefl

This also breaks when bundling with bun 1.0.4

bun build app/static/frontend/index.jsx --outdir ./build

Screenshot 2023-10-03 at 8 02 25 PM

Fixed with

index.ts

const Bun = require("bun")
import type { BunPlugin } from "bun"
import fs from "fs"
import path from "path"

const reactVirtualizedImport: BunPlugin = {
	name: "Fix react-virtualized",
	setup(build) {
		const WRONG_CODE = `import { bpfrpt_proptype_WindowScroller } from "../WindowScroller.js";`

		const file = require
			.resolve("react-virtualized")
			.replace(
				path.join("dist", "commonjs", "index.js"),
				path.join("dist", "es", "WindowScroller", "utils", "onScroll.js"),
			);
		const code = fs.readFileSync(file, "utf-8");
		const modified = code.replace(WRONG_CODE, "");
		fs.writeFileSync(file, modified);
	},
};


await Bun.build({
	entrypoints: ["app/static/frontend/index.jsx"],
	outdir: "build",
	target: "browser",
	// plugins: [reactVirtualizedImport],
})

bun index.ts

JohnDDuncanIII avatar Oct 03 '23 23:10 JohnDDuncanIII

为什么这个问题还没修复?

xiaohaoo avatar Oct 27 '23 06:10 xiaohaoo

Above solution doesn't work in a browser app if you don't want to introduce node to your project. So I created a repo that works with npm/vite/ts/react and is the latest version as of today, 9.22.5. It's only the distribution no original sources. react-virtualized-fixed

Install with npm: npm install https://github.com/samarai-project/react-virtualized-fixed.git

this will replace the npm package with the repo. Check the last line in node_modules/react-virtualized/dist/es/WindowScroller/utils/onScroll.js, needs to be commented out

samarai-jk avatar Oct 28 '23 13:10 samarai-jk

Bumping. Can the fix please get merged for this? Frustrating adding this workaround in all our apps.

tavgordon avatar Nov 17 '23 08:11 tavgordon

"postinstall": "patch-package"

it works for me. thanks a lot!

ChandraMedhika avatar Nov 25 '23 08:11 ChandraMedhika

@Rejke To fix this:

error when starting dev server:
TypeError: __require.resolve is not a function
    at Object.configResolved 

You can use the below:

vite.config.ts

import fs from 'node:fs/promises'
import path from 'node:path'
import url from 'node:url'
import { createRequire } from 'node:module'

// same usage inside defineConfig

const WRONG_CODE = `import { bpfrpt_proptype_WindowScroller } from "../WindowScroller.js";`

function reactVirtualized(): PluginOption {
  return {
    name: 'flat:react-virtualized',
    // Note: we cannot use the `transform` hook here
    //       because libraries are pre-bundled in vite directly,
    //       plugins aren't able to hack that step currently.
    //       so instead we manually edit the file in node_modules.
    //       all we need is to find the timing before pre-bundling.
    configResolved: async () => {
      const require = createRequire(import.meta.url)
      const reactVirtualizedPath = require.resolve('react-virtualized')
      const { pathname: reactVirtualizedFilePath } = new url.URL(reactVirtualizedPath, import.meta.url)
      const file = reactVirtualizedFilePath
        .replace(
          path.join('dist', 'commonjs', 'index.js'),
          path.join('dist', 'es', 'WindowScroller', 'utils', 'onScroll.js'),
        )
      const code = await fs.readFile(file, 'utf-8')
      const modified = code.replace(WRONG_CODE, '')
      await fs.writeFile(file, modified)
    },
  }
}

2023 and still facing the issue on new install, this solution works for me.

sluxzer avatar Dec 15 '23 21:12 sluxzer