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

SyntaxError: In strict mode code, functions can only be declared at top level or immediately within another function. #25

Closed
whtsky opened this issue May 28, 2018 · 10 comments · Fixed by #26

Comments

@whtsky
Copy link
Contributor

whtsky commented May 28, 2018

Encountered an error while minifying vendor.87087612.chunk.js:
SyntaxError: In strict mode code, functions can only be declared at top level or immediately within another function.
  44936 | 
  44937 | if (__DEV__) {
> 44938 |   function printWarning(format, args) {
        |  ^
  44939 |     var len = arguments.length;
  44940 |     args = new Array(len > 2 ? len - 2 : 0);
  44941 |     for (var key = 2; key < len; key++) {
@TrySound
Copy link
Contributor

TrySound commented May 28, 2018

@whtsky Show your webpack config

@whtsky
Copy link
Contributor Author

whtsky commented May 28, 2018

@TrySound
Copy link
Contributor

@whtsky Do you get this with default uglify?

@whtsky
Copy link
Contributor Author

whtsky commented May 28, 2018

webpack 4 ships with uglify-es(which is out of maintaince) but we want to use uglify-js so I didn't try actually. But I assume using webpack 3 + uglifyjs would error too.

@TrySound
Copy link
Contributor

@whtsky Well, if you don't have any problems with uglify-es just keep using webpack default. They will swap to terser (uglify-es fork) soon. Unsupported package won't introduce any breaking changes.

@TrySound
Copy link
Contributor

By the way webpack4 in production mode works as expected.

@whtsky
Copy link
Contributor Author

whtsky commented May 28, 2018

if you don't have any problems with uglify-es

uglify-es can't handle inline function correctly: mishoo/UglifyJS#2842.
I know they will swap to terser soon, but since our web app are targeting IE 10, we can make sure we don't ship ES6 code accidentally by using uglify-js.

And the thing is, I don't think

warning/browser.js

Lines 21 to 22 in 0572ddd

if (process.env.NODE_ENV !== 'production') {
function printWarning(format, args) {

Is valid in ES5 strict mode.

@TrySound
Copy link
Contributor

@gaearon I saw you committed this change here. Could you clarify on this?

@oliviertassinari
Copy link

oliviertassinari commented May 28, 2018

I can reproduce the issue with Firefox 45.0.0.

@whtsky
Copy link
Contributor Author

whtsky commented May 29, 2018

looks like fbjs package transpile their code before publishing to npm

> cat node_modules/fbjs/lib/warning.js
/**
 * Copyright (c) 2014-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 */

'use strict';

var emptyFunction = require('./emptyFunction');

/**
 * Similar to invariant but only logs a warning if the condition is not met.
 * This can be used to log issues in development environments in critical
 * paths. Removing the logging code for production environments will keep the
 * same logic and follow the same code paths.
 */

var warning = emptyFunction;

if (process.env.NODE_ENV !== 'production') {
  var printWarning = function printWarning(format) {
    for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
      args[_key - 1] = arguments[_key];
    }

    var argIndex = 0;
    var message = 'Warning: ' + format.replace(/%s/g, function () {
      return args[argIndex++];
    });
    if (typeof console !== 'undefined') {
      console.error(message);
    }
    try {
      // --- Welcome to debugging React ---
      // This error was thrown as a convenience so that you can use this stack
      // to find the callsite that caused this warning to fire.
      throw new Error(message);
    } catch (x) {}
  };

  warning = function warning(condition, format) {
    if (format === undefined) {
      throw new Error('`warning(condition, format, ...args)` requires a warning ' + 'message argument');
    }

    if (format.indexOf('Failed Composite propType: ') === 0) {
      return; // Ignore CompositeComponent proptype check.
    }

    if (!condition) {
      for (var _len2 = arguments.length, args = Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
        args[_key2 - 2] = arguments[_key2];
      }

      printWarning.apply(undefined, [format].concat(args));
    }
  };
}

module.exports = warning;

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

Successfully merging a pull request may close this issue.

3 participants