Giter Site home page Giter Site logo

cordova-js's Introduction

cordova-js

NPM

Node CI

A unified JavaScript layer for Apache Cordova projects.

Project Structure

./
  |-build-tools/ ......... custom bundler for our CommonJS-like modules
  |-pkg/ ................. generated platform cordova.js files
  |
  |-src/ ................. the code that makes up Cordova's JavaScript runtime
  |  |-cordova.js ........ common Cordova stuff
  |  |
  |  |-common/ ........... base modules shared across platfoms
  |  |  |-argscheck.js ... utility for type-checking arguments during runtime
  |  |  |-base64.js ...... base64 utilites (toArrayBuffer & fromArrayBuffer)
  |  |  |-builder.js ..... utilities to install a set of properties onto an object
  |  |  |-channel.js ..... pub/sub implementation for custom framework events
  |  |  |-init.js ........ bootstraps the Cordova platform, inject APIs and fire events
  |  |  |-utils.js ....... closures, uuids, object, cloning, extending prototypes
  |  |  |
  |  |  '-exec/ .......... exec methods
  |  |     '-proxy.js .... utility for adding and removing exec proxy methods
  |  |
  |  '-scripts/ .......... non-module JS that gets concatenated to cordova.<platform>.js
  |     |-bootstrap.js ... bootstrap the Cordova platform, inject APIs and fire events
  |     '-require.js ..... module definition and require() implementation
  |
  '-tests/ ............... unit tests

How It Works

The build-tools/build.js process is a Node.js script that concatenates all of the core Cordova plugins in this repository into a cordova.<platform>.js file under the pkg/ folder. It also wraps the plugins with a RequireJS-compatible module syntax that works in both browser and node environments. We end up with a cordova.js file that wraps each Cordova plugin into its own module.

Cordova defines a channel module under src/common/channel.js, which is a publish/subscribe implementation that the project uses for event management.

The Cordova native-to-webview bridge is initialized in src/scripts/bootstrap.js. This file attaches the boot function to the channel.onNativeReady event - fired by native with a call to:

cordova.require('cordova/channel').onNativeReady.fire()

The boot method does all the work. First, it grabs the common platform definition (under src/common/common.js) and injects all of the objects defined there onto window and other global namespaces. Next, it grabs all of the platform-specific object definitions (as defined under src/<platform>/platform.js) and overrides those onto window.

Finally, it calls the platform-specific initialize function (located in the platform definition). At this point, Cordova is fully initialized and ready to roll. Last thing we do is wait for the DOMContentLoaded event to fire to make sure the page has loaded properly. Once that is done, Cordova fires the deviceready event where you can safely attach functions that consume the Cordova APIs.

Testing

Tests run in a bundled headless Chromium instance. They can be run with:

npm test

Final testing should always be done with the Mobile Spec test application.

Creating a New Platform

In your platform repository:

  1. Create the cordova-js-src directory.

  2. Write a module that encapsulates your platform's exec method and call it exec.js. This file should be added into the <platform-repo>/cordova-js-src directory which was created from step 1.

    The exec method is a JavaScript function that enables communication from the platform's JavaScript environment into the platform's native environment. Each platform uses a different mechanism to enable this bridge. We recommend you check out the other platform exec definitions for inspiration.

    The exec method has the following method signature: function(success, fail, service, action, args)

    Methods Arguments:

    • success: a success function callback
    • fail: a failure function callback
    • service: a string identifier that the platform can resolve to a native class
    • action: a string identifier that the platform can resolve to a specific method inside the class pointed to by service
    • args: an array of parameters to pass to the native method invoked by the exec call

    It is required that new platform additions be as consistent as possible with the existing service and action labels.

  3. Define your platform definition object and name it platform.js. This file should be added into the <platform-repo>/cordova-js-src directory which was created from step 1.

    This file should export an object with the following properties:

    • id: a string representing the platform. This should match the name of the .js file.
    • bootstrap: A function that sets up the platform. Must fire the onNativeReady channel when done.
    • initialize: an optional function that is called after the global scope setup is done (i.e. Cordova and all plugins are ready)

    The following is a simple example of a platform definition:

    module.exports = {
        id: 'atari',
        bootstrap: function() {
            require('cordova/channel').onNativeReady.fire();
        }
    };
  4. Bundle the modules from cordova-js/src and <platform-repo>/cordova-js-src into a file that ends up in <platform-project>/platform_www/cordova.js. This can be done in various ways. The following is recommended:

    • Add cordova-js as a devDependency: npm i -D cordova-js
    • Build cordova.js when preparing your platform's npm package. You can do that by adding the NPM prepare hook script to your package.json:
      "scripts": {
        "prepare": "cordova-js build > project-template/platform_www/cordova.js",
        // ...
      }
    • During project creation, make sure that the cordova.js file created by the prepare script ends up where your platform expects it

cordova-js's People

Contributors

agrieve avatar audreyso avatar brianleroux avatar bryanhiggins avatar brycecurtis avatar clelland avatar dpogue avatar erisu avatar filmaj avatar gtanner avatar hermwong avatar imhotep avatar infil00p avatar jaycanuck avatar jlongster avatar jsoref avatar macdonst avatar mingfengwang avatar mmocny avatar mwoghiren avatar pmuellr avatar pplaquette avatar purplecabbage avatar raphinesse avatar revolunet avatar sgrebnov avatar shazron avatar stevengill avatar surajpindoria avatar timkim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cordova-js's Issues

Migrate from jasmine-node to Jasmine 3

I picked up maintainer responsibility of jasmine-node as a temporary workaround to resolve npm audit issues before the next major release. Now that we are targeting the next major release I think we should migrate from jasmine-node to Jasmine 3, which I think would need some minor changes to the JavaScript.

Event listener function overloads are too brittle, error if evt is undefined

The document.addEventListener, document.removeEventListener, window.addEventListener, window.removeEventListener overloads in cordova.js error if the evt variable is undefined. While this is something that also be fixed upstream in the relevant calling code, these functions should not be erroring as that is not the behaviour of what they are overloading.

window.addEventListener(undefined, () => {})

In cordova.js, the above would cause an error.
In safari/chrome console, no error is triggered.

I have create a PR to address this.
#245

Related:
ionic-team/capacitor#4178
mapbox/mapbox-gl-js#11433

Can run in webwork?

Feature Request

Can it run in webwork?

Motivation Behind Feature

JS engine is single-threaded, if a large number of time-consuming complex operations will make UI pause, such as reading and writing files, operating database, which will lead to poor user experience. So we hope to put these operations into the underlying thread (web work) to ensure that users operate smoothly.

Feature Description

ordova JS uses many main variables such as windows, which makes it impossible to use them in Web work (in this case, the main variable is self). Can you decide whether to use windows or self according to your judgment?

Alternatives or Workarounds

I am currently rewriting the code manually, but I lose all of this work every time I upgrade.

Run client-side JS through Babel

Feature Request

Motivation Behind Feature

This would allow us to just set the environments we want to support in the Babel config and stop worrying about which JS features are supported by which version of which WebView.

Feature Description

As we have a build step anyways, so we would just have to add Babel to the build process.

The only real drawback I can see is that our generated cordova.js might not be as readable as before. But I'd be OK with that.

Real-life examples where not having this bit me: #203 #204

Cannot read property 'startsWith' of undefined

Bug Report

Problem

What is expected to happen?

I'm trying to build a Cordova platform for Android with Adaptive Icons.

What does actually happen?

I get this error:

Cannot read property 'startsWith' of undefined
TypeError: Cannot read property 'startsWith' of undefined
    at updateIconResourceForAdaptive (/media/file/Documents/Code/sc4-mobile/platforms/android/cordova/lib/prepare.js:475:24)
    at updateIcons (/media/file/Documents/Code/sc4-mobile/platforms/android/cordova/lib/prepare.js:402:23)
    at /media/file/Documents/Code/sc4-mobile/platforms/android/cordova/lib/prepare.js:64:9
    at _fulfilled (/media/file/Documents/Code/sc4-mobile/node_modules/q/q.js:854:54)
    at /media/file/Documents/Code/sc4-mobile/node_modules/q/q.js:883:30
    at Promise.promise.promiseDispatch (/media/file/Documents/Code/sc4-mobile/node_modules/q/q.js:816:13)
    at /media/file/Documents/Code/sc4-mobile/node_modules/q/q.js:624:44
    at runSingle (/media/file/Documents/Code/sc4-mobile/node_modules/q/q.js:137:13)
    at flush (/media/file/Documents/Code/sc4-mobile/node_modules/q/q.js:125:13)
    at processTicksAndRejections (internal/process/task_queues.js:79:11)

Command or Code

cordova build android

Environment, Platform, Device

Ubuntu 20.04.1. Mac Pro 5,1.

Version information

Cordova 10.0.0

Checklist

  • I searched for existing GitHub issues
  • I updated all Cordova tooling to most recent version
  • I included all the necessary information above

Uniform build

It would be nice for cordova-js to be platform-independent. While I have not had a closer look at the how and why of the current build strategy, here's an outline of what I imagine:

Suggestion

  • Create a platform-independent build of cordova-js before publishing to npm and bundle it in the published package (it's not checked into git). My tool of choice for this would be rollup at the moment of writing.
  • Have platforms require cordova-js to include the core bundle in the App HTML. Any platform specific JS is handled in the platform but can make use of the provided core functionality

Positive effects

  • cordova-js can be platform agnostic => easier to add new platforms
  • switch to modern build tooling (rollup)
  • Releasing a new version of cordova-js can be done independently
  • Updating of cordova-js in a platform requires only npm i cordova-js@latest

I don't have time to look into this right now, but I'd appreciate any feedback on this


Related: #162

Script execution is prohibited

When I try to debug my project with VS Code I get following error: [cordova-tools] Error processing "launch": Script execution is prohibited

Output from debug-console:

Attaching to app
An error occurred while attaching to the debugger. Script execution is prohibited
Script execution is prohibitedError processing "launch": Error: Script execution is prohibited
at Client.processMessage (c:\Users\thhei.vscode\extensions\msjsdiag.cordova-tools-1.8.6\node_modules\noice-json-rpc\lib\noice-json-rpc.js:66:36)
at LoggingSocket.Client.socket.on (c:\Users\thhei.vscode\extensions\msjsdiag.cordova-tools-1.8.6\node_modules\noice-json-rpc\lib\noice-json-rpc.js:42:48)
at LoggingSocket.emit (events.js:187:15)
at Receiver.receiverOnMessage (c:\Users\thhei.vscode\extensions\msjsdiag.cordova-tools-1.8.6\node_modules\ws\lib\websocket.js:744:20)
at Receiver.emit (events.js:182:13)
at Receiver.dataMessage (c:\Users\thhei.vscode\extensions\msjsdiag.cordova-tools-1.8.6\node_modules\ws\lib\receiver.js:417:14)
at perMessageDeflate.decompress (c:\Users\thhei.vscode\extensions\msjsdiag.cordova-tools-1.8.6\node_modules\ws\lib\receiver.js:374:23)
at _decompress (c:\Users\thhei.vscode\extensions\msjsdiag.cordova-tools-1.8.6\node_modules\ws\lib\permessage-deflate.js:298:9)
at _inflate.flush (c:\Users\thhei.vscode\extensions\msjsdiag.cordova-tools-1.8.6\node_modules\ws\lib\permessage-deflate.js:376:7)
at afterWrite (_stream_writable.js:480:3)

Information

Command or Code

Debugging: Simulate Android in browser

Environment, Platform, Device

Version information

OS: Windows 10 Home Version 1903
IDE: VS Code
cordova version: 9.0.0 ([email protected])
Plugins: none
Chrome: 77.0.3865.90

Checklist

  • I searched for existing GitHub issues
  • I updated all Cordova tooling to most recent version
  • I included all the necessary information above

Could not find cordova.js script tag

Bug Report

Problem

running my app in a android device, the splashscreen never hide.
cheking the adb catlog, there are 3 main errors:

Error
I chromium: [INFO:CONSOLE(1)] "Uncaught Reference
Error: globalThis is not defined", source: http://localhost/main.b5e1b42bf56f00a
f562e.js (1)

I chromium: [INFO:CONSOLE(1)] "Could not find cordova.js script tag.
Plugin loading may fail.", source: http://localhost/cordova.5687a8f7461b757f98c1.js (1)

I chromium: [INFO:CONSOLE(1)] "Ionic Native: deviceready event fired after 632 ms", source: http://localhost/main.b5e1b42bf56f00a
f562e.js (1)

Environment, Platform, Device

Ionic:

Ionic CLI : 6.16.1 (C:\Users\User\AppData\Roaming\npm\n
ode_modules@ionic\cli)
Ionic Framework : @ionic/angular 5.6.11
@angular-devkit/build-angular : 12.0.5
@angular-devkit/schematics : 12.0.5
@angular/cli : 12.0.5
@ionic/angular-toolkit : 4.0.0

Cordova:

Cordova CLI : 10.0.0
Cordova Platforms : not available
Cordova Plugins : not available

Utility:

cordova-res : 0.15.3
native-run (update available: 1.4.0) : 1.3.0

Any Help?
Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.