Kekule.js: Using Kekule npm module with Webpack does not load Kekule properly.

References: #32 Steps to duplicate:

Begin any project with webpack base.

yarn add kekule

In a js file you import from index.html:

import {Kekule} from 'kekule'
or 
const {Kekule} = require('kekule')

console.log(Kekule)

Now run webpack to minify your project and open the output file and receive:

kekule.js:504 Uncaught TypeError: Cannot read property 'getElementsByTagName' of undefined
    at analysisEntranceScriptSrc (kekule.js:504)
    at init (kekule.js:631)
    at kekule.js:671
    at Object.<anonymous> (kekule.js:673)
    at Object.defineProperty.value (page.js:128749)
    at __webpack_require__ (bootstrap 2cce55343c358056e4dd:19)
    at Object.Array.indexOf.i (kekule.composer.component.js:4)
    at __webpack_require__ (bootstrap 2cce55343c358056e4dd:19)
    at Object.<anonymous> (moldraw.js:6)
    at __webpack_require__ (bootstrap 2cce55343c358056e4dd:19)
    at webpackContext (.*\.js$:111)
    at sl.module.factory.js:11

In this particular case, we see that document is undefined. In #32 we see that passing the window object works for the webpack compiler but when the script is trying to load in the browser we get errors that it cannot find the proper files. This is because webpack did not see explicit requires of those files and loses them in the build process.

Potential Fixes:

  1. Instead of appending to a script tag to them DOM, check if webpack was used and instead require the scripts.
  • Cons:
    • Each file must now officially export the class it’s building since webpack compilation for the browser does not have access to vm.runInThisContext.
    • Each file imported to kekule.js must now be imported and exported appropriately for webpack and node
  1. Somehow figure out how to import a context of a module. Basically to do for webpack what vm.runInThisContext does for node.
  • Cons:
    • It doesn’t exist (to my knowledge)

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 2
  • Comments: 31 (14 by maintainers)

Most upvoted comments

@amitsetty The change of webpack.config.js just tells webpack not to mangle some special function parameters to a short variable name such as e and does nothing else. So in my consideration the loading time should not be slowed down so much. In my own test your demo file can be loaded and parsed in less than 700ms in my computer.

Unfortunately with the current master (Dec 26, 2019):

    "kekule": "github:partridgejiang/Kekule.js#afd3b0a1ee78dfd38ed61f404e24295303fe9c6a",

importing using all three methods described by @mpomet above produce the following error for me:

kekule.min.js:1 Uncaught TypeError: Cannot read property 'createElement' of undefined
    at kekule.min.js:1
    at Object../node_modules/kekule/dist/kekule.min.js (kekule.min.js:1)
    at __webpack_require__ (bootstrap:79)
    at Object../node_modules/kekule/dist/kekule.webpack.prod.js (kekule.webpack.prod.js:2)
    at __webpack_require__ (bootstrap:79)
    at kekule.js:909
    at Object.<anonymous> (kekule.js:914)
    at Object../node_modules/kekule/dist/kekule.js (vendor.js:273567)
    at __webpack_require__ (bootstrap:79)
    ...

This error seems to be coming from the loading of the kekule webpack dist file.

This wasn’t happening with (Oct 12, 2019):

    "kekule": "github:partridgejiang/Kekule.js#6ca9cd5fa75a634e1916e3e67e86411d32483627",