weixsong / elasticlunr.js

Based on lunr.js, but more flexible and customized.
http://elasticlunr.com
MIT License
2.03k stars 148 forks source link

Support browser ES6 imports #110

Open mdaffin opened 5 years ago

mdaffin commented 5 years ago

It would be nice to be able to use elasticlunr as an ES6 import which are now supported by most major browsers. This allows better code organisation and keeps the global name space cleaner. A minimal example that almost works:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <script defer async type="module">
      import * as elasticlunr from "http://elasticlunr.com/elasticlunr.js";
    </script>
  </head>
  <body></body>
</html>

But results in the error:

eferenceError: assignment to undeclared variable lunrelasticlunr.js:90:1
    <anonymous> http://elasticlunr.com/elasticlunr.js:90
    <anonymous> http://elasticlunr.com/elasticlunr.js:2507
    InnerModuleEvaluation self-hosted:4147
    InnerModuleEvaluation self-hosted:4136
    evaluation self-hosted:4101

And fails to actually work and adding a var to line 90 then results in this error:

TypeError: root is undefinedelasticlunr.js:2497:26
    <anonymous> http://127.0.0.1:1111/elasticlunr.js:2497
    <anonymous> http://127.0.0.1:1111/elasticlunr.js:2499
    <anonymous> http://127.0.0.1:1111/elasticlunr.js:2507
    InnerModuleEvaluation self-hosted:4147
    InnerModuleEvaluation self-hosted:4136
    evaluation self-hosted:4101
airhorns commented 3 years ago

I feel like it might be best to wrap the code that adds the global alias in a function so that users can invoke it if they need it, and otherwise not try to muck around with the global?