lunr.js: Incompatible with ES6 Modules
I’m unable to load Lunr.js in an ES6 module. I know Lunr isn’t packaged up in an ES6 module, but imported scripts will still cause side effects and the window.lunr global can be used within an ES6 module [stack overflow] [codepen example]:
import "https://unpkg.com/lunr/lunr.js"
var documents = [{
"name": "Lunr",
"text": "Like Solr, but much smaller, and not as bright."
}, {
"name": "React",
"text": "A JavaScript library for building user interfaces."
}, {
"name": "Lodash",
"text": "A modern JavaScript utility library delivering modularity, performance & extras."
}]
const lunr = window.lunr
var idx = lunr(function () {
this.ref('name')
this.field('text')
documents.forEach(function (doc) {
this.add(doc)
}, this)
})
console.log(idx.search("bright"))
This results in an error: Uncaught TypeError: Cannot set property 'lunr' of undefined. on line 3461 root.lunr = factory()
About this issue
- Original URL
- State: open
- Created 5 years ago
- Reactions: 1
- Comments: 17 (3 by maintainers)
ping @olivernn consider adding contributors to the repo so we don’t have to wait
I just used a temp file, you can see the results here: https://github.com/indo-dev-0/lunr.js
I struggle to keep up with the changes in the JavaScript ecosystem these days, I’m more than happy to get lunr to a state where it can just work with ES6 modules but I’m not sure where to start.
Perhaps you can educate me, what is the right way of allowing lunr to be used as an ES6 module, is it possible to do so whilst maintaining backwards compatibility with plain script tags as well as npm?