gregberge / svgr

Transform SVGs into React components 🦁
https://react-svgr.com
MIT License
10.59k stars 422 forks source link

@svgr/webpack loader with Storybook 6.5.16 TypeError: this.getOptions is not a function #875

Open ForeverYoung1208 opened 1 year ago

ForeverYoung1208 commented 1 year ago

🐛 Bug Report

When use @svgr/webpack loader with Storybook 6.5.16 got the error on build

ModuleBuildError: Module build failed (from ./node_modules/@svgr/webpack/dist/index.js):
TypeError: this.getOptions is not a function
    at Object.svgrLoader (/home/fy/work/eten/ui-kit/node_modules/@svgr/webpack/dist/index.js:82:24)
    at /home/fy/work/eten/ui-kit/node_modules/webpack/lib/NormalModule.js:316:20
    at /home/fy/work/eten/ui-kit/node_modules/loader-runner/lib/LoaderRunner.js:367:11
    at /home/fy/work/eten/ui-kit/node_modules/loader-runner/lib/LoaderRunner.js:233:18
    at runSyncOrAsync (/home/fy/work/eten/ui-kit/node_modules/loader-runner/lib/LoaderRunner.js:143:3)
    at iterateNormalLoaders (/home/fy/work/eten/ui-kit/node_modules/loader-runner/lib/LoaderRunner.js:232:2)
    at Array.<anonymous> (/home/fy/work/eten/ui-kit/node_modules/loader-runner/lib/LoaderRunner.js:205:4)
    at Storage.finished (/home/fy/work/eten/ui-kit/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:55:16)
    at /home/fy/work/eten/ui-kit/node_modules/enhanced-resolve/lib/CachedInputFileSystem.js:91:9
    at /home/fy/work/eten/ui-kit/node_modules/graceful-fs/graceful-fs.js:123:16
    at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:68:3)

which is caused at

https://github.com/gregberge/svgr/blob/82928f02e4a2ae1160e54884461f9edde4ad2293/packages/webpack/src/index.ts#LL65C29-L65C29

which is more likely caused by absence of relevant loader context at the webpack version which Storybook 6.5.16 uses.

To Reproduce

Use any svgr imported component in the Storybook 6.5.16 and try to build Storybook.

Expected behavior

Component is imported and shows imported svg

Run npx envinfo --system --binaries --npmPackages @svgr/core,@svgr/cli,@svgr/webpack,@svgr/rollup --markdown --clipboard

Paste the results here:

## System:
 - OS: Linux 5.15 Mageia 8
 - CPU: (4) x64 Intel(R) Core(TM) i3-8100 CPU @ 3.60GHz
 - Memory: 16.21 GB / 23.42 GB
 - Container: Yes
 - Shell: 5.1.16 - /bin/bash
## Binaries:
 - Node: 16.15.1 - ~/.nvm/versions/node/v16.15.1/bin/node
 - npm: 8.11.0 - ~/.nvm/versions/node/v16.15.1/bin/npm
## npmPackages:
 - @svgr/webpack: ^8.0.1 => 8.0.1 
ForeverYoung1208 commented 1 year ago

As a solution, i felt back to version "@svgr/webpack": "5.4.0" , it works fine. Maybe, later versions also can work, i'll try in future... But for my current puropses as a temporary solution i will use this. Upd: problem file was added here https://github.com/gregberge/svgr/pull/611/files so i think any version 5.x.x should be good.