Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webpack 5: Failed to parse source map from "@mswjs/interceptors" #1030

Closed
JoeyAtSS opened this issue Dec 15, 2021 · 17 comments · Fixed by mswjs/interceptors#204 or #1095
Closed

webpack 5: Failed to parse source map from "@mswjs/interceptors" #1030

JoeyAtSS opened this issue Dec 15, 2021 · 17 comments · Fixed by mswjs/interceptors#204 or #1095
Assignees
Labels
bug Something isn't working

Comments

@JoeyAtSS
Copy link

Describe the bug

New webpack 5.0 throwing compilation error regarding source map not found
image

there are multiple of these, but for the sake of simplicity, i just pasted one of it.

Environment

  • msw: 0.36.3
  • nodejs: 14.18.2
  • npm: 8.3.0

Please also provide your browser version. N/A, this is compilation warning error

To Reproduce

Steps to reproduce the behavior:

  1. Create new react app using CRA5.0
  2. install latest msw.
  3. Npm start, the warnings are being thrown in the terminal

Expected behavior

should not have these warning errors.

Screenshots

image

@JoeyAtSS JoeyAtSS added the bug Something isn't working label Dec 15, 2021
@mauroaccornero
Copy link

I've the same warning after updating create react app. I see the warnings after npm start. All the warning are about @mswjs/interceptors at least for me. I tried to rebuild the module but didn't help.

@kettanaito
Copy link
Member

Hey, @JoeyAtSS. Thanks for reporting this.

It looks like the issue is coming from the sourcemaps for the @mswjs/interceptors package:

// node_modules/@mswjs/interceptors/lib/interceptors/fetch/index.js.map
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/interceptors/fetch/index.ts"],

Note how it references the fetch/index.ts file.

I don't think this is an issue with the library itself, as we haven't changed our bundling process in a while. It may be an indirect issue of webpack5 behaving differently, to which we have to find a proper way to adjust.

One thing I'm not certain about is why webpack attempts to require modules from the sourcemaps. To my best knowledge, sourcemaps link the compiled and source code to ease your debugging whenever the former has any issues. I wouldn't expect the source module references to be imported.

@kettanaito
Copy link
Member

I have a suspicion that CRA5 should configure webpack to ignore source maps from node_modules. I'd like to know how it used to behave previously.

@slowselfip
Copy link

slowselfip commented Dec 27, 2021

Hello,

The source map files links to the src/ - folder that is not distributed.

// node_modules/@mswjs/interceptors/lib/interceptors/utils/uuid.js.map
{"version":3,"file":"uuid.js","sourceRoot":"","sources":["../../-->src<--/utils/uuid.ts"] ...}

In the source-map-loader README it says: "The source-map-loader will extract from any JavaScript file, including those in the node_modules directory.".

As for CRA 5:
I think a change was added in this PR: facebook/create-react-app#8227
Commit: facebook/create-react-app@382ba21#diff-8e25c4f6f592c1fcfc38f0d43d62cbd68399f44f494c1b60f0cf9ccd7344d697
Current source: https://github.com/facebook/create-react-app/blob/9673858a3715287c40aef9e800c431c7d45c05a2/packages/react-scripts/config/webpack.config.js#L350

I'm seeing these warnings when starting the dev server using react-scripts start, If i exclude the above config by setting GENERATE_SOURCEMAP=false when starting the dev server the warnings disappear.

@JonRoosevelt
Copy link

JonRoosevelt commented Jan 3, 2022

Hello,

The source map files links to the src/ - folder that is not distributed.

// node_modules/@mswjs/interceptors/lib/interceptors/utils/uuid.js.map
{"version":3,"file":"uuid.js","sourceRoot":"","sources":["../../-->src<--/utils/uuid.ts"] ...}

In the source-map-loader README it says: "The source-map-loader will extract from any JavaScript file, including those in the node_modules directory.".

As for CRA 5: I think a change was added in this PR: facebook/create-react-app#8227 Commit: facebook/create-react-app@382ba21#diff-8e25c4f6f592c1fcfc38f0d43d62cbd68399f44f494c1b60f0cf9ccd7344d697 Current source: https://github.com/facebook/create-react-app/blob/9673858a3715287c40aef9e800c431c7d45c05a2/packages/react-scripts/config/webpack.config.js#L350

I'm seeing these warnings when starting the dev server using react-scripts start, If i exclude the above config by setting GENERATE_SOURCEMAP=false when starting the dev server the warnings disappear.

In case someone is lost on how to do it, just add the GENERATE_SOURCEMAP=false before the start script.
In my case:

...
"scripts": {
    "start": "GENERATE_SOURCEMAP=false react-scripts start",
   ...
  },

@ssmkhrj
Copy link

ssmkhrj commented Jan 10, 2022

Hello,
The source map files links to the src/ - folder that is not distributed.

// node_modules/@mswjs/interceptors/lib/interceptors/utils/uuid.js.map
{"version":3,"file":"uuid.js","sourceRoot":"","sources":["../../-->src<--/utils/uuid.ts"] ...}

In the source-map-loader README it says: "The source-map-loader will extract from any JavaScript file, including those in the node_modules directory.".
As for CRA 5: I think a change was added in this PR: facebook/create-react-app#8227 Commit: facebook/create-react-app@382ba21#diff-8e25c4f6f592c1fcfc38f0d43d62cbd68399f44f494c1b60f0cf9ccd7344d697 Current source: https://github.com/facebook/create-react-app/blob/9673858a3715287c40aef9e800c431c7d45c05a2/packages/react-scripts/config/webpack.config.js#L350
I'm seeing these warnings when starting the dev server using react-scripts start, If i exclude the above config by setting GENERATE_SOURCEMAP=false when starting the dev server the warnings disappear.

In case someone is lost on how to do it, just add the GENERATE_SOURCEMAP=false before the start script. In my case:

...
"scripts": {
    "start": "GENERATE_SOURCEMAP=false react-scripts start",
   ...
  },

For Windows Users 🚀🤘

Create an environment variables file named .env in the project root and add GENERATE_SOURCEMAP=false to it.

OR

Run npm i -D cross-env and then in the package.json file update the start script to the following:

"scripts": {
    "start": "cross-env GENERATE_SOURCEMAP=false react-scripts start"
}

@kettanaito
Copy link
Member

What I'd like to understand better is whether this sourcemap behavior is specific to CRA or happens on clean webpack apps?

If it's specific to CRA then it needs to be addressed when using CRA (i.e using the GENERATE_SOURCEMAP env variable). If this happens generally with any webpack app, then we need to publish the source code to NPM (mswjs/interceptors#196) to prevent this from happening.

Please, would somebody have time to spin up a clean webpack app to check this?

akunzai added a commit to akunzai/react-boilerplate that referenced this issue Jan 25, 2022
- Fixes warnings: [Failed to parse source map from](mswjs/msw#1070)
- mswjs/msw#1030
@usmaanalii
Copy link

What I'd like to understand better is whether this sourcemap behavior is specific to CRA or happens on clean webpack apps?

If it's specific to CRA then it needs to be addressed when using CRA (i.e using the GENERATE_SOURCEMAP env variable). If this happens generally with any webpack app, then we need to publish the source code to NPM (mswjs/interceptors#196) to prevent this from happening.

Please, would somebody have time to spin up a clean webpack app to check this?

I can confirm this occurs with a clean webpack setup

@kettanaito kettanaito changed the title New Webpack 5.0 throwing warnings webpack 5: Failed to parse source map from "@mswjs/interceptors" Jan 25, 2022
@atti187
Copy link

atti187 commented Jan 28, 2022

I guess it's just a matter of including ./src in

"files": [
.

@kettanaito
Copy link
Member

@atti187, absolutely. Would you like to open a pull request in the interceptors repo?

@atti187
Copy link

atti187 commented Jan 29, 2022

Sure, I'll look into it. Give me a few days to sort out a little time.

@atti187
Copy link

atti187 commented Jan 31, 2022

Meanwhile, source-map-loader can be configured to exclude @mswjs like this (keeping current excludes used in CRA5):

sourceMapLoader.exclude = /@babel(?:\/|\\{1,2})runtime|@mswjs/;

neldeles added a commit to neldeles/slash-clone that referenced this issue Jan 31, 2022
sclarkson-zoomcare added a commit to zoom-care/schedule-app that referenced this issue Feb 3, 2022
This is a known issue in the Interceptor project of MSW. See workaround here: mswjs/msw#1030 (comment)
@jimwilde
Copy link

jimwilde commented Feb 7, 2022

Not sure if this is related, as my problem isn't about source maps but polyfill breaking changes with webpack 5.

CRA 5
msw 0.36.8

Screenshot 2022-02-07 at 10 29 53

@kettanaito
Copy link
Member

kettanaito commented Feb 7, 2022

@jimwilde, that error suggests you're trying to bundle interceptors for a non-Node.js environment (i.e. a browser). Could you double-check that you're not using setupServer in the browser in any way?

It's evident from your stack trace:

@ ./node_modules/msw/node/lib/index.js
@ ./src/mocks/handlers.ts
@ ./src/mocks/browser.ts
@ ./src/index.ts

You can see how mocks/handlers.ts lead to msw/node, which is incorrect. Do not import msw/node in your handlers.ts, so then you could use src/mocks/browser.ts for in-browser interception without issues.

I highly suggest to take a look at the directory structure in one of our examples to avoid this mistake.

@jimwilde
Copy link

jimwilde commented Feb 8, 2022

@kettanaito

You were, of course, entirely correct in your diagnosis. Followed your suggested directory structure and all sorted. Thanks for the prompt response. Much appreciated!

@kettanaito
Copy link
Member

Note to me that this will require a minor version bump, as msw hasn't yet been updated to [email protected], which introduces reworked interception logic in Node.js.

@kettanaito kettanaito self-assigned this Feb 8, 2022
@github-actions
Copy link

🎉 This issue has been resolved in version 0.38.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

avipatel97 added a commit to avipatel97/capacitor-firebase-auth that referenced this issue Apr 24, 2022
webpack 5: Failed to parse source map from "@mswjs/interceptors"
Modeled after: mswjs/msw#1030
mswjs/interceptors#204
jay-babu pushed a commit to jay-babu/capacitor-firebase-auth that referenced this issue Apr 27, 2022
webpack 5: Failed to parse source map from "@mswjs/interceptors"
Modeled after: mswjs/msw#1030
mswjs/interceptors#204
davidvuong added a commit to davidvuong/pozition that referenced this issue Jul 10, 2022
NOTE! Source maps generated in Webpack under react-scripts are busted.

mswjs/msw#1030

GENERATE_SOURCEMAP=false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
9 participants