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

Rollup 2.36.* produces invalid output #3924

Closed
IronGeek opened this issue Jan 15, 2021 · 4 comments · Fixed by #3926
Closed

Rollup 2.36.* produces invalid output #3924

IronGeek opened this issue Jan 15, 2021 · 4 comments · Fixed by #3926

Comments

@IronGeek
Copy link

  • Rollup Version: >=2.36.0 (including the latest 2.36.1)
  • Operating System (or Browser): any
  • Node Version (if applicable):
  • Link to reproduction (IMPORTANT, read below): repl

Rollup >=2.36.0 produces invalid output when used to bundle the following code:

const LANGUAGE_DEFAULT = 'en';
let _isWindows = false;
let _isMacintosh = false;
let _isLinux = false;
let _isNative = false;
let _isWeb = false;
let _isIOS = false;
let _locale = undefined;
let _language = LANGUAGE_DEFAULT;
let _translationsConfigFile = undefined;
let _userAgent = undefined;
const isElectronRenderer = (typeof process !== 'undefined' && typeof process.versions !== 'undefined' && typeof process.versions.electron !== 'undefined' && process.type === 'renderer');
// OS detection
if (typeof navigator === 'object' && !isElectronRenderer) {
    _userAgent = navigator.userAgent;
    _isWindows = _userAgent.indexOf('Windows') >= 0;
    _isMacintosh = _userAgent.indexOf('Macintosh') >= 0;
    _isIOS = (_userAgent.indexOf('Macintosh') >= 0 || _userAgent.indexOf('iPad') >= 0 || _userAgent.indexOf('iPhone') >= 0) && !!navigator.maxTouchPoints && navigator.maxTouchPoints > 0;
    _isLinux = _userAgent.indexOf('Linux') >= 0;
    _isWeb = true;
    _locale = navigator.language;
    _language = _locale;
}
else if (typeof process === 'object') {
    _isWindows = (process.platform === 'win32');
    _isMacintosh = (process.platform === 'darwin');
    _isLinux = (process.platform === 'linux');
    _locale = LANGUAGE_DEFAULT;
    _language = LANGUAGE_DEFAULT;    
    _isNative = true;
}
...

Expected Behavior

Produce valid output (rollup v2.35.1 produces this valid result)

...
if (typeof navigator === 'object' && !isElectronRenderer) {
  _userAgent = navigator.userAgent;
  _isWindows = _userAgent.indexOf('Windows') >= 0;
  _isMacintosh = _userAgent.indexOf('Macintosh') >= 0;
  _isIOS = (_userAgent.indexOf('Macintosh') >= 0 || _userAgent.indexOf('iPad') >= 0 || _userAgent.indexOf('iPhone') >= 0) && !!navigator.maxTouchPoints && navigator.maxTouchPoints > 0;
  _isLinux = _userAgent.indexOf('Linux') >= 0;
}
else if (typeof process === 'object') {
  _isWindows = (process.platform === 'win32');
  _isMacintosh = (process.platform === 'darwin');
  _isLinux = (process.platform === 'linux');
}
...

Actual Behavior

Produce invalid output:

...
if (typeof navigator === 'object' && !isElectronRenderer) {
  _userAgent = navigator.userAgent;
  _userAgent.indexOf('Windows') >= 0;
  _userAgent.indexOf('Macintosh') >= 0;
  (_userAgent.indexOf('Macintosh') >= 0 || _userAgent.indexOf('iPad') >= 0 || _userAgent.indexOf('iPhone') >= 0) && !!navigator.maxTouchPoints && navigator.maxTouchPoints > 0;
  _userAgent.indexOf('Linux') >= 0;
}
else if (typeof process === 'object') {
  process.platform === 'win32');
  process.platform === 'darwin');
  process.platform === 'linux');
}
...
@lukastaegert
Copy link
Member

Could you please clarify further what is the invalid part? Wrong parentheses? Code removed that is essential?

@IronGeek
Copy link
Author

Yes, part of the code is removed which resulted in syntax error.
Result in repro here might be slightly different, but in my real project it produces the following code:

else if (typeof process === 'object') {
        _isWindows = (process.platform === 'win32');
        _isMacintosh = (process.platform === 'darwin');
        process.platform === 'linux');  // <--- here 
        _locale = LANGUAGE_DEFAULT;
        _language = LANGUAGE_DEFAULT;
...

notice that the line is missing an opening parenthesis and variable assignment

@lukastaegert
Copy link
Member

Thanks! I will investigate.

@lukastaegert
Copy link
Member

Fix at #3926

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.

2 participants