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

Build Fail: UglifyJs cannot minify generator function #3972

Closed
JackWReid opened this issue Feb 10, 2018 · 10 comments
Closed

Build Fail: UglifyJs cannot minify generator function #3972

JackWReid opened this issue Feb 10, 2018 · 10 comments

Comments

@JackWReid
Copy link

When running gatsby build on this project. I get the following error:

Error: component---src-pages-index-js-a3ebb96f3fe42e5f6e93.js from UglifyJs
SyntaxError: Unexpected token operator «*», expected punc «(» [component---src-pages-index-js-a3ebb96f3fe42e5f6e93.js:3028,42]

After investigation it seems that this method in src/components/CurrentlyReading.js:

async componentDidMount() {
  const response = await fetch('https://api.jackreid.xyz/reading');
  const readingData = await response.json();
  this.setState({ readingData });
}

is transpiled into:

var _ref = _asyncToGenerator(function* () {
  var response = yield fetch('https://api.jackreid.xyz/reading');
  var readingData = yield response.json();
  this.setState({ readingData: readingData });
});

function componentDidMount() {
  return _ref.apply(this, arguments);
}

So it looks like the build is generating an async generator syntax that uglify can't handle. For now the only workaround I can think of is deleting or commenting out the uglify section of node_modules/gatsby/dist/utils/webpack.config.js as a prebuild step. Any guidance would be appreciated.

@JackWReid JackWReid changed the title Build Fail: Uglify Cannot Minify Generator Function Build Fail: UglifyJs cannot minify generator function Feb 11, 2018
@JackWReid
Copy link
Author

For now I've just changed the async componentDidMount() method to a syncronous one with a then-able synax inside.

  componentDidMount() {
    fetch('https://api.jackreid.xyz/reading')
    .then(response => response.json())
    .then(readingData => this.setState({ readingData }));
  }

Still, it seems like a bug that part of the build process can turn valid React into JS at a stage that the uglifier config can't cope with, no?

@secretfader
Copy link

I'm seeing a similar issue in one of my projects. Even though I'm pulling from a library (which is compiled down to ES5), I'm still encountering the following error: SyntaxError: Unexpected token punc «}», expected punc «:».

Yet, since they're both Uglify errors, I bet they're related.

@m-allanson
Copy link
Contributor

@JackWReid I think this will be solved with Gatsby version 2, when webpack and it's related dependencies will all be updated. Until then if you want to keep using async functions, this might be helpful? #3931 (comment)

@nicholaswyoung Your error looks slightly different... Could you create a new issue for this? If you can create an example repo that'd be very useful too!

@secretfader
Copy link

secretfader commented Feb 13, 2018

@m-allanson But we shouldn't have to upgrade to Gatsby 2 to eliminate this error. Gatsby 1 should continue to work fine if configured properly. 😉

The site in question on my end has been running Gatsby 1.0 since the prereleases. It's a fairly large site, and I don't have the time to create a repro at the moment. I think this is a bug, potentially with a wider footprint than you originally surmised.

@m-allanson
Copy link
Contributor

m-allanson commented Feb 13, 2018

Gatsby 1 should continue to work fine if configured properly.

@nicholaswyoung I agree! I'm not fully familiar with Gatsby's babel / webpack / Uglify setup, but the issue you're seeing seems to be this: #3780, which sounds like it has no easy answer.

For a "quick fix" or workaround you might need to find where your es6 code is coming from (assuming that's what the issue is), then polyfill or remove it?

@KyleAMathews
Copy link
Contributor

@nicholaswyoung the version of uglify we're using doesn't support es6 — we also don't compile code in node_modules. We're considering following CRA's lead and adding limited support for compiling code in node_modules but we won't do this until Gatsby v2 facebook/create-react-app#3776

In the meantime, if you're using a dependency that ships es6 code, you'll either need to vendor the code or fork the package and publish an es5 version.

We know this isn't a great solution and we're sorry. If you look at the CRA repo, there's tons of people complaining about this as well so it's something that the whole ecosystem is facing as JavaScript moves forward but we web folks are still supporting older browsers.

@JackWReid
Copy link
Author

That workaround looks nice @m-allanson, thanks! Out of interest what's the timeline for v2?

@m-allanson
Copy link
Contributor

I don't think there is a timeline, but you can see progress here: https://github.com/gatsbyjs/gatsby/projects/2

@m-allanson
Copy link
Contributor

m-allanson commented Mar 20, 2018

Closing this as there's no further action to be taken here. Please re-open or create a new issue if I'm wrong!

@stefanwille
Copy link

How about a warning in the documentation about async/await not working?
I have wasted quite a bit of time to find this out.

aouerf pushed a commit to envyclient/website-old that referenced this issue Apr 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants