Prompt when the plugin is running `TypeError: this.module_.onInit is not a function`

github repo: https://github.com/rxliuli/joplin-utils/blob/master/apps/joplin-plugin-backup-prettier/rollup.config.js

I suspect that the rollup configuration is not right, but I observed that the export button exists, indicating that the registration is successful, but it does report an error when clicked. . . The following is the specific error stack

TypeError: this.module_.onInit is not a function
    at InteropService_Exporter_Custom.<anonymous> (C:\Users\rxliuli\AppData\Local\Temp\1wZv2AaDwv48dSbMFd74IhSRm1n\resources\app.asar\node_modules\@joplin\lib\services\interop\InteropService_Exporter_Custom.js:25)
    at Generator.next (<anonymous>)
    at C:\Users\rxliuli\AppData\Local\Temp\1wZv2AaDwv48dSbMFd74IhSRm1n\resources\app.asar\node_modules\@joplin\lib\services\interop\InteropService_Exporter_Custom.js:8
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\rxliuli\AppData\Local\Temp\1wZv2AaDwv48dSbMFd74IhSRm1n\resources\app.asar\node_modules\@joplin\lib\services\interop\InteropService_Exporter_Custom.js:4)
    at InteropService_Exporter_Custom.init (C:\Users\rxliuli\AppData\Local\Temp\1wZv2AaDwv48dSbMFd74IhSRm1n\resources\app.asar\node_modules\@joplin\lib\services\interop\InteropService_Exporter_Custom.js:20)
    at InteropService.<anonymous> (C:\Users\rxliuli\AppData\Local\Temp\1wZv2AaDwv48dSbMFd74IhSRm1n\resources\app.asar\node_modules\@joplin\lib\services\interop\InteropService.js:290)
    at Generator.next (<anonymous>)
    at fulfilled (C:\Users\rxliuli\AppData\Local\Temp\1wZv2AaDwv48dSbMFd74IhSRm1n\resources\app.asar\node_modules\@joplin\lib\services\interop\InteropService.js:5)

rollup config

import typescript from 'rollup-plugin-typescript2'
import externals from 'rollup-plugin-node-externals'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import copy from 'rollup-plugin-copy'
import { defineConfig } from 'rollup'
import json from '@rollup/plugin-json'

export default defineConfig([
  {
    input: 'src/index.ts',
    output: { dir: 'dist', format: 'cjs', sourcemap: true },
    plugins: [
      copy({
        targets: [{ src: 'src/manifest.json', dest: 'dist' }],
      }),
      typescript(),
      nodeResolve({
        preferBuiltins: true,
      }),
      json(),
      commonjs(),
      externals(),
    ],
  },
])

What's the plugin source code? It sounds like an export module has been defined but without and onInit function.

That's because the export module needs to be a plain object. Class instances can't be serialised and sent via ipc from the plugin to app process, so probably it's just sending {} instead of your class instance.

Now, I have fixed this error, but there is a problem with the packaged .jpl, it prompts during installation

Uncaught (in promise) TypeError: invalid base256 encoding
    at Object.exports.parse (C:\Users\rxliuli\AppData\Local\Temp\1uTqxguG3Sy5HxgxBBDZ7pTGeVd\resources\app.asar\node_modules\@joplin\lib\node_modules\tar\lib\large-numbers.js:53)
    at decNumber (C:\Users\rxliuli\AppData\Local\Temp\1uTqxguG3Sy5HxgxBBDZ7pTGeVd\resources\app.asar\node_modules\@joplin\lib\node_modules\tar\lib\header.js:245)
    at Header.decode (C:\Users\rxliuli\AppData\Local\Temp\1uTqxguG3Sy5HxgxBBDZ7pTGeVd\resources\app.asar\node_modules\@joplin\lib\node_modules\tar\lib\header.js:52)
    at new Header (C:\Users\rxliuli\AppData\Local\Temp\1uTqxguG3Sy5HxgxBBDZ7pTGeVd\resources\app.asar\node_modules\@joplin\lib\node_modules\tar\lib\header.js:39)
    at Unpack.[consumeHeader] (C:\Users\rxliuli\AppData\Local\Temp\1uTqxguG3Sy5HxgxBBDZ7pTGeVd\resources\app.asar\node_modules\@joplin\lib\node_modules\tar\lib\parse.js:107)
    at Unpack.[consumeChunkSub] (C:\Users\rxliuli\AppData\Local\Temp\1uTqxguG3Sy5HxgxBBDZ7pTGeVd\resources\app.asar\node_modules\@joplin\lib\node_modules\tar\lib\parse.js:391)
    at Unpack.[consumeChunk] (C:\Users\rxliuli\AppData\Local\Temp\1uTqxguG3Sy5HxgxBBDZ7pTGeVd\resources\app.asar\node_modules\@joplin\lib\node_modules\tar\lib\parse.js:368)
    at Unpack.write (C:\Users\rxliuli\AppData\Local\Temp\1uTqxguG3Sy5HxgxBBDZ7pTGeVd\resources\app.asar\node_modules\@joplin\lib\node_modules\tar\lib\parse.js:315)
    at Yallist.<anonymous> (C:\Users\rxliuli\AppData\Local\Temp\1uTqxguG3Sy5HxgxBBDZ7pTGeVd\resources\app.asar\node_modules\@joplin\lib\node_modules\tar\node_modules\minipass\index.js:374)
    at Yallist.forEach (C:\Users\rxliuli\AppData\Local\Temp\1uTqxguG3Sy5HxgxBBDZ7pTGeVd\resources\app.asar\node_modules\@joplin\lib\node_modules\tar\node_modules\yallist\yallist.js:156)

Isn't it a simple zip file?

It's a tar file, and not sure what this error means.

The performance of joplin api is much higher than imagined. A full backup of 370 notes, a total of 47k rows, only takes less than 2s @laurent

Sorry, it seems that I encountered a similar error when using ImportModule. The specific code is in https://github.com/rxliuli/joplin-utils/blob/891a68cbc02e8a56566a924cef2ee7e42e600dbb/apps/joplin-plugin-backup-prettier/src/index.ts#L67, but I did not use class here

23:33:17: PluginRunner: Could not send IPC message: pluginMessage :  {callbackId: "cb_com.rxliuli.joplin-plugin-backup-prettier_1630683197759_3", target: "plugin", pluginId: "com.rxliuli.joplin-plugin-backup-prettier", eventId: "___plugin_event_onExec_6", args: Array(1)} Error: An object could not be cloned.
    at EventEmitter.i.send.i.send (electron/js2c/renderer_init.js:71)
    at ipcRendererSend (C:\Users\rxliuli\AppData\Local\Temp\1wZv2AaDwv48dSbMFd74IhSRm1n\resources\app.asar\services\plugins\PluginRunner.js:24)
    at C:\Users\rxliuli\AppData\Local\Temp\1wZv2AaDwv48dSbMFd74IhSRm1n\resources\app.asar\services\plugins\PluginRunner.js:52
    at Generator.next (<anonymous>)
    at C:\Users\rxliuli\AppData\Local\Temp\1wZv2AaDwv48dSbMFd74IhSRm1n\resources\app.asar\services\plugins\PluginRunner.js:8
    at new Promise (<anonymous>)
    at __awaiter (C:\Users\rxliuli\AppData\Local\Temp\1wZv2AaDwv48dSbMFd74IhSRm1n\resources\app.asar\services\plugins\PluginRunner.js:4)
    at Object.onExec (C:\Users\rxliuli\AppData\Local\Temp\1wZv2AaDwv48dSbMFd74IhSRm1n\resources\app.asar\services\plugins\PluginRunner.js:47)
    at InteropService_Importer_Custom.<anonymous> (C:\Users\rxliuli\AppData\Local\Temp\1wZv2AaDwv48dSbMFd74IhSRm1n\resources\app.asar\node_modules\@joplin\lib\services\interop\InteropService_Importer_Custom.js:21)
    at Generator.next (<anonymous>)

Strange, it's hard to tell without testing directly but maybe there's a bug in the app and the "context" object it's passing to onExec cannot be serialised for whatever reason.

Does it happen as soon as you call "registerImportModule" or only when you try to import a file?

Occurs when importing a file

Maybe it's not related to onExec but to your importer class. Are you sure all data passed to the api are plain objects and arrays? What could happen if that you accidentally pass a promise to the api, or maybe these AsynArray don't serialize properly. Unfortunately the error doesn't help much so you might want to run things separately to find the root cause.

To be honest, I always wanted to ask, why do we serialize functions?

Anything that goes through ipc needs to be serialised (that's a limitation of Electron) and it turns out functions and class instances cannot be. Only plain objects and types can be transferred.

I checked the official example, there is the same problem, maybe you need to take a look @laurent

Indeed there was some functions incorrectly being passed to the import handler. That's fixed now on dev branch and will be on next pre-release.

In addition, whether it is possible to pass an instance of a class, it seems strange that ordinary object-oriented inheritance cannot be used