storesafe / cordova-sqlite-storage

A Cordova/PhoneGap plugin to open and use sqlite databases on Android, iOS and Windows with HTML5/Web SQL API
Other
2.15k stars 716 forks source link

Plugin breaks electron builds (cordova.js:1171 Uncaught Error: Module cordova-sqlite-storage.sql does not exist.) #985

Open aidanas opened 3 years ago

aidanas commented 3 years ago

If the plugin is added to project it breaks electron builds with the following errors printed to console:

cordova.js:1171 Uncaught Error: Module cordova-sqlite-storage.sql does not exist.
Uncaught Error: Module cordova-sqlite-storage.sql does not exist.
    at addEntry (cordova.js:1171)
    at Object.exports.runs (cordova.js:1193)
    at onScriptLoadingComplete (cordova.js:1331)
    at scriptLoadedCallback (cordova.js:1352)
    at HTMLScriptElement.<anonymous> (cordova.js:1306)

I understand that the plugin does not support electron platform but it used to not actively break it either. The issue can be recreated using basic "HelloWorld". Steps to recreate:

  1. cordova create helloworld com.example.hello HelloWorld
  2. cordova platform add electron@2.0.0
  3. cordova plugin add cordova-sqlite-storage
  4. cordova build electron --debug
  5. Install pkg generated at ./platforms/electron/build/
  6. Run installed app.
  7. Observe errors within devtools.

This appears to have been introduced in plugin version 5.1.0 as it still works fine on 5.0.0

Those are additional version comparisons:

| Electron version | Plugin version | result |
 ____________________________________________
|      1.0.0       |      6.0.0     |  ERROR  | 
 ____________________________________________
|      2.0.0       |      6.0.0     |  ERROR  | 
 ____________________________________________
|  3.0.0@nightly   |      6.0.0     |  ERROR  | 
 ____________________________________________
|      2.0.0       |      -         |  ERROR  | 
 ____________________________________________
|      2.0.0       |      5.1.0     |  ERROR  | 
 ____________________________________________
|      1.0.0       |      5.1.0     |  ERROR  | 
 ____________________________________________
|      2.0.0       |      5.0.0     |     OK  | 
 ____________________________________________
brodycj commented 3 years ago

I hope to test this in the near future, adding a bug label for now. Thanks.

afelipelli commented 3 years ago

Anyone found a solution to this? Or maybe a workaround? I'm getting the same issue doing:

ionic cordova build browser

after adding this version of the plugin

Murthrag commented 3 years ago

Anything new regarding this issue? I would really love to us this plugin for electron

rexx-org commented 2 years ago

I also just came across this problem. Effectively you can't build for Electron that doesn't use sqlite and for iOS that does use sqlite plugin on the same machine. Any workaround available? Thanks

rexx-org commented 2 years ago

I have a workaround but it is not pretty.

1) Remove the cordova-sqlite-storage plugin % cordova plugin remove cordova-sqlite-storage

2) Edit the following files and remove all JSON objects that have a pluginId: "cordova-sqlite-storage"

./platforms/electron/www/cordova_plugins.js ./platforms/electron/electron.json ./platforms/electron/platform_www/cordova_plugins.js

eg: { "file": "plugins/cordova-sqlite-storage/src/browser/SQLiteProxy.js", "id": "cordova-sqlite-storage.SQLiteProxy", "pluginId": "cordova-sqlite-storage", "runs": true }

These JSON objects in these files should be removed by the removal of the plugin. That is another bug I guess. It may be possible to only have to edit 1 of these files, but I don't know the relationship between these files.

3) Build the Electron platform % cordova build electron

If you want to build another platform that uses cordova-sqlite-storage plugin, then you will need to add the plugin again

Kawinesh commented 1 day ago

Hi the problem is the missing sql-asm-memory-growth.js file which is present in the plugins/cordova-sqlite-storage/node_modules/cordova-sqlite-storage-dependencies but when i open the electron Application and search for this path its not there.

Then i realized for some reason electron-builder does not copy directories named "node_modules" into my application which can be manged in seperate electon applcations by just giving a configration called

"build": {
"includeSubNodeModules": true
}

but not albe to do that in cordova application so only thing left for us is to use cordova hooks to do two things

  1. Copy the file from plugins/cordova-sqlite-storage/node_modules/cordova-sqlite-storage-dependencies to plugins/cordova-sqlite-storage-dependencies
  2. Change the references of the above in the files electron.json and cordova_plugins.js.

Please use the below hook as refernce and do the job in config.xml inside the <platform name="electron"> add <hook src="buildhooks/update-sql-storage.js" type="after_prepare" /> so it looks something like

  <platform name="electron">
        <hook src="hooks/after_prepare.js" type="after_prepare" />
        <preference name="ElectronSettingsFilePath" value="res/electron/settings.json" />
    </platform>

Create a file called after_prepare.js inside hooks folder and paste the below code

const fs = require('fs');
const path = require('path');

module.exports = function (ctx) {
    const rootDir = ctx.opts.projectRoot;
    const electronDir = path.join(rootDir, 'platforms', 'electron');
    const platformWwwDir = path.join(electronDir, 'platform_www');
    const filesToModify = [
        path.join(electronDir, 'electron.json'), // Corrected path for electron.json
        path.join(platformWwwDir, 'cordova_plugins.js'),
    ];

    const searchAndReplace = (filePath, searchValue, replaceValue) => {
        try {
            if (fs.existsSync(filePath)) {
                let fileContent = fs.readFileSync(filePath, 'utf8');
                if (fileContent.includes(searchValue)) {
                    fileContent = fileContent.replace(searchValue, replaceValue);
                    fs.writeFileSync(filePath, fileContent, 'utf8');
                    console.log(`Updated ${filePath}`);
                }
            } else {
                console.warn(`File ${filePath} does not exist.`);
            }
        } catch (err) {
            console.error(`Error updating file ${filePath}:`, err);
        }
    };

    const copyFile = (srcPath, destPath) => {
        try {
            const destDir = path.dirname(destPath);
            if (!fs.existsSync(destDir)) {
                fs.mkdirSync(destDir, { recursive: true });
            }
            fs.copyFileSync(srcPath, destPath);
            console.log(`Copied ${srcPath} to ${destPath}`);
        } catch (err) {
            console.error(`Error copying file from ${srcPath} to ${destPath}:`, err);
        }
    };

    const searchValue = 'plugins/cordova-sqlite-storage/node_modules/cordova-sqlite-storage-dependencies/sql-asm-memory-growth.js';
    const replaceValue = 'plugins/cordova-sqlite-storage-dependencies/sql-asm-memory-growth.js';

    // Search and replace in files
    filesToModify.forEach((filePath) => {
        searchAndReplace(filePath, searchValue, replaceValue);
    });

    // Copy the file
    const srcFilePath = path.join(
        platformWwwDir,
        'plugins',
        'cordova-sqlite-storage',
        'node_modules',
        'cordova-sqlite-storage-dependencies',
        'sql-asm-memory-growth.js',
    );
    const destFilePath = path.join(
        platformWwwDir,
        'plugins',
        'cordova-sqlite-storage-dependencies',
        'sql-asm-memory-growth.js',
    );

    if (fs.existsSync(srcFilePath)) {
        copyFile(srcFilePath, destFilePath);
    } else {
        console.error(`Source file ${srcFilePath} does not exist.`);
    }
};
rexx-org commented 1 day ago

Thanks for a better workaround. I have implemented the hook, albeit in a different scripting language and can confirm it works.