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: expected expression, got keyword 'import' #571

Closed
nrenner opened this issue Jun 28, 2022 · 1 comment
Closed

SyntaxError: expected expression, got keyword 'import' #571

nrenner opened this issue Jun 28, 2022 · 1 comment
Labels
Milestone

Comments

@nrenner
Copy link
Owner

nrenner commented Jun 28, 2022

The new import() call is causing a parse error on load in older browsers:

SyntaxError: expected expression, got keyword 'import'  brouter-web.js:55:7723

I expected the function call to fail only when called (i.e. when a mvn layer is added), but it seems import is a reserved keyword even in older browsers that don't support modules or dynamic imports yet.

Reported browser versions in use:

  • Firefox 52.9.0 ESR [1]
  • Firefox 56 [2]
  • Firefox 68 via Seamonkey [3]

The reason given is that those still support old plugins, which don't have an adequate Web Extensions replacement.

@nrenner nrenner added the bug label Jun 28, 2022
nrenner added a commit that referenced this issue Jun 28, 2022
- add import() polyfill that evals in Function() and falls back to script tag injection
- add AbortController polyfill for MapLibre (lazy loaded so can be in bundle, not for Web Workers)
- support Firefox 56, last supporting old plugins before Web Extensions
@nrenner
Copy link
Owner Author

nrenner commented Jun 28, 2022

Adding Firefox 56 to browserslist, as this is the last one supporting old-style plugins before Web Extensions only.

Babel seems to only rewrite import() to require() and relies on Webpack or AMD/CommonJS loaders to handle those (babel#9551), which leads to a ReferenceError: require is not defined in our case. So I disabled that with by the caller: { supportsDynamicImport: true } option (9852aaf).

The dynamic import polyfills I found either rely on module support (e.g. dynamic-import-polyfill) or provide complete module support we don't need (e.g. shimport). So I derived bits from those and others for our own simple polyfill that evaluates the import call inside a new Function('')() to avoid the parsing error and falls back to script tag injection in old browsers.

MapLibre needs an AbortController polyfill in older browsers (maplibre-gl-js#783) that core-js doesn't provide yet (core-js#445). Unfortunately that only works for the hillshading (DEM) layer, not for the Mapillary (vector tile) layer, where Web Workers fail, and I don't know how to polyfill them in an easy way.

@nrenner nrenner closed this as completed Jun 28, 2022
@nrenner nrenner added this to the 0.18.1 milestone Jun 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant