ES6 is not supported after Electron packaging

ε祈祈猫儿з 提交于 2019-12-07 03:14:51

问题


I'm using various ES6 syntax (such as import etc.) & React code (JSX) in my Electron-based application. During the development, I'm using the electron-prebuilt-compile package (as a dev-dependency) in order to support these new features and it works perfectly fine without any errors.

But after packaging my app using the electron-packager package and running the distributable application file, I experiencing unsupported ES6-related errors such as:

Unexpected token import

That's is how I run the electron-packager command (notice to the platform & architecture flags):

electron-packager . MyCoolApp --platform=linux --arch=x64

Any reason why a packaged/distributable version of my application does not support ES6/React features?


回答1:


Solved.

it turns out that devDependencies are being omitted during packaging by default, which means that the electron-prebuild-compile package is "out of the game" for a packaged application and without it ES6 can't be transcompiled. So in order to deactivate this default behavior, I had to call the packager command with the --no-prune flag so that the devDependencies will remain without being deleted:

electron-packager . MyCoolApp --platform=linux --arch=x64 --no-prune

In addition, I had to introduce a new script (let's name it: es6-init.js) for initialization of the main app's script in order to "compile" the code before rendering (it should be used as the main entry point script of your application):

var appRoot = path.join(__dirname, '..');

require('electron-compile').init(appRoot, require.resolve('./main'));

References:

  • https://github.com/electron-userland/electron-compile#how-does-it-work-slightly-harder-way
  • https://github.com/electron-userland/electron-packager



回答2:


Nodejs(and with it, Electron) does not support import and export. You will need to use require();



来源:https://stackoverflow.com/questions/49556041/es6-is-not-supported-after-electron-packaging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!