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

After update to 3.0.0, Webpack build is failing #659

Closed
belcherj opened this issue Sep 11, 2018 · 13 comments
Closed

After update to 3.0.0, Webpack build is failing #659

belcherj opened this issue Sep 11, 2018 · 13 comments

Comments

@belcherj
Copy link

Updating from 2.0.4 to 3.0.0 webpack is failing, see error below. I assume I need to use a different loader but wanted to raise the issue since a quick web search and perusing the issues here did not result in any others having the issue. This method of including fetch via webpack is shared a lot in examples so there will probably be others.

In my webpack config I include fetch globally like this:

new webpack.ProvidePlugin( {
	fetch: 'imports-loader?this=>global!exports-loader?global.fetch!whatwg-fetch',
} ),
    ERROR in ./node_modules/whatwg-fetch/fetch.js (./node_modules/imports-loader?this=>global!./node_modules/exports-loader?global.fetch!./node_modules/whatwg-fetch/fetch.js) 72:0
    Module parse failed: 'import' and 'export' may only appear at the top level (72:0)
    You may need an appropriate loader to handle this file type.
    | }
    |
    > export function Headers(headers) {
    |   this.map = {};
    |
     @ ./plugins/wccom-plugins/geo-page-replace/assets/setup.js 1:0-79
@belcherj
Copy link
Author

belcherj commented Sep 11, 2018

webpack version 4.18.0 works with 2.0.4 but not with 3.0.0

@mislav
Copy link
Contributor

mislav commented Sep 11, 2018

Thank you for reporting. It looks like this webpack setup doesn't seem to respect main as specified in package.json. I'm not familiar with webpack, but is there a way it could be configured to follow main? See discussion around main vs module #656 (comment)

Note that whatwg-fetch v2 wasn't a module, but v3 is; maybe you can change your webpack configuration to load it like you load other modules?

@mislav mislav changed the title After update to 3.0.0 Webpack build is failing on line 72 After update to 3.0.0, Webpack build is failing Sep 11, 2018
@belcherj
Copy link
Author

On my list to loop back here and figure out a solution.

@andrejpavlovic
Copy link

andrejpavlovic commented Oct 3, 2018

This worked for me:

new webpack.ProvidePlugin({
  fetch: 'exports-loader?self.fetch!whatwg-fetch/dist/fetch.umd',
})

@mbifulco
Copy link

@andrejpavlovic can you explain a bit more about your solution? Was it done as a fork to fetch, or in your own project's config? Apologies for my obvious ignorance here - I haven't spent much time configuring webpack at all. Trying to get fetch 3 to work with a create-react-app project, which obfuscates webpack config, for the most part.

@mbifulco
Copy link

mbifulco commented Nov 5, 2018

Sorry to be persistent here - @belcherj any chance you've got an update or workaround for this that might work with create-react-app?

@belcherj
Copy link
Author

belcherj commented Nov 5, 2018

Sorry, got pulled onto another project and never ended up having time to figure out the solution.

@mbifulco
Copy link

Is there someone else who can look at this @belcherj?

@belcherj
Copy link
Author

belcherj commented Nov 12, 2018

I just fixed it on the that project by using @andrejpavlovic 's solution:

Remove: fetch: 'imports-loader?this=>global!exports-loader?global.fetch!whatwg-fetch',
Replace with: fetch: 'exports-loader?self.fetch!whatwg-fetch/dist/fetch.umd',

@mislav
Copy link
Contributor

mislav commented Nov 12, 2018

Would this also work? exports-loader?self.fetch!whatwg-fetch

The problem with the exports-loader?self.fetch!whatwg-fetch/dist/fetch.umd approach is that it specifies the "main" file explicitly and will break again if the "main" file changes in a future major version.

@belcherj
Copy link
Author

Tested and that works as well!

@scott-ln
Copy link

@mislav: Trying your modification results in TypeError: "exports" is read-only for me.

@nikolas
Copy link

nikolas commented Nov 13, 2019

@mislav with exports-loader?self.fetch!whatwg-fetch I get the error:

fetch.js?f8ee:520 Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
    at Module.eval (fetch.js?f8ee:520)
    at eval (index.js?self.fetch!./node_modules/whatwg-fetch/fetch.js:527)
    at Module../node_modules/exports-loader/index.js?self.fetch!./node_modules/whatwg-fetch/fetch.js (bundle-k.js:1696)
    at __webpack_require__ (bundle-k.js:20)
    at eval (Xhr.js:181)
    at Module../src/Xhr.js (bundle-k.js:4995)
    at __webpack_require__ (bundle-k.js:20)
    at eval (JuxtaposeApplication.jsx:32)
    at Module../src/JuxtaposeApplication.jsx (bundle-k.js:4779)
    at __webpack_require__ (bundle-k.js:20)

but @belcherj's solution works in my case.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants