Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

"global" check is naive, and causes really odd bugs #132

Closed
jakearchibald opened this issue Oct 18, 2016 · 1 comment · Fixed by #149
Closed

"global" check is naive, and causes really odd bugs #132

jakearchibald opened this issue Oct 18, 2016 · 1 comment · Fixed by #149

Comments

@jakearchibald
Copy link

jakearchibald commented Oct 18, 2016

const firstpassGlobal = /\b(?:require|module|exports|global)\b/;

As far as I can tell, this is string-searching for "global" and switching behaviour as a result.

My builds started failing because I had a JS comment that contained the word "global".

The error suggested the failure was when it tried to parse an async function - not sure why it failed there, but if I delete "global" from a previous comment everything works again.

Here's my rollup config:

return rollup({
  entry: src,
  sourceMap: true,
  cache,
  format: 'iife',
  plugins: [
    nodeResolve({
      preferBuiltins: false,
      browser: true,
      jsnext: true,
      main: true
    }),
    commonjs(),
    rollupBabel({
      presets: ['stage-3', ['es2015', {modules: false}]],
      plugins: [["transform-react-jsx", {pragma:"h"}], "external-helpers"]
    })
  ]
})

I've added {ignoreGlobal: true} to commonjs to fix things in the meantime.

@Rich-Harris
Copy link
Contributor

The regex is used to quickly determine if a file could be a CommonJS file – if it doesn't contain require, or module, or exports, or global, then clearly it isn't a CommonJS file so the plugin skips it to speed things up.

If one of those words is detected, then we have to parse it (at this stage we're still not sure if it's CommonJS or not). Once we have the AST we can do things like check to see if global is defined locally (if it's used, and isn't in a comment) – and then either transform it or skip it.

But we never get to that point, because of this line. async isn't valid ES6, so Acorn fails to parse it.

Two possible solutions:

  • We change 6 to 8, so that async is allowed. Don't see a good reason not to do this – will try and get round to it later
  • Transpile the code (with Babel or rollup-plugin-async, or whatever) before this plugin sees it – transforms happen in the order they're listed in the plugins array

Rich-Harris added a commit that referenced this issue Dec 14, 2016
Rich-Harris added a commit that referenced this issue Dec 14, 2016
use ecmaVersion: 8, not 6
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants